-
Notifications
You must be signed in to change notification settings - Fork 20
/
colibricore_indexedpatternmodel.pxi
357 lines (315 loc) · 16.4 KB
/
colibricore_indexedpatternmodel.pxi
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
cpdef getdata(self, Pattern pattern):
"""Return the indices at which the pattern occurs"""
cdef cIndexedData cvalue
if pattern in self:
cvalue = self.data[pattern.cpattern]
value = IndexedData()
value.bind(cvalue)
return value
else:
raise KeyError
cpdef add(self, Pattern pattern, indices):
"""Add a pattern to the indexed model
:param pattern: The pattern to add
:type pattern: Pattern
:param indices: list (or other iterable) of 2-tuples specifying the (sentence,index) of each occurrence of the pattern
"""
cdef cIndexedData * cvalue
cvalue = &(self.data[pattern.cpattern])
cdef cIndexReference ref
for sentence,token in indices:
ref.sentence = <int> sentence
ref.token = <int> token
self.data.add(pattern.cpattern, cvalue, ref)
def __iter__(self):
"""Iterate over all patterns in this model"""
it = self.data.begin()
cdef cPattern cpattern
while it != self.data.end():
cpattern = deref(it).first
pattern = Pattern()
pattern.bind(cpattern)
yield pattern
inc(it)
def items(self):
"""Iterate over all patterns and their index data (IndexedData instances) in this model"""
it = self.data.begin()
cdef cPattern cpattern
cdef cIndexedData cvalue
while it != self.data.end():
cpattern = deref(it).first
cvalue = deref(it).second
pattern = Pattern()
pattern.bind(cpattern)
value = IndexedData()
value.bind(cvalue)
yield (pattern,value)
inc(it)
def covered(self, indexreference):
if not isinstance(indexreference, tuple) or not len(indexreference) == 2:
raise ValueError("Expected tuple")
cdef unsigned int sentence = indexreference[0]
cdef unsigned int token = indexreference[1]
cdef cIndexReference ref = cIndexReference(sentence, token)
cdef cIndexReference ref2
cdef unordered_set[cPatternPointer] results = self.data.getreverseindex(ref)
cdef unordered_set[cPatternPointer] results2
cdef unordered_set[cPatternPointer].iterator resit
cdef cPattern cpattern
if not results.empty():
return True
else:
for i in range(1, token+1):
if token-i >= 0:
ref2 = cIndexReference(sentence, token-i)
results2 = self.data.getreverseindex(ref2)
resit = results2.begin()
while resit != results2.end():
cpattern = deref(resit).pattern()
if cpattern.n() >= token+1:
return True
inc(resit)
return False
cpdef outputrelations(self, Pattern pattern, ClassDecoder decoder):
"""Compute and output (to stdout) all relations for the specified pattern:
:param pattern: The pattern to output relations for
:type pattern: Pattern
:param decoder: The class decoder
:type decoder: ClassDecoder
"""
self.data.outputrelations(pattern.cpattern,decoder.data,&cout)
def getsubchildren(self, Pattern pattern, int occurrencethreshold = 0, int category = 0, int size = 0):
"""Get subsumption children for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:param category: Constrain by patterns of the specified category (colibricore.Category.NGRAM,colibricore.Category.SKIPGRAM, colibricore.Category.FLEXGRAM)
:param size: Constrain by patterns of the specified size
:type size: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrences for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getsubchildren(pattern.cpattern, occurrencethreshold, category, size)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def getsubparents(self, Pattern pattern, int occurrencethreshold = 0, int category = 0, int size = 0):
"""Get subsumption parents for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:param category: Constrain by patterns of the specified category (colibricore.Category.NGRAM,colibricore.Category.SKIPGRAM, colibricore.Category.FLEXGRAM)
:param size: Constrain by patterns of the specified size
:type size: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrences for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getsubparents(pattern.cpattern, occurrencethreshold, category, size)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def getleftneighbours(self, Pattern pattern, int occurrencethreshold = 0, int category = 0, int size = 0, int cutoff = 0):
"""Get left neighbours for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:param category: Constrain by patterns of the specified category (colibricore.Category.NGRAM,colibricore.Category.SKIPGRAM, colibricore.Category.FLEXGRAM)
:param size: Constrain by patterns of the specified size
:type size: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrences for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getleftneighbours(pattern.cpattern, occurrencethreshold, category, size,cutoff)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def getrightneighbours(self, Pattern pattern, int occurrencethreshold = 0, int category = 0, int size = 0, int cutoff = 0):
"""Get right neighbours for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:param category: Constrain by patterns of the specified category (colibricore.Category.NGRAM,colibricore.Category.SKIPGRAM, colibricore.Category.FLEXGRAM)
:param size: Constrain by patterns of the specified size
:type size: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrences for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getrightneighbours(pattern.cpattern, occurrencethreshold, category, size,cutoff)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def getskipcontent(self, Pattern pattern):
"""Get skip content for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrence for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getskipcontent(pattern.cpattern)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def gettemplates(self, Pattern pattern, int occurrencethreshold = 0):
"""Get templates (abstracting skipgrams) for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrence for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.gettemplates(pattern.cpattern, occurrencethreshold)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def getinstances(self, Pattern pattern, int occurrencethreshold = 0):
"""Get templates (abstracting skipgrams) for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrence for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getinstances(pattern.cpattern, occurrencethreshold)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def getcooc(self, Pattern pattern, int occurrencethreshold = 0, int category = 0, int size = 0):
"""Get left-side co-occurrences for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:param category: Constrain by patterns of the specified category (colibricore.Category.NGRAM,colibricore.Category.SKIPGRAM, colibricore.Category.FLEXGRAM)
:param size: Constrain by patterns of the specified size
:type size: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrence for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getcooc(pattern.cpattern, occurrencethreshold, category, size)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def getleftcooc(self, Pattern pattern, int occurrencethreshold = 0, int category = 0, int size = 0):
"""Get left-side co-occurrences for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:param category: Constrain by patterns of the specified category (colibricore.Category.NGRAM,colibricore.Category.SKIPGRAM, colibricore.Category.FLEXGRAM)
:param size: Constrain by patterns of the specified size
:type size: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrence for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getleftcooc(pattern.cpattern, occurrencethreshold, category, size)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def getrightcooc(self, Pattern pattern, int occurrencethreshold = 0, int category = 0, int size = 0):
"""Get right-side co-occurrences for the specified pattern
:param pattern: The pattern
:type pattern: Pattern
:param occurrencethreshold: Constrain by patterns occurring at least this many times in this relationship (default: 0, unconstrained)
:type occurrencethreshold: int
:param category: Constrain by patterns of the specified category (colibricore.Category.NGRAM,colibricore.Category.SKIPGRAM, colibricore.Category.FLEXGRAM)
:param size: Constrain by patterns of the specified size
:type size: int
:rtype: generator over (Pattern,value) tuples. The values correspond to the number of occurrence for this particularrelationship
"""
if self.data.reverseindex == NULL:
raise ValueError("No reverseindex was specified but this method requires it, set reverseindex to an IndexedCorpus instance upon model construction")
cdef t_relationmap relations = self.data.getrightcooc(pattern.cpattern, occurrencethreshold, category, size)
cdef t_relationmap_iterator relit = relations.begin()
cdef cPattern cpattern
cdef unsigned int value
while relit != relations.end():
cpattern = deref(relit).first
value = deref(relit).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(relit)
def computeflexgrams_fromskipgrams(self):
"""Compute flexgrams from skipgrams in the model. Returns the number of flexgrams found."""
return self.data.computeflexgrams_fromskipgrams()
def computeflexgrams_fromcooc(self,double threshold):
"""Compute flexgrams based on co-occurence. The threshold is expressed in normalised pointwise mutual information. Returns the number of flexgrams found. Flexgrams contain at only one gap using this method."""
return self.data.computeflexgrams_fromcooc(threshold)