Lyonel Tanganco commited on
Commit
20ea429
·
1 Parent(s): d6c18ca

add tags.py

Browse files
Files changed (1) hide show
  1. src/utils/tags.py +37 -0
src/utils/tags.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def tag_user_input(file_path, user_input):
2
+ """
3
+ Tag user input with keywords/substances from a file.
4
+
5
+ This function:
6
+ 1. Loads tags from the specified file (preserving original case)
7
+ 2. Detects matches in user input (case-insensitive, partial matches)
8
+ 3. Returns a list of matching tags in their original case
9
+
10
+ Args:
11
+ file_path (str): Path to the file containing tags (one per line)
12
+ user_input (str): The user's input text to search for tags
13
+
14
+ Returns:
15
+ list: List of matching tags in their original case from the file
16
+ """
17
+ # Load tags (preserve original case)
18
+ tags_original = []
19
+ tags_lower = []
20
+ with open(file_path, 'r', encoding='utf-8') as f:
21
+ for line in f:
22
+ stripped = line.strip()
23
+ if stripped:
24
+ tags_original.append(stripped)
25
+ tags_lower.append(stripped.lower())
26
+
27
+ # Convert user input to lowercase for case-insensitive matching
28
+ user_input_lower = user_input.lower()
29
+
30
+ # Find matching tags (case-insensitive, partial matches)
31
+ matching_tags = []
32
+ for i, tag_lower in enumerate(tags_lower):
33
+ if tag_lower in user_input_lower:
34
+ matching_tags.append(tags_original[i])
35
+
36
+ return matching_tags
37
+