RJ3vans commited on
Commit
d339273
·
verified ·
1 Parent(s): 72de3a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +219 -219
app.py CHANGED
@@ -2739,256 +2739,256 @@ def process_text(input_text):
2739
  para_simplified = []
2740
 
2741
  for original_sent in sents:
2742
- hidden, lists = hide_paren_lists(original_sent)
2743
- working_set = deque([(hidden, lists)])
2744
- seen = set()
2745
- sentence_final = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2746
 
2747
- iters = 0
2748
- while working_set:
2749
- iters += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
2750
 
2751
- if iters > MAX_ITERS:
2752
  print(
2753
- f"ABORT: exceeded {MAX_ITERS} iterations; "
2754
- f"draining {len(working_set)} item(s) to final"
2755
  )
2756
- while working_set:
2757
- t, ls = working_set.popleft()
2758
- for s in expand_paren_lists(t, ls):
2759
- sentence_final.append(clean_output(s))
2760
- break
2761
 
2762
- current, lists = working_set.popleft()
2763
- current = current.strip()
2764
- if not current:
2765
- continue
2766
 
2767
- current = clean_output(current)
2768
- if not current:
2769
- continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2770
 
2771
- key = re.sub(r"\s+", " ", current.lower())
2772
- if key in seen:
2773
- print(f"SKIP duplicate: {current[:80]}...")
2774
- for s in expand_paren_lists(current, lists):
2775
- sentence_final.append(clean_output(s))
2776
- continue
2777
- seen.add(key)
 
 
 
 
 
 
 
 
2778
 
2779
- print(
2780
- f"\n--- iter {iters} | queue={len(working_set)} | "
2781
- f"{current[:100]}..."
2782
- )
2783
 
2784
- tagged_list = tag_all_signs_in_sent(current)
 
 
 
 
2785
 
2786
- has_ssccv = any(
2787
- any(x in t for x in ["M:Y_SSCCV", "M:Y_ESCCV"])
2788
- for t in tagged_list
2789
- )
2790
- has_ssmn = any(
2791
- any(x in t for x in ["M:Y_SSMN", "M:Y_ESMN"])
2792
- for t in tagged_list
2793
- )
2794
- has_ccv = any(
2795
- "<M:Y_CCV>" in t or "M:Y_CCV" in t for t in tagged_list
2796
- )
2797
- has_cmv1 = any(
2798
- "<M:Y_CMV1>" in t or "M:Y_CMV1" in t for t in tagged_list
2799
- )
2800
- has_cmn1 = any(
2801
- "<M:Y_CMN1>" in t or "M:Y_CMN1" in t for t in tagged_list
2802
- )
2803
- has_cln = any(
2804
- "<M:Y_CLN>" in t or "M:Y_CLN" in t for t in tagged_list
2805
- )
 
 
 
 
 
2806
 
2807
- skipped_ssccv = set()
2808
- mode = xml_tag = None
2809
- simplifier = span_fn = None
2810
- bracketed_version = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2811
 
2812
- # SSCCV first, but skip cognitive/communicative VPs
2813
- if has_ssccv:
2814
- while True:
2815
  bracketed_version = build_single_sign_bracketed(
2816
- tagged_list,
2817
- mode="ssccv",
2818
- skip_indices=skipped_ssccv,
2819
  )
2820
  if not bracketed_version:
2821
- break
2822
-
 
2823
  print(f"\nProcessing: {current}")
2824
  print(
2825
- f"Bracketed version (ssccv): {bracketed_version}"
2826
  )
2827
-
2828
  cleaned = clean_for_spaCy(bracketed_version)
2829
- tokens, pos, lemmas, preds = tag_SSCCV_span(cleaned)
2830
  tokens, pos, lemmas, preds = drop_bracket_tokens(
2831
  tokens, pos, lemmas, preds
2832
  )
2833
 
