DawnC commited on
Commit
ea980d5
·
verified ·
1 Parent(s): 0347f2d

Update response_processor.py

Browse files
Files changed (1) hide show
  1. response_processor.py +78 -43
response_processor.py CHANGED
@@ -315,20 +315,80 @@ class ResponseProcessor:
315
  def _critical_format_preprocess(self, response: str) -> str:
316
  """
317
  關鍵格式預處理,處理最常見的格式問題
318
-
319
  Args:
320
  response: 原始回應
321
-
322
  Returns:
323
  str: 預處理後的回應
324
  """
325
  if not response:
326
  return response
327
-
328
  try:
329
  import re
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
 
331
- # 第一優先級:處理斜線問題
332
  # 首先處理已知的斜線組合,使用形容詞替換
333
  for slash_combo, replacement in self.slash_replacements.items():
334
  if slash_combo.lower() in response.lower():
@@ -339,13 +399,11 @@ class ResponseProcessor:
339
  replacement_formatted = replacement.title()
340
  else:
341
  replacement_formatted = replacement
342
-
343
  # 執行替換(不區分大小寫)
344
  response = re.sub(re.escape(slash_combo), replacement_formatted, response, flags=re.IGNORECASE)
345
- self.logger.debug(f"Replaced slash pattern '{slash_combo}' with '{replacement_formatted}'")
346
-
347
  # 處理其他未預定義的斜線模式
348
- # 標準斜線模式:word/word
349
  slash_pattern = r'\b([a-zA-Z]+)/([a-zA-Z]+)\b'
350
  matches = list(re.finditer(slash_pattern, response))
351
  for match in reversed(matches): # 從後往前處理避免位置偏移
@@ -356,22 +414,19 @@ class ResponseProcessor:
356
  else:
357
  replacement = word2
358
  response = response[:match.start()] + replacement + response[match.end():]
359
- self.logger.debug(f"Replaced general slash pattern '{match.group(0)}' with '{replacement}'")
360
-
361
- # 第二優先級:處理底線格式
362
  # 首先處理已知的底線組合
363
  for underscore_combo, replacement in self.underscore_replacements.items():
364
  if underscore_combo in response:
365
  response = response.replace(underscore_combo, replacement)
366
- self.logger.debug(f"Replaced underscore pattern '{underscore_combo}' with '{replacement}'")
367
-
368
  # 處理三個詞的底線組合:word_word_word → word word word
369
  response = re.sub(r'\b([a-z]+)_([a-z]+)_([a-z]+)\b', r'\1 \2 \3', response)
370
-
371
  # 處理任何剩餘的底線模式:word_word → word word
372
  response = re.sub(r'\b([a-zA-Z]+)_([a-zA-Z]+)\b', r'\1 \2', response)
373
-
374
- # 第三優先級:修正不完整句子
375
  incomplete_sentence_fixes = [
376
  (r'\bIn\s*,\s*', 'Throughout the area, '),
377
  (r'\bOverall,\s+exudes\b', 'Overall, the scene exudes'),
@@ -379,35 +434,15 @@ class ResponseProcessor:
379
  (r'\bwith its lights turned illuminating\b', 'with its lights illuminating'),
380
  (r'\bwhere it stands as\b', 'where it stands as'),
381
  ]
382
-
383
  for pattern, replacement in incomplete_sentence_fixes:
384
  response = re.sub(pattern, replacement, response, flags=re.IGNORECASE)
385
-
386
- # 第四優先級:語法修正處理(像是person and people)
387
- grammar_fixes = [
388
- (r'\b(\d+)\s+persons\b', r'\1 people'),
389
- (r'\bone\s+persons\b', 'one person'),
390
- (r'\btwo\s+persons\b', 'two people'),
391
- (r'\bthree\s+persons\b', 'three people'),
392
- (r'\bfour\s+persons\b', 'four people'),
393
- (r'\bfive\s+persons\b', 'five people'),
394
- (r'\bsix\s+persons\b', 'six people'),
395
- (r'\bseven\s+persons\b', 'seven people'),
396
- (r'\beight\s+persons\b', 'eight people'),
397
- (r'\bnine\s+persons\b', 'nine people'),
398
- (r'\bten\s+persons\b', 'ten people'),
399
- (r'\bmultiple\s+persons\b', 'multiple people'),
400
- (r'\bseveral\s+persons\b', 'several people'),
401
- (r'\bmany\s+persons\b', 'many people'),
402
- (r'\ba\s+few\s+persons\b', 'a few people'),
403
- (r'\bsome\s+persons\b', 'some people')
404
- ]
405
-
406
- for pattern, replacement in grammar_fixes:
407
- response = re.sub(pattern, replacement, response, flags=re.IGNORECASE)
408
-
409
  return response
