RJ3vans commited on
Commit
d8d97cb
·
verified ·
1 Parent(s): fd8143a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -14
app.py CHANGED
@@ -61,6 +61,12 @@ import gradio as gr
61
  import os
62
 
63
  from collections import deque # add this at the top of app.py if not already there
 
 
 
 
 
 
64
 
65
  seen = set()
66
 
@@ -363,23 +369,71 @@ def sentence_has_anaphoric_pronoun(sent: str) -> bool:
363
  return any(t in _ANAPHORIC_PRONOUNS for t in tokens)
364
 
365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
  def order_sentences_for_discourse(sents):
367
  """
368
- Print sentences with anaphoric pronouns last so pronouns
369
- are less likely to precede their antecedents.
370
- Stable within each group (original relative order kept).
 
 
371
  """
372
- without = []
373
- with_pron = []
374
- for s in sents:
375
- s = (s or "").strip()
376
- if not s:
377
- continue
378
- if sentence_has_anaphoric_pronoun(s):
379
- with_pron.append(s)
380
- else:
381
- without.append(s)
382
- return without + with_pron
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
  ###############################################################################
384
 
385
  ###############################################################################
 
61
  import os
62
 
63
  from collections import deque # add this at the top of app.py if not already there
64
+ from collections import defaultdict
65
+
66
+ PRONOUNS = {
67
+ "he", "she", "it", "they", "him", "her", "them",
68
+ "his", "hers", "its", "their", "theirs",
69
+ }
70
 
71
  seen = set()
72
 
 
369
  return any(t in _ANAPHORIC_PRONOUNS for t in tokens)
370
 
371
 
372
+ def sentence_entity_key(sent: str) -> str:
373
+ """Primary entity ≈ subject head, lowercased lemma/text."""
374
+ doc = nlp(sent)
375
+ subj = next((t for t in doc if t.dep_ in ("nsubj", "nsubjpass")), None)
376
+ if subj is None:
377
+ subj = next((t for t in doc if t.pos_ in ("PROPN", "NOUN", "PRON")), None)
378
+ if subj is None:
379
+ return sent.lower()[:40]
380
+ # Use lemma for "man"/"men"; text for names
381
+ if subj.pos_ == "PROPN":
382
+ return subj.text.lower()
383
+ return subj.lemma_.lower()
384
+
385
+ def is_pronoun_sentence(sent: str) -> bool:
386
+ doc = nlp(sent)
387
+ for t in doc:
388
+ if t.dep_ in ("nsubj", "nsubjpass") and t.lower_ in PRONOUNS:
389
+ return True
390
+ return bool(re.search(r"\b(he|she|it|they|him|her|them)\b", sent, re.I))
391
+
392
+ def is_identifying(sent: str) -> bool:
393
+ """Property / location / copula — good 'given' material."""
394
+ return bool(
395
+ re.search(
396
+ r"\b(is|are|was|were|lives?|lived|lives in|is a|is an)\b",
397
+ sent,
398
+ re.I,
399
+ )
400
+ )
401
+
402
  def order_sentences_for_discourse(sents):
403
  """
404
+ 1. Pronoun sentences last (antecedent first).
405
+ 2. Group remaining sentences by subject entity.
406
+ 3. Within a group: identifying sentences before event sentences.
407
+ 4. Group order = first appearance in the current list
408
+ (≈ order derived from the source sentence).
409
  """
410
+ cleaned = [s.strip() for s in sents if s and s.strip()]
411
+ if len(cleaned) <= 1:
412
+ return cleaned
413
+
414
+ pronoun_sents = []
415
+ lexical_sents = []
416
+ for s in cleaned:
417
+ (pronoun_sents if is_pronoun_sentence(s) else lexical_sents).append(s)
418
+
419
+ groups = defaultdict(list)
420
+ order = []
421
+ for s in lexical_sents:
422
+ key = sentence_entity_key(s)
423
+ if key not in groups:
424
+ order.append(key)
425
+ groups[key].append(s)
426
+
427
+ ordered = []
428
+ for key in order:
429
+ chunk = groups[key]
430
+ ident = [s for s in chunk if is_identifying(s)]
431
+ rest = [s for s in chunk if s not in ident]
432
+ ordered.extend(ident + rest)
433
+
434
+ ordered.extend(pronoun_sents)
435
+ return ordered
436
+
437
  ###############################################################################
438
 
439
  ###############################################################################