2834
- if is_cognitive_communicative_vp(preds):
2835
- ssccv_idxs = [
2836
- i
2837
- for i, t in enumerate(tagged_list)
2838
- if "M:Y_SSCCV" in str(t)
2839
- or "M:Y_ESCCV" in str(t)
2840
- ]
2841
- remaining = [
2842
- i
2843
- for i in ssccv_idxs
2844
- if i not in skipped_ssccv
2845
- ]
2846
- if remaining:
2847
- skipped_ssccv.add(remaining[0])
2848
- print(
2849
- " Skip SSCCV "
2850
- "(cognitive/communicative VP)"
2851
- )
2852
- continue
2853
-
2854
- mode = "ssccv"
2855
- xml_tag = "M:Y_SSCCV"
2856
- simplifier = SSCCVsimplify
2857
- span_fn = tag_SSCCV_span
2858
- break
2859
-
2860
- if mode is None and has_ssmn:
2861
- mode = "ssmn"
2862
- xml_tag = "M:Y_SSMN"
2863
- simplifier = SSMNsimplify
2864
- span_fn = tag_SSMN_span
2865
- elif mode is None and has_ccv:
2866
- mode = "ccv"
2867
- xml_tag = "M:Y_CCV"
2868
- simplifier = CompoundSimplify
2869
- span_fn = tag_CCV_span
2870
- elif mode is None and has_cmv1:
2871
- mode = "cmv1"
2872
- xml_tag = "M:Y_CMV1"
2873
- simplifier = CompoundSimplify
2874
- span_fn = tag_CMV1_span
2875
- elif mode is None and has_cmn1:
2876
- mode = "cmn1"
2877
- xml_tag = "M:Y_CMN1"
2878
- simplifier = CompoundSimplify
2879
- span_fn = tag_CMN1_span
2880
- elif mode is None and has_cln:
2881
- mode = "cln"
2882
- xml_tag = "M:Y_CLN"
2883
- simplifier = CompoundSimplify
2884
- span_fn = tag_CLN_span
2885
- elif mode is None:
2886
- for s in expand_paren_lists(current, lists):
2887
- sentence_final.append(clean_output(s))
2888
- continue
2889
-
2890
- if mode != "ssccv":
2891
- bracketed_version = build_single_sign_bracketed(
2892
- tagged_list, mode=mode
2893
- )
2894
- if not bracketed_version:
2895
- for s in expand_paren_lists(current, lists):
2896
- sentence_final.append(clean_output(s))
2897
- continue
2898
- print(f"\nProcessing: {current}")
2899
- print(
2900
- f"Bracketed version ({mode}): {bracketed_version}"
2901
  )
2902
- cleaned = clean_for_spaCy(bracketed_version)
2903
- tokens, pos, lemmas, preds = span_fn(cleaned)
2904
- tokens, pos, lemmas, preds = drop_bracket_tokens(
2905
- tokens, pos, lemmas, preds
2906
  )
2907
 
2908
- cleaned_tokens, clean_pos, clean_lemmas, cleaned_preds = (
2909
- sign_cleaning(tokens, pos, lemmas, preds)
2910
- )
2911
- improved_preds = improve_predicted_tag_sequence(
2912
- cleaned_preds
2913
- )
2914
-
2915
- xml_sent = create_xml_sent(
2916
- cleaned_tokens,
2917
- clean_pos,
2918
- clean_lemmas,
2919
- improved_preds,
2920
- xml_tag,
2921
- )
2922
- simps = simplifier(xml_sent)
2923
- if simps:
2924
- simps = [fix_subject_verb_agreement(s) for s in simps]
2925
-
2926
- if (
2927
- simps
2928
- and simplifier is CompoundSimplify
2929
- and xml_tag
2930
- in ("M:Y_CCV", "M:Y_CMV1", "M:Y_CMN1", "M:Y_CLN")
2931
- ):
2932
- coord = extract_coordinator_from_xml(xml_sent)
2933
- if coord:
2934
- rewritten = []
2935
- for s in simps:
2936
- s2 = rewrite_residual_list_comma(s, coord)
2937
- if s2 != s:
2938
- print(
2939
- f" Residual list rewrite ({coord}): "
2940
- f"{s!r} → {s2!r}"
2941
- )
2942
- rewritten.append(s2)
2943
- simps = rewritten
2944
-
2945
- if simps and xml_tag == "M:Y_CMN1":
2946
- simps = [fix_subject_verb_agreement(s) for s in simps]
2947
-
2948
- if not simps:
2949
- print(
2950
- "Simplifier returned []; forcing final (no re-queue)"
2951
  )
