youssefreda9 commited on
Commit
7520375
·
1 Parent(s): 291a28f

debug: Add grammar diff logging to diagnose missing suggestions

Browse files
Files changed (1) hide show
  1. src/app.py +7 -0
src/app.py CHANGED
@@ -857,6 +857,8 @@ def analyze_text():
857
  from nlp.grammar.grammar_service import correct_grammar
858
  corrected_grammar = correct_grammar(current_text)
859
  logger.info(f"[ANALYZE] Step 2: Grammar done in {time.time()-t0:.2f}s")
 
 
860
  if corrected_grammar != current_text:
861
  # Word-level grammar diffs (individual words, not chunks)
862
  g_orig_words = get_word_positions(current_text)
@@ -868,6 +870,7 @@ def analyze_text():
868
  )
869
 
870
  for tag, i1, i2, j1, j2 in g_matcher.get_opcodes():
 
871
  if tag == 'replace':
872
  # Process each original word individually
873
  for k in range(i1, i2):
@@ -886,10 +889,12 @@ def analyze_text():
886
  if existing['type'] == 'spelling':
887
  if not (orig_end <= existing['start'] or orig_start >= existing['end']):
888
  overlaps = True
 
889
  break
890
  if overlaps:
891
  continue
892
 
 
893
  suggestions.append({
894
  'start': orig_start,
895
  'end': orig_end,
@@ -901,6 +906,8 @@ def analyze_text():
901
 
902
  mappers.append(OffsetMapper(current_text, corrected_grammar))
903
  current_text = corrected_grammar
 
 
904
  except Exception as e:
905
  logger.error(f"[ANALYZE] Grammar failed: {e}")
906
 
 
857
  from nlp.grammar.grammar_service import correct_grammar
858
  corrected_grammar = correct_grammar(current_text)
859
  logger.info(f"[ANALYZE] Step 2: Grammar done in {time.time()-t0:.2f}s")
860
+ logger.info(f"[GRAMMAR] Input: '{current_text}'")
861
+ logger.info(f"[GRAMMAR] Output: '{corrected_grammar}'")
862
  if corrected_grammar != current_text:
863
  # Word-level grammar diffs (individual words, not chunks)
864
  g_orig_words = get_word_positions(current_text)
 
870
  )
871
 
872
  for tag, i1, i2, j1, j2 in g_matcher.get_opcodes():
873
+ logger.info(f"[GRAMMAR] opcode: {tag} orig[{i1}:{i2}]={[w[0] for w in g_orig_words[i1:i2]]} → corr[{j1}:{j2}]={[w[0] for w in g_corr_words[j1:j2]]}")
874
  if tag == 'replace':
875
  # Process each original word individually
876
  for k in range(i1, i2):
 
889
  if existing['type'] == 'spelling':
890
  if not (orig_end <= existing['start'] or orig_start >= existing['end']):
891
  overlaps = True
892
+ logger.info(f"[GRAMMAR] SKIP '{o_word}'→'{c_word}' (overlaps with spelling '{existing['original']}'→'{existing['correction']}')")
893
  break
894
  if overlaps:
895
  continue
896
 
897
+ logger.info(f"[GRAMMAR] ADD suggestion: '{o_word}' → '{c_word}' at orig[{orig_start}:{orig_end}]")
898
  suggestions.append({
899
  'start': orig_start,
900
  'end': orig_end,
 
906
 
907
  mappers.append(OffsetMapper(current_text, corrected_grammar))
908
  current_text = corrected_grammar
909
+ else:
910
+ logger.info(f"[GRAMMAR] No changes — grammar output == input")
911
  except Exception as e:
912
  logger.error(f"[ANALYZE] Grammar failed: {e}")
913