youssefreda9 commited on
Commit
b4e2427
·
1 Parent(s): e7b2a51

Fix grammar JSON spacing bug

Browse files
src/nlp/grammar/grammar_rules.py CHANGED
@@ -718,7 +718,14 @@ class ArabicGrammarGuard:
718
  """Apply all grammar rules to model output."""
719
  text = self.preserve_numbers(original_text, generated_text)
720
 
721
- # ── Fix Hallucinated Subject Gender & Protect Structured Data ──
 
 
 
 
 
 
 
722
  orig_words = original_text.split()
723
  corr_words = text.split()
724
  if len(orig_words) == len(corr_words):
@@ -728,8 +735,8 @@ class ArabicGrammarGuard:
728
  o_clean = o.rstrip('.,،؛;:!؟?()[]{}«»"\'…')
729
  c_clean = c.rstrip('.,،؛;:!؟?()[]{}«»"\'…')
730
 
731
- # Protect structured data (English, JSON, Hashtags, Code)
732
- if re.search(r'[a-zA-Z]|\{.*\}|\[.*\]|<.*>|#\S+|@\S+', o):
733
  corr_words[i] = o
734
  # Revert grammar hallucinations on adjacent Arabic words caused by structured data
735
  if i > 0:
 
718
  """Apply all grammar rules to model output."""
719
  text = self.preserve_numbers(original_text, generated_text)
720
 
721
+ # ── Fix T5 Spacing Hallucinations & Protect Structured Data ──
722
+ # T5 often adds spaces around structural brackets. Fix them before splitting.
723
+ for bracket in ['{', '}', '[', ']', '<', '>']:
724
+ if f' {bracket}' not in original_text:
725
+ text = text.replace(f' {bracket}', bracket)
726
+ if f'{bracket} ' not in original_text:
727
+ text = text.replace(f'{bracket} ', bracket)
728
+
729
  orig_words = original_text.split()
730
  corr_words = text.split()
731
  if len(orig_words) == len(corr_words):
 
735
  o_clean = o.rstrip('.,،؛;:!؟?()[]{}«»"\'…')
736
  c_clean = c.rstrip('.,،؛;:!؟?()[]{}«»"\'…')
737
 
738
+ # Protect structured data (English, JSON, Code, URLs)
739
+ if re.search(r'[a-zA-Z]|\{|\[|<|#|@|://', o):
740
  corr_words[i] = o
741
  # Revert grammar hallucinations on adjacent Arabic words caused by structured data
742
  if i > 0:
src/nlp/punctuation/punctuation_rules.py CHANGED
@@ -167,7 +167,7 @@ def validate_punctuation_diff(diff: dict, full_text: str = '') -> bool:
167
 
168
  # ── Protect Structured Data (English, URLs, Emails, Hashtags, Code/JSON) ──
169
  # Block punctuation modifications near structured data unless it's a valid terminal punctuation
170
- if re.search(r'[a-zA-Z]|\{.*\}|\[.*\]|<.*>|#\S+|@\S+', original):
171
  is_at_end = False
172
  if full_text and 'end' in diff:
173
  is_at_end = diff['end'] >= len(full_text) - 2
@@ -183,7 +183,7 @@ def validate_punctuation_diff(diff: dict, full_text: str = '') -> bool:
183
  return False
184
 
185
  # Block spacing corruptions in JSON/Code (e.g. {"name"} -> { "name" })
186
- if re.search(r'\{.*\}|\[.*\]|<.*>|https?://', original):
187
  # Only allow if the ONLY change is appending a terminal mark at the very end
188
  if original != correction and not (is_at_end and correction.endswith(('.', '؟')) and correction[:-1].rstrip() == original.rstrip()):
189
  logger.info(f"[PUNC-SAFETY] Blocked corruption of JSON/Code/URL: '{original}' -> '{correction}'")
 
167
 
168
  # ── Protect Structured Data (English, URLs, Emails, Hashtags, Code/JSON) ──
169
  # Block punctuation modifications near structured data unless it's a valid terminal punctuation
170
+ if re.search(r'[a-zA-Z]|\{|\[|<|#|@|://', original):
171
  is_at_end = False
172
  if full_text and 'end' in diff:
173
  is_at_end = diff['end'] >= len(full_text) - 2
 
183
  return False
184
 
185
  # Block spacing corruptions in JSON/Code (e.g. {"name"} -> { "name" })
186
+ if re.search(r'\{|\[|<|://', original):
187
  # Only allow if the ONLY change is appending a terminal mark at the very end
188
  if original != correction and not (is_at_end and correction.endswith(('.', '؟')) and correction[:-1].rstrip() == original.rstrip()):
189
  logger.info(f"[PUNC-SAFETY] Blocked corruption of JSON/Code/URL: '{original}' -> '{correction}'")