2952
- current = fix_subject_verb_agreement(current)
2953
- for s in expand_paren_lists(current, lists):
2954
- sentence_final.append(clean_output(s))
2955
- continue
2956
 
2957
- queued_any = False
2958
- for s in simps:
2959
- plain_s = clean_output(s)
2960
- plain_s = fix_subject_verb_agreement(plain_s)
2961
- if not plain_s:
2962
- continue
2963
- plain_key = re.sub(r"\s+", " ", plain_s.lower())
2964
- if plain_key == key:
2965
- print(
2966
- f" No progress (same as input): "
2967
- f"{plain_s[:80]}..."
2968
- )
2969
- for exp in expand_paren_lists(plain_s, lists):
2970
- sentence_final.append(clean_output(exp))
2971
- continue
2972
- if plain_key in seen:
 
 
 
 
 
 
 
2973
  print(
2974
- f" Skip already-seen result: "
2975
- f"{plain_s[:80]}..."
2976
  )
2977
- for exp in expand_paren_lists(plain_s, lists):
2978
- sentence_final.append(clean_output(exp))
 
2979
  continue
2980
- working_set.append((plain_s, lists))
2981
- queued_any = True
2982
- print(f" → Queued: {plain_s}")
2983
-
2984
- if not queued_any:
2985
- for s in expand_paren_lists(current, lists):
2986
- sentence_final.append(clean_output(s))
2987
-
2988
- for s in order_sentences_for_discourse(sentence_final):
2989
- s = clean_output(s)
2990
- if s:
2991
- para_simplified.append(s)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2992
 
2993
  final_output = "\n\n".join(output_paragraphs)
2994
  print(f"\n=== FINAL OUTPUT ===\n{final_output}")
 
2739
  para_simplified = []
2740
 
2741
  for original_sent in sents:
2742
+ hidden, lists = hide_paren_lists(original_sent)
2743
+ working_set = deque([(hidden, lists)])
2744
+ seen = set()
2745
+ sentence_final = []
2746
+
2747
+ iters = 0
2748
+ while working_set:
2749
+ iters += 1
2750
+
2751
+ if iters > MAX_ITERS:
2752
+ print(
2753
+ f"ABORT: exceeded {MAX_ITERS} iterations; "
2754
+ f"draining {len(working_set)} item(s) to final"
2755
+ )
2756
+ while working_set:
2757
+ t, ls = working_set.popleft()
2758
+ for s in expand_paren_lists(t, ls):
2759
+ sentence_final.append(clean_output(s))
2760
+ break
2761
 
2762
+ current, lists = working_set.popleft()
2763
+ current = current.strip()
2764
+ if not current:
2765
+ continue
2766
+
2767
+ current = clean_output(current)
2768
+ if not current:
2769
+ continue
2770
+
2771
+ key = re.sub(r"\s+", " ", current.lower())
2772
+ if key in seen:
2773
+ print(f"SKIP duplicate: {current[:80]}...")
2774
+ for s in expand_paren_lists(current, lists):
2775
+ sentence_final.append(clean_output(s))
2776
+ continue
2777
+ seen.add(key)
2778
 
 
2779
  print(
2780
+ f"\n--- iter {iters} | queue={len(working_set)} | "
2781
+ f"{current[:100]}..."
2782
  )
 
 
 
 
 
2783
 
2784
+ tagged_list = tag_all_signs_in_sent(current)
 
 
 
2785
 
