GopiBNA commited on
Commit
4ba6fa3
·
verified ·
1 Parent(s): b03b1d6

Upload 11 files

Browse files
Files changed (1) hide show
  1. engine/comparator.py +65 -13
engine/comparator.py CHANGED
@@ -53,6 +53,46 @@ def _canon(s):
53
  return re.sub(r"\s+", " ", str(s)).strip().lower()
54
 
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  def _combine_ai_rows(ai_rows):
57
  """Multi-row handling: when the AI Excel has several rows for the
58
  same Field name, merge them into a single row. Values are joined
@@ -409,11 +449,22 @@ def write_comparison_sheet(ws, rows, lang, tie, reco_only=False, project_url=Non
409
  except Exception:
410
  pass
411
  # Free-text fields (Description / Milestones / Principal Works /
412
- # Main Works) — machine-translate the TRANSFORMED VALUE column
413
- # only. The AI's output is always in English, so the Transformed
414
- # Value needs to be rendered in the researcher's selected
415
- # Output Language. The BNA Value column is left as-extracted so
416
- # it mirrors exactly what BNA Platform shows.
 
 
 
 
 
 
 
 
 
 
 
417
  try:
418
  ai_field_norm = _norm(row.get("ai_field"))
419
  FREE_TEXT_FIELDS = {
@@ -427,15 +478,16 @@ def write_comparison_sheet(ws, rows, lang, tie, reco_only=False, project_url=Non
427
  "principal works", "obras principales", "obras principais",
428
  }
429
  if lang != "en" and ai_field_norm in FREE_TEXT_FIELDS:
430
- # Translate the Transformed Value (AI's English → target lang)
431
- if transformed_disp and len(str(transformed_disp).strip()) > 20:
 
 
 
 
 
432
  transformed_disp = _translator.translate_free_text(
433
- str(transformed_disp), src_lang="en", dst_lang=lang, tie=tie)
434
- # AI Value and BNA Value are intentionally NOT translated:
435
- # - AI Value: stays as the AI Excel wrote it (English)
436
- # - BNA Value: stays as extracted from BNA Platform
437
- # (whatever language BNA stored — usually English,
438
- # sometimes editor-translated)
439
  except Exception:
440
  pass
441
  # "Recommendation" column now carries the Match/Differ verdict directly.
 
53
  return re.sub(r"\s+", " ", str(s)).strip().lower()
54
 
55
 
56
+ # Quick heuristic for free-text language detection. Returns 'en', 'es',
57
+ # 'pt', or 'unknown'. Used to decide whether the Transformed Value
58
+ # column should be machine-translated to match BNA's language.
59
+ _PT_STOPS = re.compile(
60
+ r"\b(?:o|a|os|as|do|da|dos|das|um|uma|de|para|com|por|que|n[aã]o|ser|est[aã]|s[aã]o|mais|seu|sua|seus|suas|projeto|obras|opera[çc][aã]o)\b",
61
+ re.IGNORECASE)
62
+ _ES_STOPS = re.compile(
63
+ r"\b(?:el|la|los|las|del|al|de|en|para|con|por|que|no|este|esta|sus|ser|est[áa]|son|m[aá]s|su|proyecto|obras|operaci[oó]n)\b",
64
+ re.IGNORECASE)
65
+ _EN_STOPS = re.compile(
66
+ r"\b(?:the|of|to|and|in|for|is|on|with|that|this|by|as|will|are|its|project|works|operation)\b",
67
+ re.IGNORECASE)
68
+
69
+
70
+ def _detect_text_lang(text):
71
+ if not text:
72
+ return "unknown"
73
+ s = str(text).strip()
74
+ if len(s) < 40:
75
+ return "unknown"
76
+ low = s.lower()
77
+ # Strong signal from accented characters that English doesn't use.
78
+ pt_char_hits = sum(low.count(c) for c in "çãõẽâô")
79
+ es_char_hits = sum(low.count(c) for c in "ñ¿¡")
80
+ # Stopword counts as the main vote.
81
+ pt = len(_PT_STOPS.findall(low))
82
+ es = len(_ES_STOPS.findall(low))
83
+ en = len(_EN_STOPS.findall(low))
84
+ # Accent characters bias the verdict toward PT/ES.
85
+ pt_score = pt + 3 * pt_char_hits
86
+ es_score = es + 3 * es_char_hits
87
+ if pt_score >= en and pt_score > es_score and pt_score >= 2:
88
+ return "pt"
89
+ if es_score >= en and es_score > pt_score and es_score >= 2:
90
+ return "es"
91
+ if en >= 2:
92
+ return "en"
93
+ return "unknown"
94
+
95
+
96
  def _combine_ai_rows(ai_rows):
97
  """Multi-row handling: when the AI Excel has several rows for the
98
  same Field name, merge them into a single row. Values are joined
 
449
  except Exception:
450
  pass
451
  # Free-text fields (Description / Milestones / Principal Works /
452
+ # Main Works) — language-aware Transformed Value.
453
+ #
454
+ # Translation rule, per row:
455
+ # 1. If the BNA Value came back in the target language (because
456
+ # BNA editors translated it for this project), translate the
457
+ # Transformed Value to the target language too — both
458
+ # columns line up and the comparison is apples-to-apples.
459
+ # 2. If the BNA Value came back in English (because BNA has no
460
+ # translated version stored for this project), KEEP the
461
+ # Transformed Value as the AI's original English. That way
462
+ # the comparison is between Agentic's original English text
463
+ # and BNA's English text — same language, fair comparison.
464
+ #
465
+ # This sidesteps the previous problem where translating the
466
+ # Transformed Value to ES/PT while BNA Value stayed English made
467
+ # the two columns un-comparable visually.
468
  try:
469
  ai_field_norm = _norm(row.get("ai_field"))
470
  FREE_TEXT_FIELDS = {
 
478
  "principal works", "obras principales", "obras principais",
479
  }
480
  if lang != "en" and ai_field_norm in FREE_TEXT_FIELDS:
481
+ bna_text_lang = _detect_text_lang(str(bna_disp or ""))
482
+ # Only translate the Transformed Value when BNA actually
483
+ # returned the target language. Otherwise leave it as the
484
+ # AI's original English so it aligns with the BNA column.
485
+ if (bna_text_lang == lang
486
+ and transformed_disp
487
+ and len(str(transformed_disp).strip()) > 20):
488
  transformed_disp = _translator.translate_free_text(
489
+ str(transformed_disp), src_lang="en",
490
+ dst_lang=lang, tie=tie)
 
 
 
 
491
  except Exception:
492
  pass
493
  # "Recommendation" column now carries the Match/Differ verdict directly.