1
|
1 |
|
import pickle |
2
|
1 |
|
import json |
3
|
1 |
|
import os |
4
|
|
|
|
5
|
1 |
|
class TextStream: |
6
|
|
|
""" |
7
|
|
|
A class to load and save files containing the nounification maps stored in txt format |
8
|
|
|
""" |
9
|
|
|
|
10
|
1 |
|
def __init__(self, directSymbol = ' -> ', inverseSymbol = ' <- ', separator = ', '): |
11
|
1 |
|
self.directSymbol = directSymbol |
12
|
1 |
|
self.inverseSymbol = inverseSymbol |
13
|
1 |
|
self.separator = separator |
14
|
|
|
|
15
|
1 |
|
def load(self, f): |
16
|
|
|
""" |
17
|
|
|
Load the content of f |
18
|
|
|
""" |
19
|
1 |
|
verbToNounsDirect = {} |
20
|
1 |
|
verbToNounsInverse = {} |
21
|
1 |
|
for line in f: |
22
|
1 |
|
line = line.replace('\n', '') |
23
|
1 |
|
if self.directSymbol in line: |
24
|
1 |
|
assert self.inverseSymbol not in line |
25
|
1 |
|
[key, nounList] = line.split(self.directSymbol, 1) |
26
|
1 |
|
target = verbToNounsDirect |
27
|
1 |
|
elif self.inverseSymbol in line: |
28
|
1 |
|
assert self.directSymbol not in line |
29
|
1 |
|
[key, nounList] = line.split(self.inverseSymbol, 1) |
30
|
1 |
|
target = verbToNounsInverse |
31
|
|
|
else: |
32
|
|
|
raise Exception("[nounDB] incorrect file format.") |
33
|
1 |
|
for noun in nounList.split(self.separator): |
34
|
1 |
|
if not key in target: |
35
|
1 |
|
target[key] = [noun] |
36
|
1 |
|
elif not noun in target[key]: |
37
|
1 |
|
target[key].append(noun) |
38
|
1 |
|
return [verbToNounsDirect, verbToNounsInverse] |
39
|
|
|
|
40
|
1 |
|
def dump(self, data, f): |
41
|
|
|
""" |
42
|
|
|
Save data into f |
43
|
|
|
""" |
44
|
1 |
|
[verbToNounsDirect, verbToNounsInverse] = data |
45
|
1 |
|
l = sorted([(x, 0) for x in verbToNounsDirect.keys()] + [(x, 1) for x in verbToNounsInverse.keys()]) |
46
|
1 |
|
for couple in l: |
47
|
1 |
|
if couple[1] == 0: |
48
|
1 |
|
f.write('%s%s%s\n' % (couple[0], self.directSymbol, self.separator.join(verbToNounsDirect[couple[0]]))) |
49
|
1 |
|
if couple[1] == 1: |
50
|
1 |
|
f.write('%s%s%s\n' % (couple[0], self.inverseSymbol, self.separator.join(verbToNounsInverse[couple[0]]))) |
51
|
|
|
|
52
|
1 |
|
class Nounificator: |
53
|
|
|
""" |
54
|
|
|
A class to handle the correspondances from the verbs to the nouns. |
55
|
|
|
""" |
56
|
1 |
|
pickleExtension = {'pickle', 'pkl', 'p'} |
57
|
1 |
|
jsonExtension = {'json'} |
58
|
1 |
|
txtExtension = {'txt'} |
59
|
1 |
|
def __init__(self): |
60
|
1 |
|
self.verbToNounsDirect = {} |
61
|
1 |
|
self.verbToNounsInverse = {} |
62
|
|
|
|
63
|
1 |
|
def select(self, x): |
64
|
1 |
|
if x[1] == 0: |
65
|
1 |
|
return ('%s:\t->%s' % (x[0], self.verbToNounsDirect[x[0]])) |
66
|
|
|
else: |
67
|
1 |
|
return ('%s:\t<-%s' % (x[0], self.verbToNounsInverse[x[0]])) |
68
|
|
|
|
69
|
1 |
|
def __str__(self): |
70
|
1 |
|
l = sorted([(x, 0) for x in self.verbToNounsDirect.keys()] + [(x, 1) for x in self.verbToNounsInverse.keys()]) |
71
|
1 |
|
return '\n'.join([self.select(x) for x in l]) |
72
|
|
|
|
73
|
1 |
|
def __eq__(self, other): |
74
|
1 |
|
return self.__dict__ == other.__dict__ |
75
|
|
|
|
76
|
1 |
|
def load(self, fileName): |
77
|
|
|
""" |
78
|
|
|
Load the database from the file of given name (pickle or json format). |
79
|
|
|
""" |
80
|
1 |
|
fileExtension = os.path.splitext(fileName)[1][1:] |
81
|
1 |
|
if fileExtension in self.pickleExtension: |
82
|
1 |
|
f = open(fileName, 'rb') |
83
|
1 |
|
module = pickle |
84
|
1 |
|
elif fileExtension in self.jsonExtension: |
85
|
1 |
|
f = open(fileName, 'r') |
86
|
1 |
|
module = json |
87
|
1 |
|
elif fileExtension in self.txtExtension: |
88
|
1 |
|
f = open(fileName, 'r') |
89
|
1 |
|
module = TextStream() |
90
|
1 |
|
[self.verbToNounsDirect, self.verbToNounsInverse] = module.load(f) |
91
|
1 |
|
f.close() |
92
|
|
|
|
93
|
1 |
|
def save(self, fileName): |
94
|
|
|
""" |
95
|
|
|
Save the database into the file of given name (pickle format). |
96
|
|
|
""" |
97
|
1 |
|
fileExtension = os.path.splitext(fileName)[1][1:] |
98
|
1 |
|
if fileExtension in self.pickleExtension: |
99
|
1 |
|
f = open(fileName, 'wb') |
100
|
1 |
|
pickle.dump([self.verbToNounsDirect, self.verbToNounsInverse], f) |
101
|
1 |
|
elif fileExtension in self.jsonExtension: |
102
|
1 |
|
f = open(fileName, 'w') |
103
|
1 |
|
json.dump([self.verbToNounsDirect, self.verbToNounsInverse], f, indent=4, sort_keys=True) |
104
|
1 |
|
elif fileExtension in self.txtExtension: |
105
|
1 |
|
f = open(fileName, 'w') |
106
|
1 |
|
TextStream().dump([self.verbToNounsDirect, self.verbToNounsInverse], f) |
107
|
1 |
|
f.close() |
108
|
|
|
|
109
|
1 |
|
def _add(self, verb, noun, target): |
110
|
1 |
|
try: |
111
|
1 |
|
if not noun in target[verb]: |
112
|
1 |
|
target[verb].append(noun) |
113
|
1 |
|
except: |
114
|
1 |
|
target[verb] = [noun] |
115
|
|
|
|
116
|
1 |
|
def addDirect(self, verb, noun): |
117
|
|
|
""" |
118
|
|
|
Add the given noun to the direct nounifications of the given verb. |
119
|
|
|
""" |
120
|
1 |
|
self._add(verb, noun, self.verbToNounsDirect) |
121
|
|
|
|
122
|
1 |
|
def addInverse(self, verb, noun): |
123
|
|
|
""" |
124
|
|
|
Add the given noun to the inverse nounifications of the given verb. |
125
|
|
|
""" |
126
|
1 |
|
self._add(verb, noun, self.verbToNounsInverse) |
127
|
|
|
|
128
|
1 |
|
def addListDirect(self, verb, nounList): |
129
|
|
|
""" |
130
|
|
|
Add the given list of nouns to the direct nounifications of the given verb. |
131
|
|
|
""" |
132
|
1 |
|
for noun in nounList: |
133
|
1 |
|
self.addDirect(verb, noun) |
134
|
|
|
|
135
|
1 |
|
def addListInverse(self, verb, nounList): |
136
|
|
|
""" |
137
|
|
|
Add the given list of nouns to the inverse nounifications of the given verb. |
138
|
|
|
""" |
139
|
1 |
|
for noun in nounList: |
140
|
1 |
|
self.addInverse(verb, noun) |
141
|
|
|
|
142
|
1 |
|
def _remove(self, verb, noun, target): |
143
|
1 |
|
target[verb].remove(noun) |
144
|
1 |
|
if target[verb] == []: |
145
|
1 |
|
target.pop(verb) |
146
|
|
|
|
147
|
1 |
|
def removeDirect(self, verb, noun): |
148
|
|
|
""" |
149
|
|
|
Remove the given noun from the direct nounifications of the given verb. |
150
|
|
|
""" |
151
|
1 |
|
self._remove(verb, noun, self.verbToNounsDirect) |
152
|
|
|
|
153
|
1 |
|
def removeInverse(self, verb, noun): |
154
|
|
|
""" |
155
|
|
|
Remove the given noun from the inverse nounifications of the given verb. |
156
|
|
|
""" |
157
|
1 |
|
self._remove(verb, noun, self.verbToNounsInverse) |
158
|
|
|
|
159
|
1 |
|
def _removeVerb(self, verb, target): |
160
|
1 |
|
target.pop(verb) |
161
|
|
|
|
162
|
1 |
|
def removeVerbDirect(self, verb): |
163
|
|
|
""" |
164
|
|
|
Remove all the direct nounifications of the given verb. |
165
|
|
|
""" |
166
|
1 |
|
self._removeVerb(verb, self.verbToNounsDirect) |
167
|
|
|
|
168
|
1 |
|
def removeVerbInverse(self, verb): |
169
|
|
|
""" |
170
|
|
|
Remove all the inverse nounifications of the given verb. |
171
|
|
|
""" |
172
|
1 |
|
self._removeVerb(verb, self.verbToNounsInverse) |
173
|
|
|
|
174
|
1 |
|
def _toNouns(self, verb, target): |
175
|
1 |
|
return target.get(verb, []) |
176
|
|
|
|
177
|
1 |
|
def directNouns(self, verb): |
178
|
|
|
""" |
179
|
|
|
Return the list of direct nounifications of the given noun. |
180
|
|
|
""" |
181
|
1 |
|
return self._toNouns(verb, self.verbToNounsDirect) |
182
|
|
|
|
183
|
1 |
|
def inverseNouns(self, verb): |
184
|
|
|
""" |
185
|
|
|
Return the list of inverse nounifications of the given noun. |
186
|
|
|
""" |
187
|
1 |
|
return self._toNouns(verb, self.verbToNounsInverse) |
188
|
|
|
|
189
|
1 |
|
def exists(self, verb): |
190
|
|
|
""" |
191
|
|
|
Return True if and only if there exists (direct or inverse) nounification(s) of the given verb. |
192
|
|
|
""" |
193
|
1 |
|
return verb in self.verbToNounsDirect or verb in self.verbToNounsInverse |
194
|
|
|
|
195
|
1 |
|
def merge(self, other): |
196
|
|
|
""" |
197
|
|
|
Merge with the given nounificator. |
198
|
|
|
""" |
199
|
1 |
|
for key, value in other.verbToNounsDirect.items(): |
200
|
1 |
|
self.addListDirect(key, value) |
201
|
1 |
|
for key, value in other.verbToNounsInverse.items(): |
202
|
|
|
self.addListInverse(key, value) |
203
|
|
|
|