2786
+ has_ssccv = any(
2787
+ any(x in t for x in ["M:Y_SSCCV", "M:Y_ESCCV"])
2788
+ for t in tagged_list
2789
+ )
2790
+ has_ssmn = any(
2791
+ any(x in t for x in ["M:Y_SSMN", "M:Y_ESMN"])
2792
+ for t in tagged_list
2793
+ )
2794
+ has_ccv = any(
2795
+ "<M:Y_CCV>" in t or "M:Y_CCV" in t for t in tagged_list
2796
+ )
2797
+ has_cmv1 = any(
2798
+ "<M:Y_CMV1>" in t or "M:Y_CMV1" in t for t in tagged_list
2799
+ )
2800
+ has_cmn1 = any(
2801
+ "<M:Y_CMN1>" in t or "M:Y_CMN1" in t for t in tagged_list
2802
+ )
2803
+ has_cln = any(
2804
+ "<M:Y_CLN>" in t or "M:Y_CLN" in t for t in tagged_list
2805
+ )
2806
 
2807
+ skipped_ssccv = set()
2808
+ mode = xml_tag = None
2809
+ simplifier = span_fn = None
2810
+ bracketed_version = None
2811
+
2812
+ # SSCCV first, but skip cognitive/communicative VPs
2813
+ if has_ssccv:
2814
+ while True:
2815
+ bracketed_version = build_single_sign_bracketed(
2816
+ tagged_list,
2817
+ mode="ssccv",
2818
+ skip_indices=skipped_ssccv,
2819
+ )
2820
+ if not bracketed_version:
2821
+ break
2822
 
2823
+ print(f"\nProcessing: {current}")
2824
+ print(
2825
+ f"Bracketed version (ssccv): {bracketed_version}"
2826
+ )
2827
 
2828
+ cleaned = clean_for_spaCy(bracketed_version)
2829
+ tokens, pos, lemmas, preds = tag_SSCCV_span(cleaned)
2830
+ tokens, pos, lemmas, preds = drop_bracket_tokens(
2831
+ tokens, pos, lemmas, preds
2832
+ )
2833
 
2834
+ if is_cognitive_communicative_vp(preds):
2835
+ ssccv_idxs = [
2836
+ i
2837
+ for i, t in enumerate(tagged_list)
2838
+ if "M:Y_SSCCV" in str(t)
2839
+ or "M:Y_ESCCV" in str(t)
2840
+ ]
2841
+ remaining = [
2842
+ i
2843
+ for i in ssccv_idxs
2844
+ if i not in skipped_ssccv
2845
+ ]
2846
+ if remaining:
2847
+ skipped_ssccv.add(remaining[0])
2848
+ print(
2849
+ " Skip SSCCV "
2850
+ "(cognitive/communicative VP)"
2851
+ )
2852
+ continue
2853
+
2854
+ mode = "ssccv"
2855
+ xml_tag = "M:Y_SSCCV"
2856
+ simplifier = SSCCVsimplify
2857
+ span_fn = tag_SSCCV_span
2858
+ break
2859
 
2860
+ if mode is None and has_ssmn:
2861
+ mode = "ssmn"
2862
+ xml_tag = "M:Y_SSMN"
2863
+ simplifier = SSMNsimplify
2864
+ span_fn = tag_SSMN_span
2865
+ elif mode is None and has_ccv:
2866
+ mode = "ccv"
2867
+ xml_tag = "M:Y_CCV"
2868
+ simplifier = CompoundSimplify
2869
+ span_fn = tag_CCV_span
2870
+ elif mode is None and has_cmv1:
2871
+ mode = "cmv1"
2872
+ xml_tag = "M:Y_CMV1"
2873
+ simplifier = CompoundSimplify
2874
+ span_fn = tag_CMV1_span
2875
+ elif mode is None and has_cmn1:
2876
+ mode = "cmn1"
2877
+ xml_tag = "M:Y_CMN1"
2878
+ simplifier = CompoundSimplify
2879
+ span_fn = tag_CMN1_span
2880
+ elif mode is None and has_cln:
2881
+ mode = "cln"
2882
+ xml_tag = "M:Y_CLN"
2883
+ simplifier = CompoundSimplify
2884
+ span_fn = tag_CLN_span
2885
+ elif mode is None:
2886
+ for s in expand_paren_lists(current, lists):
2887
+ sentence_final.append(clean_output(s))
2888
+ continue
2889
 
2890
+ if mode != "ssccv":
 
 
2891
  bracketed_version = build_single_sign_bracketed(
2892
+ tagged_list, mode=mode
 
 
2893
  )
2894
  if not bracketed_version:
