-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterns.py
339 lines (297 loc) · 10.6 KB
/
patterns.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from copy import deepcopy
class Pattern:
def __init__(self, text):
self.text = text
self.ast = parse(text)
self.extended = build_possibilities(self.ast, [[]])
self.possibilities = len(self.extended)
self.min_length = None
self.max_length = 0
for pc in self.extended:
if len(pc) > self.max_length:
self.max_length = len(pc)
if self.min_length is None or len(pc) < self.min_length:
self.min_length = len(pc)
def extended_by_length(self):
res = []
nb = 0
for e in self.extended:
res.append([nb, len(e), *e])
nb += 1
return res
def to_xml(self, name='pattern_xml.txt'):
f = open(name, mode='w', encoding='utf-8')
f.write(str(suite))
f.close()
def to_txt(self, name='pattern_extended.txt'):
f = open(name, mode='w', encoding='utf-8')
for line in pattern_compiled:
f.write(str(line) + '\n')
f.close()
def find_x(self, corpus, nb):
"""Warning : this method find the pattern anywhere in th title"""
data = []
for key, title in corpus.titles.items():
words = title.words
for ex in self.extended:
i = 0
start = None
matched = None
while i < min(len(ex), len(words)) and ex[i] != words[i].pos:
i += 1
if i < min(len(ex), len(words)):
start = i
i += 1
ln = 1
while ln < len(ex) and i < min(len(ex), len(words)):
if ex[i] == words[i].pos:
ln += 1
else:
break
if ln == len(ex):
data.append(title)
break
if len(data) > nb:
break
return data
def find_one(self, corpus):
""" Try to find in data one title corresponding to at least one extended form of the pattern.
Pattern is searched EVERYWHERE in the title.
data must be a CORPUS.
Warning:
- contrary to the rest of the lib, this function needs a Corpus, not a list of POS.
- contrary to the rest of the lib, this function searches anywhere in the title."""
for key, title in corpus.titles.items():
words = title.words
for ex in self.extended:
i = 0
start = None
matched = None
while i < min(len(ex), len(words)) and ex[i] != words[i].pos:
i += 1
if i < min(len(ex), len(words)):
start = i
i += 1
ln = 1
while ln < len(ex) and i < min(len(ex), len(words)):
if ex[i] == words[i].pos:
ln += 1
else:
break
if ln == len(ex):
return title, start, ex
return None, None, None
def trilist(self, words, after=None):
"Transform a list of words to 3 lists for forms, lemma and pos."
forms = []
lemma = []
pos = []
if after is None:
for w in words:
forms.append(w.form)
lemma.append(w.lemma)
pos.append(w.pos)
else:
started = False
for w in words:
if started:
forms.append(w.form)
lemma.append(w.lemma)
pos.append(w.pos)
if w.form == after:
started = True
return forms, lemma, pos
#def find_all_with(self, corpus, n1, n2):
# for key, title in corpus.titles.items():
# forms, lemma, pos = self.trilist(title.words, after=':')
def match_one(self, val):
"""Take a list of pos as val"""
if len(val) < self.min_length:
return None
selected = self.extended
for i in range(len(val)):
filtered = []
for j in range(len(selected)):
if i >= len(selected[j]) or val[i] == selected[j][i]:
if len(val) >= len(selected[j]): # don't match start of extended form not complete!
filtered.append(selected[j])
selected = filtered
if len(selected) == 0:
break
if len(selected) > 0:
max_length = 0
final = None
for pc in selected:
if len(pc) > max_length:
max_length = len(pc)
final = pc
return final
else:
return None
def match(self, data, info=True):
matched = []
unmatched = []
itercount = 0
iterdisplay = 1000
iterstep = 1000
for key, val in data.items():
itercount += 1
if itercount == iterdisplay:
print(itercount, 'elements done.')
iterdisplay += iterstep
if len(val) < self.min_length:
unmatched.append(key)
continue
selected = self.extended
for i in range(len(val)):
filtered = []
for j in range(len(selected)):
if i >= len(selected[j]) or val[i] == selected[j][i]:
if len(val) >= len(selected[j]): # don't match start of extended form not complete!
filtered.append(selected[j])
selected = filtered
if len(selected) == 0:
break
if len(selected) > 0:
perfect = False
for pc in selected:
if len(pc) == len(val):
perfect = True
break
matched.append(key)
else:
unmatched.append(key)
return matched, unmatched
def __str__(self):
return self.text
def __repr__(self):
return "Pattern : " + self.text
class Node:
def __init__(self):
self.opt = False
def __str__(self):
return f'<Node opt={self.opt}/>'
def display(self, level=0):
return ' ' * level + str(self)
class Identifier(Node):
def __init__(self, val):
Node.__init__(self)
self.val = val
def __str__(self):
return f'<Identifier val={self.val} opt={self.opt}/>'
class Suite(Node):
def __init__(self):
Node.__init__(self)
self.nodes = []
self.name = 'Suite'
self.mod = 'AND'
def append(self, node : Node):
if not isinstance(node, Node):
raise Exception('Not a Node: ' + str(node))
self.nodes.append(node)
def last(self):
return self.nodes[-1]
def display(self, level=0):
s = ' ' * level + f'<{self.name} opt={self.opt}\n'
cpt = 0
for n in self.nodes:
x = self.mod if cpt < len(self.nodes) - 1 else ''
s += n.display(level + 1) + f' {x}\n'
cpt += 1
s += ' ' * level + '/>'
return s
def __str__(self):
return self.display()
def __getitem__(self, i):
return self.nodes[i]
class Choice(Suite):
def __init__(self):
Suite.__init__(self)
self.name = 'Choice'
self.mod = 'OR'
def parse(pattern, choice=False):
#print('Parsing:', pattern, 'choice=', choice)
suite = Suite() if not choice else Choice()
w = ''
in_word = False
i = 0
def find_closing(pattern, form, i):
level = 1
last = None
closing = {'(' : ')', '[' : ']'}
closing = closing[form]
for i2 in range(i + 1, len(pattern)):
c2 = pattern[i2]
if c2 == form:
level += 1
elif c2 == closing:
level -= 1
if level == 0:
last = i2
break
if last is None:
raise Exception("Not closing ) found!")
return last
while i < len(pattern):
c = pattern[i]
# identifier
if c.isalpha() or c == '+':
in_word = True
elif in_word:
in_word = False
suite.append(Identifier(w))
w = ''
if in_word:
w += c
# option
if c == '?':
suite.last().opt = True
elif c == '[':
last = find_closing(pattern, '[', i)
suite.append(parse(pattern[i+1 : last], True))
i = last
elif c == '(':
last = find_closing(pattern, '(', i)
suite.append(parse(pattern[i+1 : last]))
i = last
i += 1
if in_word:
suite.append(Identifier(w))
return suite
def build_possibilities(elem, possibilities):
if type(elem) == Identifier:
new_possibilities = deepcopy(possibilities)
for np in new_possibilities:
np.append(elem.val)
if not elem.opt:
possibilities = new_possibilities
else:
possibilities += new_possibilities
elif type(elem) == Suite:
new_possibilities = deepcopy(possibilities)
for e2 in elem:
new_possibilities = build_possibilities(e2, new_possibilities)
if not elem.opt:
possibilities = new_possibilities
else:
possibilities += new_possibilities
elif type(elem) == Choice:
new_possibilities = []
for e2 in elem:
new_possibilities.append(deepcopy(possibilities))
new_possibilities[-1] = build_possibilities(e2, new_possibilities[-1])
if not elem.opt: # if not optional, there will be only our choices available, not the original ones that we must delete
possibilities = []
for n in new_possibilities:
possibilities += n
else:
raise Exception("Type not known: " + str(type(elem)))
return possibilities
if __name__ == '__main__':
from titles import Word
w = [Word('tests', 'test', 'DET'), Word(':', ':', 'PONCT'), Word('la', 'la', 'DET'), Word('maison', 'maison', 'NC'), Word('de', 'de', 'P'), Word('la', 'la', 'DET'), Word('forêt', 'forêt', 'NC')]
pattern = Pattern('DET? ADJ? [NC NPP] [NC NPP]? ADJ? [(P DET?) P+D] ADJ? [NC NPP] [NC NPP]? ADJ?')
forms, lemma, pos = pattern.trilist(w, after=':')
print(forms)
print(lemma)
print(pos)