-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtauto.py
89 lines (64 loc) · 2.11 KB
/
tauto.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def nums():
num = 0
while True:
yield num
num += 1
num_generator = nums()
referents = set()
bottom = {}
class Statement:
def __init__(self, d, s, r):
self.domain = d
self.sign = s
self.referent = r
def __repr__(self):
return "IN {}: {} = {}".format(self.domain, self.sign, self.referent)
def new_r():
n = next(num_generator)
referents.add(n)
return n
class Host:
def __init__(self, name):
self.name = name
self.d_idx = {}
self.s_idx = {}
self.r_idx = {}
def statements(self, *args):
found_referents = {}
for phrase in args:
statement = self.entry(phrase[0], phrase[1])
if statement:
found_referents[phrase[0]] = statement.referent
if len(found_referents) > 1:
raise ValueError("Inconsistent assertions: {}".format(found_referents))
elif found_referents:
referent = list(found_referents.values())[0]
else:
referent = new_r()
results = set()
for phrase in args:
results.add(self.propose(phrase[0], phrase[1], referent))
def propose(self, d, s, referent=None):
statement = self.entry(d, s)
if not statement:
if referent is None:
referent = new_r()
statement = Statement(d, s, referent)
self.d_idx.setdefault(d, {})[s] = statement
self.s_idx.setdefault(s, {})[d] = statement
self.r_idx.setdefault(referent, set()).add(statement)
return statement
def bootstrap(self, name, referent=None):
if referent is None:
referent = new_r()
bottom[self.name] = referent
return self.propose(name, name, referent=referent)
def __getitem__(self, value):
return self.s_idx[value]
def entry(self, domain, sign):
return self.d_idx.get(domain, {}).get(sign)
def synonyms(self, domain, sign):
entry = self.entry(domain, sign)
return self.r_idx[entry.referent]
english = Host("ENG")
english.bootstrap("english")