2895
+ for s in expand_paren_lists(current, lists):
2896
+ sentence_final.append(clean_output(s))
2897
+ continue
2898
  print(f"\nProcessing: {current}")
2899
  print(
2900
+ f"Bracketed version ({mode}): {bracketed_version}"
2901
  )
 
2902
  cleaned = clean_for_spaCy(bracketed_version)
2903
+ tokens, pos, lemmas, preds = span_fn(cleaned)
2904
  tokens, pos, lemmas, preds = drop_bracket_tokens(
2905
  tokens, pos, lemmas, preds
2906
  )
2907
 
2908
+ cleaned_tokens, clean_pos, clean_lemmas, cleaned_preds = (
2909
+ sign_cleaning(tokens, pos, lemmas, preds)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2910
  )
2911
+ improved_preds = improve_predicted_tag_sequence(
2912
+ cleaned_preds
 
 
2913
  )
2914
 
2915
+ xml_sent = create_xml_sent(
2916
+ cleaned_tokens,
2917
+ clean_pos,
2918
+ clean_lemmas,
2919
+ improved_preds,
2920
+ xml_tag,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2921
  )
2922
+ simps = simplifier(xml_sent)
2923
+ if simps:
2924
+ simps = [fix_subject_verb_agreement(s) for s in simps]
 
2925
 
2926
+ if (
2927
+ simps
2928
+ and simplifier is CompoundSimplify
2929
+ and xml_tag
2930
+ in ("M:Y_CCV", "M:Y_CMV1", "M:Y_CMN1", "M:Y_CLN")
2931
+ ):
2932
+ coord = extract_coordinator_from_xml(xml_sent)
2933
+ if coord:
2934
+ rewritten = []
2935
+ for s in simps:
2936
+ s2 = rewrite_residual_list_comma(s, coord)
2937
+ if s2 != s:
2938
+ print(
2939
+ f" Residual list rewrite ({coord}): "
2940
+ f"{s!r} → {s2!r}"
2941
+ )
2942
+ rewritten.append(s2)
2943
+ simps = rewritten
2944
+
2945
+ if simps and xml_tag == "M:Y_CMN1":
2946
+ simps = [fix_subject_verb_agreement(s) for s in simps]
2947
+
2948
+ if not simps:
2949
  print(
2950
+ "Simplifier returned []; forcing final (no re-queue)"
 
2951
  )
2952
+ current = fix_subject_verb_agreement(current)
2953
+ for s in expand_paren_lists(current, lists):
2954
+ sentence_final.append(clean_output(s))
2955
  continue
2956
+
2957
+ queued_any = False
2958
+ for s in simps:
2959
+ plain_s = clean_output(s)
2960
+ plain_s = fix_subject_verb_agreement(plain_s)
2961
+ if not plain_s:
2962
+ continue
2963
+ plain_key = re.sub(r"\s+", " ", plain_s.lower())
2964
+ if plain_key == key:
2965
+ print(
2966
+ f" No progress (same as input): "
2967
+ f"{plain_s[:80]}..."
2968
+ )
2969
+ for exp in expand_paren_lists(plain_s, lists):
2970
+ sentence_final.append(clean_output(exp))
2971
+ continue
2972
+ if plain_key in seen:
2973
+ print(
2974
+ f" Skip already-seen result: "
2975
+ f"{plain_s[:80]}..."
2976
+ )
2977
+ for exp in expand_paren_lists(plain_s, lists):
2978
+ sentence_final.append(clean_output(exp))
2979
+ continue
2980
+ working_set.append((plain_s, lists))
2981
+ queued_any = True
2982
+ print(f" → Queued: {plain_s}")
2983
+
2984
+ if not queued_any:
2985
+ for s in expand_paren_lists(current, lists):
2986
+ sentence_final.append(clean_output(s))
2987
+
2988
+ for s in order_sentences_for_discourse(sentence_final):
2989
+ s = clean_output(s)
2990
+ if s:
2991
+ para_simplified.append(s)
2992
 
2993
  final_output = "\n\n".join(output_paragraphs)
2994
  print(f"\n=== FINAL OUTPUT ===\n{final_output}")