410
-
411
  except Exception as e:
412
  self.logger.warning(f"Error in critical format preprocessing: {str(e)}")
413
  return response
@@ -1158,4 +1193,4 @@ class ResponseProcessor:
1158
  "suffixes_to_remove_count": len(self.suffixes_to_remove),
1159
  "repetitive_patterns_count": len(self.repetitive_patterns),
1160
  "initialization_status": "success"
1161
- }
 
315
  def _critical_format_preprocess(self, response: str) -> str:
316
  """
317
  關鍵格式預處理,處理最常見的格式問題
318
+
319
  Args:
320
  response: 原始回應
321
+
322
  Returns:
323
  str: 預處理後的回應
324
  """
325
  if not response:
326
  return response
327
+
328
  try:
329
  import re
330
+ # 移除各種形式的 confirmed
331
+ confirmed_patterns = [
332
+ r'\bconfirmed\s+', # "confirmed cars" -> "cars"
333
+ r'\b(\d+)\s+confirmed\s+([a-zA-Z\s]+)', # "12 confirmed cars" -> "12 cars"
334
+ r'\b(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)\s+confirmed\s+([a-zA-Z\s]+)', # "twelve confirmed cars" -> "twelve cars"
335
+ ]
336
+
337
+ for pattern in confirmed_patterns:
338
+ if pattern == r'\bconfirmed\s+':
339
+ response = re.sub(pattern, '', response, flags=re.IGNORECASE)
340
+ else:
341
+ response = re.sub(pattern, r'\1 \2', response, flags=re.IGNORECASE)
342
+
343
+ # 數字轉文字的完整字典
344
+ number_conversions = {
345
+ '0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five',
346
+ '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten',
347
+ '11': 'eleven', '12': 'twelve', '13': 'thirteen', '14': 'fourteen', '15': 'fifteen',
348
+ '16': 'sixteen', '17': 'seventeen', '18': 'eighteen', '19': 'nineteen', '20': 'twenty'
349
+ }
350
+
351
+ # 強化數字替換邏輯 - 處理各種語法結構
352
+ for digit, word in number_conversions.items():
353
+ # 模式1: 數字 + 名詞 (如 "3 cars", "12 people")
354
+ pattern1 = rf'\b{digit}\s+([a-zA-Z]+(?:\s+[a-zA-Z]+)*)\b'
355
+ response = re.sub(pattern1, rf'{word} \1', response)
356
+
357
+ # 模式2: 數字 + visible/present + 名詞 (如 "3 visible traffic lights")
358
+ pattern2 = rf'\b{digit}\s+(visible|present|apparent|evident)\s+([a-zA-Z]+(?:\s+[a-zA-Z]+)*)\b'
359
+ response = re.sub(pattern2, rf'{word} \1 \2', response, flags=re.IGNORECASE)
360
+
361
+ # 模式3: 介詞 + 數字 + 名詞 (如 "against a backdrop of 3 visible traffic lights")
362
+ pattern3 = rf'\b(against|with|featuring|including|containing)\s+(?:a\s+backdrop\s+of\s+)?{digit}\s+([a-zA-Z]+(?:\s+[a-zA-Z]+)*)\b'
363
+ response = re.sub(pattern3, rf'\1 {word} \2', response, flags=re.IGNORECASE)
364
+
365
+ # 模式4: 複合描述中的數字 (如 "featuring twelve confirmed cars and 3 confirmed persons")
366
+ pattern4 = rf'\b(and|,)\s+{digit}\s+([a-zA-Z]+(?:\s+[a-zA-Z]+)*)\b'
367
+ response = re.sub(pattern4, rf'\1 {word} \2', response, flags=re.IGNORECASE)
368
+
369
+ grammar_fixes = [
370
+ # persons -> people 的全面修正
371
+ (r'\b(\d+|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)\s+persons\b', r'\1 people'),
372
+ (r'\bmultiple\s+persons\b', 'multiple people'),
373
+ (r'\bseveral\s+persons\b', 'several people'),
374
+ (r'\bmany\s+persons\b', 'many people'),
375
+ (r'\ba\s+few\s+persons\b', 'a few people'),
376
+ (r'\bsome\s+persons\b', 'some people'),
377
+ (r'\bvarious\s+persons\b', 'various people'),
378
+ (r'\bnumerous\s+persons\b', 'numerous people'),
379
+
380
+ # 修正語法結構問題
381
+ (r'\bvisible\s+traffic\s+lights\b', 'traffic lights visible'),
382
+ (r'\bpresent\s+traffic\s+lights\b', 'traffic lights present'),
383
+ (r'\bapparent\s+traffic\s+lights\b', 'traffic lights apparent'),
384
+
385
+ # 修正重複的形容詞結構
386
+ (r'\b(visible|present|apparent|evident)\s+(visible|present|apparent|evident)\s+', r'\1 '),
387
+ ]
388
+
389
+ for pattern, replacement in grammar_fixes:
390
+ response = re.sub(pattern, replacement, response, flags=re.IGNORECASE)
391
 
 
392
  # 首先處理已知的斜線組合,使用形容詞替換
