-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparaphrase.py
More file actions
59 lines (48 loc) · 1.93 KB
/
paraphrase.py
File metadata and controls
59 lines (48 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import List, Tuple, Set
import random
def _predicate_to_phrases(predicate: str) -> List[str]:
mapping = {
'NEXT_TO': ['is next to', 'is adjacent to', 'sits beside'],
'NEAR': ['is near', 'is close to', 'lies near'],
'RELATED': ['is related to', 'is associated with', 'connects to'],
'DISTANT': ['is distant from', 'is far from', 'is not closely related to']
}
return mapping.get(predicate.upper(), [predicate.lower()])
def generate_variants(
facts: List[Tuple[str, str, str, int, dict]],
synsets=None,
n_variants: int = 3,
include_inverse: bool = True
) -> List[str]:
"""Generate semantically varied sentence variants from inferred facts.
facts: list of (subject, predicate, object, position, modifiers)
synsets: Synsets instance (optional) for synonym substitution
Returns list of sentence strings.
"""
variants = []
if not facts:
return variants
# Use up to first 3 facts to build variants
facts = facts[:3]
for _ in range(n_variants):
parts = []
for subj, pred, obj, pos, mods in facts:
# Choose phrase for predicate
phr = random.choice(_predicate_to_phrases(pred))
# Substitute synonyms if synsets provided
s = subj
o = obj
if synsets is not None:
subs = list(synsets.expand(s))
obs = list(synsets.expand(o))
if subs:
s = random.choice(subs)
if obs:
o = random.choice(obs)
# Randomly inverse occasionally if include_inverse
if include_inverse and random.random() < 0.3:
parts.append(f"{o.capitalize()} {phr} {s}.")
else:
parts.append(f"{s.capitalize()} {phr} {o}.")
variants.append(' '.join(parts))
return variants