RJ3vans commited on
Commit
892e6e9
·
verified ·
1 Parent(s): 489c5e0

Update app.py

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