-
Notifications
You must be signed in to change notification settings - Fork 20
/
colibricore_wrapper.in.pyx
1412 lines (1134 loc) · 52.7 KB
/
colibricore_wrapper.in.pyx
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#embedsignature=True
#*****************************
# Colibri Core
# by Maarten van Gompel
# Centre for Language Studies
# Radboud University Nijmegen
#
# http://proycon.github.io/colibri-core
#
# Licensed under GPLv3
#****************************/
from libcpp.string cimport string
from libcpp cimport bool
from libcpp.vector cimport vector
from cython.operator cimport dereference as deref, preincrement as inc
from cython import address
from colibricore_classes cimport ClassEncoder as cClassEncoder, ClassDecoder as cClassDecoder, Pattern as cPattern, PatternPointer as cPatternPointer, IndexedData as cIndexedData, IndexReference as cIndexReference, PatternMap as cPatternMap, HashOrderedPatternMap as cHashOrderedPatternMap, PatternSet as cPatternSet, PatternModelOptions as cPatternModelOptions, PatternSetModel as cPatternSetModel, PatternModel as cPatternModel,IndexedPatternModel as cIndexedPatternModel, IndexedDataHandler as cIndexedDataHandler, BaseValueHandler as cBaseValueHandler, cout, IndexedCorpus as cIndexedCorpus, SKIPPATTERN as cSKIPPATTERN, FLEXPATTERN as cFLEXPATTERN, UNKPATTERN as cUNKPATTERN, BOUNDARYPATTERN as cBOUNDARYPATTERN, AlignedPatternMap as cAlignedPatternMap, PatternModelInterface as cPatternModelInterface, PatternVector as cPatternVector, PatternFeatureVector as cPatternFeatureVector, PatternFeatureVectorMap as cPatternFeatureVectorMap, PatternAlignmentModel as cPatternAlignmentModel, BasicPatternAlignmentModel as cBasicPatternAlignmentModel, patternfromfile as cpatternfromfile, t_relationmap, t_relationmap_double, t_relationmap_iterator, t_relationmap_double_iterator, istream
from unordered_map cimport unordered_map
from unordered_set cimport unordered_set
from libc.stdint cimport *
from libcpp.map cimport map as stdmap
from libcpp.utility cimport pair
import os.path
from collections import Counter
from sys import version
def encode(s):
return s.encode('utf-8')
class Category:
"""Pattern Category"""
NGRAM=1
SKIPGRAM=2
FLEXGRAM=3
cdef class ClassEncoder:
"""The class encoder allows patterns to be built from their string representation. Load in a class file and invoke the ``buildpattern()`` method"""
cdef cClassEncoder data
cdef str _filename
cdef unordered_map[string,unsigned int] freqlist
cdef int minlength
cdef int maxlength
def __init__(self, str filename=None, int minlength=0, int maxlength=0):
self.minlength = minlength
self.maxlength = maxlength
if filename:
self._filename = filename
if os.path.exists(filename):
self.data.load(encode(filename), minlength, maxlength)
else:
raise FileNotFoundError("File " + filename + " does not exist")
else:
self._filename = ""
def filename(self):
return self._filename
def __len__(self):
"""Returns the total number of classes"""
return self.data.size()
def buildpattern(self, str text, bool allowunknown=True, bool autoaddunknown=False):
"""Builds a pattern: converts a string representation into a Pattern
:param text: The actual text of the pattern
:type text: str
:param allowunknown: Encode unknown classes as 'unknown', a single class for all, rather than failing with an exception if a word type is unseen (bool, default=False)
:type allowunknown: bool
:param autoaddunknown: Automatically add unknown classes to the model (bool, default=False)
:type autoaddunknown: bool
:rtype: Pattern
"""
cdef cPattern cpattern = self.data.buildpattern(encode(text), allowunknown, autoaddunknown)
pattern = Pattern()
pattern.bind(cpattern)
return pattern
def processcorpus(self, str filename): #build a class from this dataset
"""Process a corpus, call buildclasses() when finished with all corpora"""
if os.path.exists(filename):
self.data.processcorpus(encode(filename), self.freqlist)
def buildclasses(self):
"""Build classes, call after processing all corpora with processcorpus()"""
self.data.buildclasses(self.freqlist)
def build(self, str filename): #build a class from this dataset
"""Builds a class encoder from a plain-text corpus (utf-8). Equivalent to a call to processcorpus() followed by buildclasses()"""
if os.path.exists(filename):
self.data.build(encode(filename))
else:
raise FileNotFoundError("File " + filename + " does not exist")
def encodefile(self, str sourcefile, str targetfile, bool allowunknown=True, bool addunknown=False, bool append=False, bool ignorenewlines=False): #apply the encoder to a file
"""Encodes the specified sourcefile according to the classer (as targetfile)
:param sourcefile: Source filename
:type sourcefile: str
:param targetfile: Target filename
:type sourcefile: str
:param allowunknown: Encode unknown classes as 'unknown', a single class for all, rather than failing with an exception if a word type is unseen (bool, default=False)
:type allowunknown: bool
:param addunknown: Add unknown classes to the class encoder (bool, default=False)
:type addunknown: bool
:param append: Append to file (bool, default=False)
:type append: bool
"""
if os.path.exists(sourcefile):
self.data.encodefile(encode(sourcefile), encode(targetfile),allowunknown, addunknown, append, ignorenewlines, True)
else:
raise FileNotFoundError("File " + sourcefile + " does not exist")
def save(self, str filename):
if not self.filename:
self.filename = filename
self.data.save(encode(filename))
cdef class ClassDecoder:
"""The Class Decoder allows Patterns to be decoded back to their string representation. An instance of ClassDecoder is passed to Pattern.tostring()"""
cdef cClassDecoder data #it's not actually a pointer anymore..
cdef str _filename
def __init__(self, str filename=None):
if filename:
self._filename = filename
if os.path.exists(filename):
self.data.load(encode(filename))
else:
raise FileNotFoundError("No such file: " + filename)
else:
self._filename = ""
def __len__(self):
"""Returns the total number of classes"""
return self.data.size()
def decodefile(self, str filename):
if os.path.exists(filename):
return self.data.decodefiletostring(encode(filename)).decode('utf-8') #bytes to str (python3)
else:
raise FileNotFoundError("File " + filename + " does not exist")
def filename(self):
return self._filename
def patternfromfile(str filename):
"""Builds a single pattern from corpus data, will ignore any newlines. You may want to use IndexedCorpus instead."""
cdef cPattern cpattern = cpatternfromfile(encode(filename))
pattern = Pattern()
pattern.bind(cpattern)
return pattern
cdef class Pattern:
"""The Pattern class contains an ngram, skipgram or flexgram, and allows a wide variety of actions to be performed on it. It is stored in a memory-efficient fashion and facilitating fast operation and comparison. Use ClassEncoder.buildpattern to build a pattern."""
cdef cPattern cpattern
cdef cPattern getcpattern(self):
return self.cpattern
cdef bind(self, cPattern& cpattern):
self.cpattern = cpattern
def bindunk(self):
self.cpattern = cUNKPATTERN
def bindskip(self):
self.cpattern = cSKIPPATTERN
def bindflex(self):
self.cpattern = cFLEXPATTERN
def bindboundary(self):
self.cpattern = cBOUNDARYPATTERN
def tostring(self, ClassDecoder decoder):
"""Convert a Pattern back to a str
:param decoder: the class decoder to use
:type decoder: ClassDecoder
:rtype: str
"""
return str(self.cpattern.tostring(decoder.data),'utf-8')
def unknown(self):
return self.cpattern.unknown()
def __contains__(self, Pattern pattern):
"""Check if the specified pattern occurs within this larger pattern.
:param pattern: the subpattern to look for
:type pattern: Pattern
:rtype: bool
Example::
subpattern in pattern
"""
cdef bool r
r = self.cpattern.contains(pattern.cpattern)
return r
def __bool__(self):
return self.cpattern.n() > 0
def __len__(self):
"""Returns the length of the pattern in words/tokens::
len(pattern)
"""
return self.cpattern.n()
def __copy__(self):
"""Produces a copy of the pattern (deep)::
pattern2 = copy(pattern)
"""
cdef cPattern c_pattern
c_pattern = cPattern(self.cpattern) #copy constructor
newpattern = Pattern()
newpattern.bind(c_pattern)
return newpattern
def __deepcopy__(self):
"""Produces a copy of the pattern (deep)::
pattern2 = copy(pattern)
"""
cdef cPattern c_pattern
c_pattern = cPattern(self.cpattern) #copy constructor
newpattern = Pattern()
newpattern.bind(c_pattern)
return newpattern
def concat(self, Pattern pattern):
cdef cPattern newcpattern = self.cpattern + pattern.cpattern
newpattern = Pattern()
newpattern.bind(newcpattern)
return newpattern
def __add__(self, Pattern other):
"""Concatenate two patterns together, forming a larger one::
pattern3 = pattern1 + pattern2
"""
return self.concat(other)
def __getitem__(self, item):
"""Retrieves a word/token from the pattern::
unigram = pattern[index]
Or retrieves a subpattern::
subpattern = pattern[begin:end]
:param item: an index or slice
:rtype: a Pattern instance
"""
cdef int start
cdef int stop
cdef cPattern c_pattern
newpattern = Pattern()
if isinstance(item, slice):
start = item.start
stop = item.stop
if not stop:
stop = len(self)
if not start:
start = 0
c_pattern = cPattern(self.cpattern, start, stop - start)
newpattern.bind(c_pattern)
return newpattern
else:
if item < 0:
start = len(self) + item
else:
start = item
c_pattern = cPattern(self.cpattern, start, 1)
newpattern.bind(c_pattern)
return newpattern
def __iter__(self):
"""Iterates over all words/tokens in this pattern"""
for i in range(0, len(self)):
yield self[i]
def bytesize(self):
"""Returns the number of bytes used to encode this pattern in memory"""
return self.cpattern.bytesize()
def skipcount(self):
"""Returns the number of gaps in this pattern"""
return self.cpattern.skipcount()
def category(self):
"""Returns the category of this pattern
:rtype: Category.NGRAM (1), Category.SKIPGRAM (2), or Category.FLEXGRAM (3)
"""
return self.cpattern.category()
def __hash__(self):
"""Returns the hashed value for this pattern"""
return self.cpattern.hash()
def __richcmp__(Pattern self, Pattern other, int op):
"""Allows comparisons of two patterns using the usual operators, <, > , <=, <=, =="""
if op == 2: # ==
return self.cpattern == other.cpattern
elif op == 0: #<
return self.cpattern < other.cpattern
elif op == 4: #>
return self.cpattern > other.cpattern
elif op == 3: #!=
return not( self.cpattern == other.cpattern)
elif op == 1: #<=
return (self.cpattern == other.cpattern) or (self.cpattern < other.cpattern)
elif op == 5: #>=
return (self.cpattern == other.cpattern) or (self.cpattern > other.cpattern)
def reverse(self):
"""Reverse the pattern (all the words will be in reverse order)"""
cdef cPattern newcpattern = self.cpattern.reverse()
newpattern = Pattern()
newpattern.bind(newcpattern)
return newpattern
cdef Pattern add(Pattern self, Pattern other):
cdef cPattern newcpattern = self.cpattern + other.cpattern
newpattern = Pattern()
newpattern.bind(newcpattern)
return newpattern
def ngrams(self,int n=0, int maxn=0 ):
"""Generator iterating over all ngrams of a particular size (or range thereof) that are enclosed within this pattern. Despite the name, this may also return skipgrams!
:param n: The desired size to obtain, if unspecified (0), will extract all ngrams
:type n: int
:param maxn: If larger than n, will extract ngrams in the specified n range
:type maxn: int
:rtype: generator over Pattern instances
"""
if n == 0:
return self.subngrams()
elif maxn >= n:
return self.subngrams(n,maxn)
else:
return self._ngrams_aux(n)
def _ngrams_aux(self,int n):
cdef vector[cPattern] result
self.cpattern.ngrams(result, n)
cdef cPattern cngram
cdef vector[cPattern].iterator it = result.begin()
cdef vector[cPattern].iterator it_end = result.end()
while it != it_end:
cngram = deref(it)
ngram = Pattern()
ngram.bind(cngram)
yield ngram
inc(it)
def parts(self):
"""Generating iterating over the consecutive non-gappy parts in a skipgram of flexgram
:rtype: generator over Pattern instances
"""
cdef vector[cPattern] result
self.cpattern.parts(result)
cdef cPattern cngram
cdef vector[cPattern].iterator it = result.begin()
cdef vector[cPattern].iterator it_end = result.end()
while it != it_end:
cngram = deref(it)
ngram = Pattern()
ngram.bind(cngram)
yield ngram
inc(it)
def gaps(self):
"""Generator iterating over the gaps in a skipgram or flexgram, return a tuple (begin,length) for each. For flexgrams, the minimum length (1) is always returned.
:rtype: generator over (begin, length) tuples
"""
cdef vector[pair[int,int]] result
self.cpattern.gaps(result)
cdef vector[pair[int,int]].iterator it = result.begin()
cdef vector[pair[int,int]].iterator it_end = result.end()
cdef pair[int,int] p
while it != it_end:
p = deref(it)
yield (p.first, p.second)
inc(it)
def toflexgram(self):
"""Converts a skipgram to a flexgram
:rtype: Pattern
"""
cdef cPattern newcpattern = self.cpattern.toflexgram()
newpattern = Pattern()
newpattern.bind(newcpattern)
return newpattern
def subngrams(self,int minn=1,int maxn=99):
"""Generator iterating over all ngrams of all sizes that are enclosed within this pattern. Despite the name, this may also return skipgrams!
:param minn: minimum length (default 1)
:type minn: int
:param maxn: maximum length (default unlimited)
:type maxn: int
:rtype: generator over Pattern instances
"""
minn = max(1,minn)
maxn = min(maxn, len(self) -1 )
for n in range(minn,maxn+1):
for pattern in self.ngrams(n):
yield pattern
def tolist(self):
"""Returns a list representing the raw classes in the pattern"""
cdef vector[unsigned int] state = self.cpattern.tovector()
return state
def __bytes__(self):
cdef int s = self.bytesize()
cdef bytes b = self.cpattern.data[:s]
return b
def __getstate__(self):
cdef int s = self.bytesize()
cdef bytes b = self.cpattern.data[:s]
return b
def __setstate__(self, bytes byterep):
cdef unsigned char * cdata = byterep
self.cpattern.set(cdata, len(byterep))
def isgap(self, int index):
return self.cpattern.isgap(index)
def isskipgram(self):
return self.cpattern.isskipgram()
def isflexgram(self):
return self.cpattern.isflexgram()
def instanceof(self, Pattern skipgram):
"""Is this an instantiation of the skipgram/flexgram? Instantiation is not necessarily full, aka: A ? B C is also an instantiation of A ? ? C"""
return self.cpattern.instanceof(skipgram.cpattern.getpointer())
cdef class IndexedData:
"""IndexedData is essentially a set of indexes in the form of (sentence,token) tuples, sentence is generally 1-indexed, token is always 0-indexed. It is used by Indexed Pattern Models to keep track of exact occurrences of all the patterns. Use len() to if you're merely interested in the number of occurrences, rather than their exact wherabouts."""
cdef cIndexedData data
cdef bind(self, cIndexedData cdata):
self.data = cdata
def __contains__(self, item):
"""Tests if the specified (sentence,token) tuple is in the set"""
if not isinstance(item, tuple) or len(item) != 2:
raise ValueError("Item should be a 2-tuple (sentence,token)")
cdef cIndexReference ref = cIndexReference(item[0], item[1])
return self.data.has(ref)
def __iter__(self):
"""Iterate over all (sentence,token) tuples in the set"""
cdef cIndexReference ref
cdef cIndexedData.iterator it = self.data.begin()
cdef cIndexedData.iterator it_end = self.data.end()
while it != it_end:
ref = deref(it)
yield (ref.sentence, ref.token)
inc(it)
def __len__(self):
"""Returns the number of occurrences, i.e. the length of the set"""
return self.data.size()
def __bool__(self):
return self.data.size() > 0
def __int__(self):
return self.data.size()
cdef class PatternSet:
"""This is a simple low-level set that contains Pattern instances"""
cdef cPatternSet[uint] data
cdef cPatternSet[uint].iterator it
cdef cPatternSet[uint].iterator it_end
@include colibricore_patternset.pxi
cdef class PatternDict_int32: #maps Patterns to uint32
"""This is a simple low-level dictionary that takes Pattern instances as keys, and integer (max 32 bit, unsigned) as value. For complete pattern models, use IndexedPatternModel or UnindexPatternModel instead."""
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t] data
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t].iterator it
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t].iterator it_end
cdef uint32_t value
@include colibricore_patterndict.pxi
def __setitem__(self, Pattern pattern, uint32_t v):
"""Set the value for a pattern in the dictionary
:param pattern: the pattern
:param value: its value
"""
self.data[pattern.cpattern] = v
cdef bind(self, cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t]& newdata):
self.data = newdata
cdef class SmallPatternDict_int32: #maps Patterns to uint32
"""This is a simple low-level dictionary that takes Pattern instances as keys, and integer (max 32 bit, unsigned) as value. For complete pattern models, use IndexedPatternModel or UnindexPatternModel instead. This is a Small version taht allows at most 65536 patterns."""
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint16_t] data
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint16_t].iterator it
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint16_t].iterator it_end
cdef uint32_t value
@include colibricore_patterndict.pxi
def __setitem__(self, Pattern pattern, uint32_t v):
"""Set the value for a pattern in the dictionary
:param pattern: the pattern
:param value: its value
"""
self.data[pattern.cpattern] = v
cdef bind(self, cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint16_t]& newdata):
self.data = newdata
cdef class TinyPatternDict_int32: #maps Patterns to uint32
"""This is a simple low-level dictionary that takes Pattern instances as keys, and integer (max 32 bit, unsigned) as value. For complete pattern models, use IndexedPatternModel or UnindexPatternModel instead. This is a tiny version that allow only up to 256 patterns."""
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint8_t] data
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint8_t].iterator it
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint8_t].iterator it_end
cdef uint32_t value
@include colibricore_patterndict.pxi
def __setitem__(self, Pattern pattern, uint32_t v):
"""Set the value for a pattern in the dictionary
:param pattern: the pattern
:param value: its value
"""
self.data[pattern.cpattern] = v
cdef bind(self, cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint8_t]& newdata):
self.data = newdata
cdef class PatternDict_int: #maps Patterns to uint64
"""This is a simple low-level dictionary that takes Pattern instances as keys, and integer (unsigned 64 bit) as value. For complete pattern models, use IndexedPatternModel or UnindexPatternModel instead."""
cdef cPatternMap[uint,cBaseValueHandler[uint],uint32_t] data
cdef cPatternMap[uint,cBaseValueHandler[uint],uint32_t].iterator it
cdef cPatternMap[uint,cBaseValueHandler[uint],uint32_t].iterator it_end
cdef int value
@include colibricore_patterndict.pxi
def __setitem__(self, Pattern pattern, int v):
"""Set the value for a pattern in the dictionary
:param pattern: the pattern
:param value: its value
"""
self.data[pattern.cpattern] = v
cdef class PatternDict_float: #maps Patterns to float
"""This is a simple low-level dictionary that takes Pattern instances as keys, and float (double) as value. For complete pattern models, use IndexedPatternModel or UnindexPatternModel instead."""
cdef cPatternMap[float,cBaseValueHandler[float],uint32_t] data
cdef cPatternMap[float,cBaseValueHandler[float],uint32_t].iterator it
cdef cPatternMap[float,cBaseValueHandler[float],uint32_t].iterator it_end
cdef float value
@include colibricore_patterndict.pxi
def __setitem__(self, Pattern pattern, float v):
"""Set the value for a pattern in the dictionary
:param pattern: the pattern
:param value: its value
"""
self.data[pattern.cpattern] = v
cdef class AlignedPatternDict_int32: #maps Patterns to Patterns to uint32 (nested dicts)
"""This is a simple low-level dictionary that takes Pattern instances as keys, and integer (unsigned 64 bit) as value. For complete pattern models, use IndexedPatternModel or UnindexPatternModel instead."""
cdef cAlignedPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t] data
cdef cAlignedPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t].iterator it
cdef cAlignedPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t].iterator it_end
def __len__(self):
"""Return the total number of patterns in the dictionary. If you want the length for a particular pattern, use childcount(pattern)"""
return self.data.size()
def childcount(self, Pattern pattern):
"""Returns the number of children for the specified pattern. Use children(pattern) to iterate over them."""
return self.data[pattern.cpattern].size()
cpdef has(self, Pattern pattern):
return self.data.has(pattern.cpattern)
cpdef haspair(self, Pattern pattern, Pattern pattern2):
if not self.data.has(pattern.cpattern):
return False
else:
return self.data[pattern.cpattern].has(pattern2.cpattern)
def __contains__(self, item):
"""Test if the pattern or the combination of patterns is in the aligned dictionary::
pattern in aligneddict
Or:
(pattern1,pattern2) in aligneddict
"""
if isinstance(item, tuple):
if len(item) != 2:
raise ValueError("Expected instance of Pattern or two-tuple of Patterns")
elif not isinstance(item[0], Pattern) or not isinstance(item[1], Pattern):
raise ValueError("Expected instance of Pattern or two-tuple of Patterns")
return self.haspair(item[0], item[1])
elif not isinstance(item, Pattern):
raise ValueError("Expected instance of Pattern or two-tuple of Patterns")
return self.has(item)
def __iter__(self):
"""Iterate over all patterns in the dictionary. If you want to iterate over pattern pairs, use pairs() instead, to iterate over the children for a specific pattern, use children()"""
it = self.data.begin()
cdef cPattern cpattern
it_end = self.data.end()
while it != it_end:
cpattern = deref(it).first
pattern = Pattern()
pattern.bind(cpattern)
yield pattern
inc(it)
def children(self, Pattern pattern):
"""Iterate over all patterns in the dictionary. If you want to iterate over pattern pairs, use pairs() instead"""
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t].iterator it2
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t].iterator it2_end
cdef cPattern cpattern
it2 = self.data[pattern.cpattern].begin()
it2_end = self.data[pattern.cpattern].end()
while it2 != it2_end:
cpattern = deref(it2).first
pattern = Pattern()
pattern.bind(cpattern)
yield pattern
inc(it2)
def items(self):
"""Iterate over all pattern pairs and their values in the dictionary. Yields (pattern1,pattern2,value) tuples"""
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t].iterator it2
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t].iterator it2end
cdef int value
it = self.data.begin()
cdef cPattern cpattern
it_end = self.data.end()
while it != it_end:
cpattern = deref(it).first
pattern = Pattern()
pattern.bind(cpattern)
it2 = self.data[pattern.cpattern].begin()
it2end = self.data[pattern.cpattern].end()
while it2 != it2end:
cpattern = deref(it2).first
pattern2 = Pattern()
pattern2.bind(cpattern)
value = deref(it2).second
yield pattern, pattern2, value
inc(it2)
inc(it)
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t] * getmap(self, Pattern pattern):
if not isinstance(pattern, Pattern):
raise ValueError("Expected instance of Pattern")
if not self.has(pattern):
self.data[pattern.cpattern]
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t] * m = &(self.data[pattern.cpattern])
return m
cpdef getpair(self, Pattern pattern, Pattern pattern2):
return self.data[pattern.cpattern][pattern2.cpattern]
cpdef setpair(self, Pattern pattern, Pattern pattern2, uint32_t value):
self.data[pattern.cpattern][pattern2.cpattern] = value
def __getitem__(self, item):
"""Retrieve the item, item is a two-tuple of Pattern instances.
aligneddict[(pattern1,pattern2)]
:param item: A two tuple of Pattern instances
"""
cdef cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint32_t] * mapdata
if isinstance(item, tuple):
if len(item) != 2:
raise ValueError("Expected instance of Pattern or two-tuple of Patterns")
elif not isinstance(item[0], Pattern) or not isinstance(item[1], Pattern):
raise ValueError("Expected instance of Pattern or two-tuple of Patterns")
return self.getpair(item[0], item[1])
elif isinstance(item, Pattern):
mapdata = self.getmap(item[0])
d = PatternDict_int32()
d.bind(deref(mapdata))
return d
else:
raise ValueError("Expected instance of Pattern or two-tuple of Patterns")
def __setitem__(self, item, value):
if isinstance(item, tuple):
if len(item) != 2:
raise ValueError("Expected two-tuple of Patterns")
elif not isinstance(item[0], Pattern) or not isinstance(item[1], Pattern):
raise ValueError("Expected two-tuple of Patterns")
self.setpair(item[0], item[1], value)
else:
raise ValueError("Expected two-tuple of Patterns")
def read(self, str filename):
if os.path.exists(filename):
self.data.read(encode(filename))
else:
raise FileNotFoundError
def write(self, str filename):
self.data.write(encode(filename))
cdef class IndexedPatternModel:
"""Indexed Pattern Model. Implemented using a hash map (dictionary)"""
cdef cIndexedPatternModel[cPatternMap[cIndexedData,cIndexedDataHandler,uint64_t]] data
cdef cPatternModel[cIndexedData,cIndexedDataHandler,cPatternMap[cIndexedData,cIndexedDataHandler,uint64_t]].iterator it
@include colibricore_patternmodel.pxi
@include colibricore_indexedpatternmodel.pxi
cdef class HashOrderedIndexedPatternModel:
"""Indexed Pattern Model. Implemented using an ordered map"""
cdef cIndexedPatternModel[cHashOrderedPatternMap[cIndexedData,cIndexedDataHandler,uint64_t]] data
cdef cPatternModel[cIndexedData,cIndexedDataHandler,cHashOrderedPatternMap[cIndexedData,cIndexedDataHandler,uint64_t]].iterator it
@include colibricore_patternmodel.pxi
@include colibricore_indexedpatternmodel.pxi
cdef class PatternSetModel:
cdef cPatternSetModel data
@include colibricore_patternset.pxi
cdef cPatternModelInterface* getinterface(self):
return self.data.getinterface()
def __init__(self, str filename = "",PatternModelOptions options = None):
"""Initialise a pattern model. Either an empty one or loading from file.
:param filename: The name of the file to load, must be a valid colibri patternmodel file
:type filename: str
:param options: An instance of PatternModelOptions, containing the options used for loading
:type options: PatternModelOptions
"""
if filename:
if not options:
options = PatternModelOptions()
self.load(filename,options)
def load(self, str filename, PatternModelOptions options=None):
"""Load a patternmodel from file
:param filename: The name of the file to load, must be a valid colibri patternmodel file
:type filename: str
:param options: An instance of PatternModelOptions, containing the options used for loading
:type options: PatternModelOptions
"""
if options is None:
options = PatternModelOptions()
if filename and not os.path.exists(filename):
raise FileNotFoundError(filename)
self.data.load(encode(filename), options.coptions)
def read(self, str filename, PatternModelOptions options=None):
"""Alias for load"""
self.load(filename, options)
def write(self, str filename):
"""Write a patternmodel to file
:param filename: The name of the file to write to
:type filename: str
"""
self.data.write(encode(filename))
cpdef write(self, str filename):
"""Write a patternmodel to file
:param filename: The name of the file to write to
:type filename: str
"""
self.data.write(encode(filename))
cdef class UnindexedPatternModel:
"""Unindexed Pattern Model, less flexible and powerful than its indexed counterpart, but smaller memory footprint"""
cdef cPatternModel[uint32_t,cBaseValueHandler[uint32_t],cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint64_t]] data
cdef cPatternModel[uint32_t,cBaseValueHandler[uint32_t],cPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint64_t]].iterator it
@include colibricore_patternmodel.pxi
@include colibricore_unindexedpatternmodel.pxi
cdef class OrderedUnindexedPatternModel:
"""Unindexed Pattern Model, implemented using an ordered map, less flexible and powerful than its indexed counterpart, but smaller memory footprint"""
cdef cPatternModel[uint32_t,cBaseValueHandler[uint32_t],cHashOrderedPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint64_t]] data
cdef cPatternModel[uint32_t,cBaseValueHandler[uint32_t],cHashOrderedPatternMap[uint32_t,cBaseValueHandler[uint32_t],uint64_t]].iterator it
@include colibricore_patternmodel.pxi
@include colibricore_unindexedpatternmodel.pxi
cdef class PatternModelOptions:
"""Options for Pattern model, you can get and set the following attributes:
* MINTOKENS - The token threshold, patterns with an occurrence below this will be pruned
* MAXLENGTH - Maximum pattern length
* DOSKIPGRAMS - Compute skipgrams?
* DOSKIPGRAMS_EXHAUSTIVE - Compute skipgrams exhaustively?
* MINSKIPTYPES - Minimum amount of different skip content types
* MAXSKIPS - The maximum amount of skips in a skipgram
* DOREVERSEINDEX - Build reverse index? (default: True)
* DOPATTERNPERLINE - Assume each line holds one single pattern.
* MINTOKENS_UNIGRAMS - Word occurrence threshold (secondary threshold): only count patterns in which the words/unigrams occur at least this many times, only effective when the primary
* MINTOKENS_SKIPGRAMS - The occurrence threshold for skipgrams, minimum amount of occurrences for a pattern to be included in a model. Defaults to the same value as MINTOKENS. Only used if DOSKIPGRAMS or DO_SKIPGRAMS_EXHAUSTIVE is set to true
* DOREMOVENGRAMS - Remove n-grams from the model
* DOREMOVESKIPGRAMS - Remove skipgrams from the model
* DOREMOVEFLEXGRAMS - Remove flexgrams from the model
* DORESET - Reset all counts before training
* PRUNENONSUBSUMED - Prune all n-grams up to this length that are *NOT* subsumed by higher-order ngrams
* PRUNESUBSUMED - Prune all n-grams up to this length that are subsumed by higher-order ngrams
* DEBUG
* QUIET (default: False)
These can also be passed at keyword arguments to the constructor, in a case insensitive fashion::
options = PatternModelOptions(mintokens=3)
"""
cdef cPatternModelOptions coptions
def __init__(self, **kwargs):
for kwarg, value in kwargs.items():
setattr(self,kwarg.upper(), value)
def __setattr__(self,key, value):
if key == 'MINTOKENS':
self.coptions.MINTOKENS = value
elif key == 'MINLENGTH':
self.coptions.MINLENGTH = value
elif key == 'MAXLENGTH':
self.coptions.MAXLENGTH = value
elif key == 'DOSKIPGRAMS':
self.coptions.DOSKIPGRAMS = value
elif key == 'DOSKIPGRAMS_EXHAUSTIVE':
self.coptions.DOSKIPGRAMS_EXHAUSTIVE = value
elif key == 'MINTOKENS_UNIGRAMS':
self.coptions.MINTOKENS_UNIGRAMS = value
elif key == 'MINTOKENS_SKIPGRAMS':
self.coptions.MINTOKENS_SKIPGRAMS = value
elif key == 'MINSKIPTYPES':
self.coptions.MINSKIPTYPES = value
elif key == 'MAXSKIPS':
self.coptions.MAXSKIPS = value
elif key == 'DOREVERSEINDEX':
self.coptions.DOREVERSEINDEX = value
elif key == 'DOPATTERNPERLINE':
self.coptions.DOPATTERNPERLINE = value
elif key == 'DOREMOVENGRAMS':
self.coptions.DOREMOVENGRAMS = value
elif key == 'DOREMOVESKIPGRAMS':
self.coptions.DOREMOVESKIPGRAMS = value
elif key == 'DOREMOVEFLEXGRAMS':
self.coptions.DOREMOVEFLEXGRAMS = value
elif key == 'DORESET':
self.coptions.DORESET = value
elif key == 'DEBUG':
self.coptions.DEBUG = value
elif key == 'QUIET':
self.coptions.QUIET = value
elif key == 'PRUNENONSUBSUMED':
self.coptions.PRUNENONSUBSUMED = value
elif key == 'PRUNESUBSUMED':
self.coptions.PRUNESUBSUMED = value
else:
raise KeyError
def __getattr__(self,key):
if key == 'MINTOKENS':
return self.coptions.MINTOKENS
elif key == 'MINLENGTH':
return self.coptions.MINLENGTH
elif key == 'MAXLENGTH':
return self.coptions.MAXLENGTH
elif key == 'DOSKIPGRAMS':
return self.coptions.DOSKIPGRAMS
elif key == 'DOSKIPGRAMS_EXHAUSTIVE':
return self.coptions.DOSKIPGRAMS_EXHAUSTIVE
elif key == 'MINTOKENS_UNIGRAMS':
return self.coptions.MINTOKENS_UNIGRAMS
elif key == 'MINTOKENS_SKIPGRAMS':
return self.coptions.MINTOKENS_SKIPGRAMS
elif key == 'MINSKIPTYPES':
return self.coptions.MINSKIPTYPES
elif key == 'MAXSKIPS':
return self.coptions.MAXSKIPS
elif key == 'DOREVERSEINDEX':
return self.coptions.DOREVERSEINDEX
elif key == 'DOPATTERNPERLINE':
return self.coptions.DOPATTERNPERLINE
elif key == 'DOREMOVENGRAMS':
return self.coptions.DOREMOVENGRAMS
elif key == 'DOREMOVESKIPGRAMS':
return self.coptions.DOREMOVESKIPGRAMS
elif key == 'DOREMOVEFLEXGRAMS':
return self.coptions.DOREMOVEFLEXGRAMS
elif key == 'DORESET':
return self.coptions.DORESET
elif key == 'DEBUG':
return self.coptions.DEBUG
elif key == 'QUIET':
return self.coptions.QUIET
elif key == 'PRUNENONSUBSUMED':
return self.coptions.PRUNENONSUBSUMED
elif key == 'PRUNESUBSUMED':
return self.coptions.PRUNESUBSUMED
else:
raise KeyError
cdef class IndexedCorpus:
"""An indexed version of a corpus, reads an entire corpus (colibri.dat file) in memory"""
cdef cIndexedCorpus * data
cdef str _filename
cdef bool unload
cdef object frommodel #allow assigning python model from which we take the reverse index, to prevent the python garbage collector collecting the model when we are still alive
def __init__(self, str filename=""):
""":param filename: The name of the colibri.dat file to load"""
self._filename = filename
self.data = new cIndexedCorpus()
if filename:
self.data.load(encode(filename), True) #last bool is debug