Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
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 |
-
|
| 369 |
-
|
| 370 |
-
|
|
|
|
|
|
|
| 371 |
"""
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
###############################################################################
|