-
Notifications
You must be signed in to change notification settings - Fork 3
/
_categorization.pyx
360 lines (277 loc) · 11.6 KB
/
_categorization.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
# TODO:
# * Update setup.py to handle the extension module properly.
# * Update pyramids.categorization to assume the package is already compiled.
# * Put the pure Python implementation of pyramids.categories back, and use it as a fallback if
# compilation fails.
from cpython cimport Py_INCREF, Py_DECREF
__author__ = 'Aaron Hosford'
__all__ = [
'Property',
'Category',
'LinkLabel',
'CATEGORY_WILDCARD',
'get_all_properties',
'get_all_category_names',
'get_all_link_labels',
]
EMPTY_SET = frozenset()
cdef class InternedString:
cdef str value
# The __interner argument is present to ensure interned strings aren't created via direct
# construction # by accident, which is a common occurrence.
def __cinit__(self, str value, *, StringInterner __interner):
Py_INCREF(value)
self.value = value
def __str__(self) -> str:
return self.value
def __repr__(self) -> str:
return '%s(%r)' % (type(self).__name__, self.value)
def __eq__(self, InternedString other) -> bool:
return self.value is other.value
def __ne__(self, InternedString other) -> bool:
return self.value is not other.value
def __lt__(self, InternedString other) -> bool:
return self.value is not other.value and self.value < other.value
def __le__(self, InternedString other) -> bool:
return self.value is other.value or self.value <= other.value
def __gt__(self, InternedString other) -> bool:
return self.value is not other.value and self.value > other.value
def __ge__(self, InternedString other) -> bool:
return self.value is other.value or self.value >= other.value
def __hash__(self) -> int:
return id(self.value)
def __add__(a, b) -> str:
# Cython doesn't do __radd__. Instead it just calls the same method with the operands
# reversed.
return str(a) + str(b)
def startswith(self, other) -> bool:
return self.value.startswith(other)
def endswith(self, other) -> bool:
return self.value.endswith(other)
def __getitem__(self, x):
return self.value[x]
cpdef long addr(self):
return id(self.value)
cdef class StringInterner:
cdef dict _intern_map
cdef type _subtype
cdef frozenset _all
def __cinit__(self, type subtype=InternedString):
self._intern_map = {}
self._subtype = subtype
self._all = frozenset()
cdef InternedString intern(self, str s):
if s in self._intern_map:
return self._intern_map[s]
else:
result = self._subtype(s, __interner=self)
self._intern_map[s] = result
self._all |= {result}
return result
cdef frozenset get_all(self):
return self._all
cdef class Property(InternedString):
@staticmethod
def get(name) -> Property:
if isinstance(name, Property):
return name
else:
return _property_interner.intern(name)
def __repr__(self) -> str:
return 'Property.get(%r)' % self.value
cdef class CategoryName(InternedString):
@staticmethod
def get(name) -> CategoryName:
if isinstance(name, CategoryName):
return name
else:
return _category_name_interner.intern(name)
def __repr__(self) -> str:
return 'CategoryName.get(%r)' % self.value
cdef class LinkLabel(InternedString):
@staticmethod
def get(name) -> LinkLabel:
if isinstance(name, LinkLabel):
return name
else:
return _link_label_interner.intern(name)
def __repr__(self) -> str:
return 'LinkLabel.get(%r)' % self.value
cpdef frozenset make_property_set(properties):
cdef Property prop
cdef list props
if properties:
props = []
for p in properties:
if isinstance(p, str):
prop = Property.get(p)
else:
prop = p
props.append(prop)
return frozenset(props)
else:
return EMPTY_SET
cdef class Category:
"""Represents a category of classification for a parse tree or parse tree node."""
cdef InternedString _name
cdef frozenset _positive_properties
cdef frozenset _negative_properties
cdef long _hash
# @staticmethod
# def get(name, positive_properties=None, negative_properties=None) -> Category:
# cdef InternedString i_name
# cdef Property prop
# cdef frozenset positives
# cdef frozenset negatives
# cdef Category result
#
# if isinstance(name, str):
# i_name = _category_name_interner.intern(name)
# else:
# i_name = name
# positives = make_property_set(positive_properties)
# negatives = make_property_set(negative_properties)
#
# result = Category(i_name, positives, negatives)
# return result
def __cinit__(self, name, positive_properties=None, negative_properties=None):
cdef long hash_value
cdef Property prop
cdef frozenset both
cdef InternedString i_name
cdef frozenset positives
cdef frozenset negatives
if isinstance(name, str):
i_name = _category_name_interner.intern(name)
else:
i_name = name
positives = make_property_set(positive_properties)
negatives = make_property_set(negative_properties)
hash_value = hash(i_name)
for prop in positives:
hash_value ^= hash(prop) * 5
for prop in negatives:
hash_value ^= hash(prop) * 7
both = positives & negatives
if both:
raise ValueError("Property is both positive and negative: %s" % ', '.join(str(prop)
for prop in both))
Py_INCREF(i_name)
Py_INCREF(positives)
Py_INCREF(negatives)
self._name = i_name
self._positive_properties = positives
self._negative_properties = negatives
self._hash = hash_value
@property
def name(self) -> InternedString:
return self._name
@property
def positive_properties(self) -> frozenset:
return self._positive_properties
@property
def negative_properties(self) -> frozenset:
return self._negative_properties
#cdef bint has_props(self, vector[Property] properties):
def has_properties(self, *properties) -> bool:
cdef Property i_prop
for prop in properties:
i_prop = Property.get(prop)
if i_prop not in self._positive_properties:
return False
return True
def lacks_properties(self, *properties) -> bool:
cdef Property i_prop
for prop in properties:
i_prop = Property.get(prop)
if i_prop in self._positive_properties:
return False
return True
def to_str(self, bint simplify=True) -> str:
properties = []
if self._positive_properties:
properties = sorted(str(prop) for prop in self._positive_properties)
if not simplify and self._negative_properties:
properties.extend(sorted('-' + str(prop) for prop in self._negative_properties))
if properties:
return str(self._name) + '(%s)' % ','.join(properties)
else:
return str(self._name)
def __str__(self) -> str:
return self.to_str()
def __repr__(self) -> str:
return 'Category(%r, %r, %r)' % (str(self._name),
sorted(str(prop) for prop in self._positive_properties),
sorted(str(prop) for prop in self._negative_properties))
def __hash__(self) -> int:
return self._hash
def __eq__(self, Category other) -> bool:
# We can use "is" instead of "==" for names because we intern them all ahead of time.
return self is other or (
self._hash == other._hash and
self._name is other._name and
self._positive_properties == other._positive_properties and
self._negative_properties == other._negative_properties
)
def __ne__(self, Category other) -> bool:
return not self.__eq__(other)
def __le__(self, Category other) -> bool:
if self._name is not other._name: # They are interned...
return self._name < other._name
if len(self._positive_properties) != len(other._positive_properties):
return len(self._positive_properties) < len(other._positive_properties)
if len(self._negative_properties) != len(other._negative_properties):
return len(self._negative_properties) < len(other._negative_properties)
my_sorted_positive = sorted(self._positive_properties)
other_sorted_positive = sorted(other._positive_properties)
if my_sorted_positive != other_sorted_positive:
return my_sorted_positive < other_sorted_positive
return sorted(self._negative_properties) <= sorted(other._negative_properties)
def __lt__(self, Category other) -> bool:
if self._name is not other._name: # They are interned...
return self._name < other._name
if len(self._positive_properties) != len(other._positive_properties):
return len(self._positive_properties) < len(other._positive_properties)
if len(self._negative_properties) != len(other._negative_properties):
return len(self._negative_properties) < len(other._negative_properties)
self_sorted_positive = sorted(self._positive_properties)
other_sorted_positive = sorted(other._positive_properties)
if self_sorted_positive != other_sorted_positive:
return self_sorted_positive < other_sorted_positive
return sorted(self._negative_properties) < sorted(other._negative_properties)
def __ge__(self, Category other) -> bool:
return other <= self
def __gt__(self, Category other) -> bool:
return not (self <= other)
def __contains__(self, Category other) -> bool:
# They must have the same name, and all the properties that apply
# to this category must apply to the other category. We can use
# "is" instead of "==" because we intern the names ahead of time.
return self is other or (
(self._name is other._name or self.is_wildcard()) and
self._positive_properties <= other._positive_properties and
not self._negative_properties & other._positive_properties
)
def is_wildcard(self) -> bool:
return self._name is CATEGORY_WILDCARD
def promote_properties(self, positive, negative) -> Category:
cdef frozenset positives
cdef frozenset negatives
cdef Category category
positives = make_property_set(positive)
negatives = make_property_set(negative)
category = Category(self._name, (self._positive_properties |
(positives - self._negative_properties)),
(self._negative_properties | (negatives - self._positive_properties)))
return category
cdef StringInterner _category_name_interner = StringInterner(CategoryName)
cdef StringInterner _property_interner = StringInterner(Property)
cdef StringInterner _link_label_interner = StringInterner(LinkLabel)
cdef InternedString _CATEGORY_WILDCARD = _category_name_interner.intern("_")
CATEGORY_WILDCARD = _CATEGORY_WILDCARD
def get_all_category_names() -> frozenset:
return _category_name_interner.get_all()
def get_all_properties() -> frozenset:
return _property_interner.get_all()
def get_all_link_labels() -> frozenset:
return _link_label_interner.get_all()