helal94hb1 commited on
Commit
67e765b
·
1 Parent(s): 9d90187

fix: abbreviation AA

Browse files
app/api/v2_endpoints.py CHANGED
@@ -154,7 +154,7 @@ async def handle_v2_query(
154
  original_query = request.query
155
 
156
  # --- EDIT: Call the new, direct replacement function ---
157
- normalized_query = query_expansion_service.expand_tph_in_query(original_query)
158
 
159
  if original_query != normalized_query:
160
  logger.info(f"Query expanded from '{original_query}' to '{normalized_query}'")
 
154
  original_query = request.query
155
 
156
  # --- EDIT: Call the new, direct replacement function ---
157
+ normalized_query = query_expansion_service.replace_abbreviations(original_query)
158
 
159
  if original_query != normalized_query:
160
  logger.info(f"Query expanded from '{original_query}' to '{normalized_query}'")
app/services/query_expansion_service.py CHANGED
@@ -164,6 +164,41 @@ def expand_tph_in_query(query_text: str) -> str:
164
 
165
  return re.sub(pattern, replacement, query_text, flags=re.IGNORECASE)
166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  # async def generate_query_variations(query: str, num_variations: int = 2) -> List[str]:
168
  # """
169
  # Uses a local T5 model to generate paraphrases of the user's query.
 
164
 
165
  return re.sub(pattern, replacement, query_text, flags=re.IGNORECASE)
166
 
167
+ def replace_abbreviations(query_text: str) -> str:
168
+ """
169
+ Expands a predefined set of abbreviations.
170
+ Specifically, "AA" is only replaced if "Arrangement" is not already in the query.
171
+ """
172
+ # 1. Define all replacement rules.
173
+ replacements = {
174
+ 'tph': 'Payment Hub',
175
+ 'aa': 'Arrangement'
176
+ # Add other unconditional replacements here.
177
+ }
178
+
179
+ # 2. Check if the word "Arrangement" is already in the query (case-insensitive).
180
+ # We use a regex with \b to ensure we match the whole word.
181
+ if re.search(r'\bArrangement\b', query_text, re.IGNORECASE):
182
+ # If "Arrangement" is found, we don't want to replace "AA".
183
+ # So, we remove the 'aa' rule from our dictionary for this run.
184
+ del replacements['aa']
185
+
186
+ # 3. If there are no rules left to apply, return the original query.
187
+ if not replacements:
188
+ return query_text
189
+
190
+ # 4. Build the regex pattern ONLY with the rules that are still active.
191
+ pattern = re.compile(
192
+ r'\b(' + '|'.join(replacements.keys()) + r')\b',
193
+ re.IGNORECASE
194
+ )
195
+
196
+ # 5. The replacer function remains the same.
197
+ def get_replacement(match):
198
+ return replacements[match.group(0).lower()]
199
+
200
+ # 6. Perform the substitution and return the result.
201
+ return pattern.sub(get_replacement, query_text)
202
  # async def generate_query_variations(query: str, num_variations: int = 2) -> List[str]:
203
  # """
204
  # Uses a local T5 model to generate paraphrases of the user's query.