393
  for slash_combo, replacement in self.slash_replacements.items():
394
  if slash_combo.lower() in response.lower():
 
399
  replacement_formatted = replacement.title()
400
  else:
401
  replacement_formatted = replacement
402
+
403
  # 執行替換(不區分大小寫)
404
  response = re.sub(re.escape(slash_combo), replacement_formatted, response, flags=re.IGNORECASE)
405
+
 
406
  # 處理其他未預定義的斜線模式
 
407
  slash_pattern = r'\b([a-zA-Z]+)/([a-zA-Z]+)\b'
408
  matches = list(re.finditer(slash_pattern, response))
409
  for match in reversed(matches): # 從後往前處理避免位置偏移
 
414
  else:
415
  replacement = word2
416
  response = response[:match.start()] + replacement + response[match.end():]
417
+
 
 
418
  # 首先處理已知的底線組合
419
  for underscore_combo, replacement in self.underscore_replacements.items():
420
  if underscore_combo in response:
421
  response = response.replace(underscore_combo, replacement)
422
+
 
423
  # 處理三個詞的底線組合:word_word_word → word word word
424
  response = re.sub(r'\b([a-z]+)_([a-z]+)_([a-z]+)\b', r'\1 \2 \3', response)
425
+
426
  # 處理任何剩餘的底線模式:word_word → word word
427
  response = re.sub(r'\b([a-zA-Z]+)_([a-zA-Z]+)\b', r'\1 \2', response)
428
+
429
+ # 確保句子的完整性
430
  incomplete_sentence_fixes = [
431
  (r'\bIn\s*,\s*', 'Throughout the area, '),
432
  (r'\bOverall,\s+exudes\b', 'Overall, the scene exudes'),
 
434
  (r'\bwith its lights turned illuminating\b', 'with its lights illuminating'),
435
  (r'\bwhere it stands as\b', 'where it stands as'),
436
  ]
437
+
438
  for pattern, replacement in incomplete_sentence_fixes:
439
  response = re.sub(pattern, replacement, response, flags=re.IGNORECASE)
440
+
441
+ # 清理多餘空格並確保格式一致性
442
+ response = re.sub(r'\s+', ' ', response).strip()
443
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
  return response
445
+
446
  except Exception as e:
447
  self.logger.warning(f"Error in critical format preprocessing: {str(e)}")
448
  return response
 
1193
  "suffixes_to_remove_count": len(self.suffixes_to_remove),
1194
  "repetitive_patterns_count": len(self.repetitive_patterns),
1195
  "initialization_status": "success"
1196
+ }