PhraseAttributes.py
10.9 KB
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
#! /usr/bin/python
# -*- coding: utf-8 -*-
from syntax.models_phrase import *
from syntax.models import Position
def get_case(name, **kwargs):
return Case.objects.get(name=name)
def get_prep(name, **kwargs):
prep, _ = Preposition.objects.get_or_create(name=name)
return prep
def get_comprep(name, **kwargs):
prep, _ = ComplexPreposition.objects.get_or_create(name=name)
return prep
def get_cptype(name, **kwargs):
cptype, _ = CPType.objects.get_or_create(name=name)
return cptype
def get_cpreals(names, **kwargs):
cpreals = []
if names:
for name in names:
cpreal, _ = CPRealisation.objects.get_or_create(name=name)
cpreals.append(cpreal)
return cpreals
def get_aspect(name, **kwargs):
return PhraseAspect.objects.get(name=name)
def get_neg(name, **kwargs):
return PhraseNegativity.objects.get(name=name)
def get_inhsie(name, **kwargs):
return PhraseInherentSie.objects.get(name=name)
def get_advcat(name, **kwargs):
return AdverbialCategory.objects.get(name=name)
def get_comparcat(name, **kwargs):
comparcat, _ = ComparCategory.objects.get_or_create(name=name)
return comparcat
def get_num(name, **kwargs):
return Number.objects.get(name=name)
def get_gend(name, **kwargs):
return Gender.objects.get(name=name)
def get_deg(name, **kwargs):
return Degree.objects.get(name=name)
#def store_phrase(phrase):
# dummy_postition = Position.objects.get(id=1)
# return phrase.store(dummy_postition, dict())
# for compar
def get_lexes(phrases, stored_positions=None):
#return [store_phrase(lex) for lex in lexes]
return [lex.store(None, stored_positions) for lex in phrases]
# for lex(xp), lex(advp)
def get_lex(phrase, stored_positions=None):
return phrase.store(None, stored_positions)
# for fixed phrases
def get_phrase(phrase, stored_positions=None):
return phrase.store(None, stored_positions)
def get_text(text, **kwargs):
return text.strip('\'')
#-------------------------------------------------------------------------------
'''
transformations = {
'case' : get_case,
'prep' : get_prep,
'comprep' : get_comprep,
'cptype' : get_cptype,
'aspect' : get_aspect,
'neg' : get_neg,
'advcat' : get_advcat,
'comparcat' : get_comparcat,
# lex
'num' : get_num,
}
'''
class AttributesHelper(object):
# many-to-many fields can’t be initialised on model object creation
def __init__(self, attrs_cls=None, lex_attrs_cls=None, attr_getters=None, m2m_attr_getters={}, lex_attr_getters=None, lex_m2m_attr_getters={}):
self.attrs_cls = attrs_cls
self.lex_attrs_cls = lex_attrs_cls
self.attr_getters = attr_getters
self.m2m_attr_getters = m2m_attr_getters
self.lex_attr_getters = lex_attr_getters
self.lex_m2m_attr_getters = lex_m2m_attr_getters
prepp_attr_getters = {
'prep' : lambda x: x._prep._value,
'case' : lambda x: x._prep._case._value,
}
attrs_helpers = {
'np' : AttributesHelper(
attrs_cls=NPAttributes,
lex_attrs_cls=LexNPAttributes,
attr_getters={
'case' : lambda x: x._case._value,
},
lex_attr_getters={
'num' : lambda x: x._number,
}),
'nump' : AttributesHelper(
attrs_cls=NumPAttributes,
attr_getters={
'case' : lambda x: x._case._value,
},
#lex_attr_getters={
# 'num' : lambda x: x._number,
#}
),
'adjp' : AttributesHelper(
attrs_cls=AdjPAttributes,
lex_attrs_cls=LexAdjPAttributes,
attr_getters={
'case' : lambda x: x._case._value,
},
lex_attr_getters={
'num' : lambda x: x._number,
'gend' : lambda x: x._gender,
'deg' : lambda x: x._degree,
}),
'ppasp' : AttributesHelper(
attrs_cls=PPasPAttributes,
lex_attrs_cls=LexPPasPAttributes,
attr_getters={
'case' : lambda x: x._case._value,
},
lex_attr_getters={
'num' : lambda x: x._number,
'gend' : lambda x: x._gender,
'neg' : lambda x: x._negativity,
}),
'pactp' : AttributesHelper(
attrs_cls=PActPAttributes,
lex_attrs_cls=LexPActPAttributes,
attr_getters={
'case' : lambda x: x._case._value,
},
lex_attr_getters={
'num' : lambda x: x._number,
'gend' : lambda x: x._gender,
'neg' : lambda x: x._negativity,
'inhsie' : lambda x: x._inherent_sie,
}),
'prepnp' : AttributesHelper(
attrs_cls=PrepNPAttributes,
lex_attrs_cls=LexPrepNPAttributes,
attr_getters=prepp_attr_getters,
lex_attr_getters={
'num' : lambda x: x._number,
}),
'prepnump' : AttributesHelper(
attrs_cls=PrepNumPAttributes,
#lex_attrs_cls=LexPrepNumPAttributes,
attr_getters=prepp_attr_getters,
#lex_attr_getters={
# 'num' : lambda x: x._number,
#}
),
'prepadjp' : AttributesHelper(
attrs_cls=PrepAdjPAttributes,
lex_attrs_cls=LexPrepAdjPAttributes,
attr_getters=prepp_attr_getters,
lex_attr_getters={
'num' : lambda x: x._number,
'gend' : lambda x: x._gender,
'deg' : lambda x: x._degree,
}),
'prepppasp' : AttributesHelper(
attrs_cls=PrepPPasPAttributes,
lex_attrs_cls=LexPrepPPasPAttributes,
attr_getters=prepp_attr_getters,
lex_attr_getters={
'num' : lambda x: x._number,
'gend' : lambda x: x._gender,
'neg' : lambda x: x._negativity,
}),
'prepgerp' : AttributesHelper(
attrs_cls=PrepGerPAttributes,
lex_attrs_cls=LexPrepGerPAttributes,
attr_getters=prepp_attr_getters,
lex_attr_getters={
'num' : lambda x: x._number,
'neg' : lambda x: x._negativity,
'inhsie' : lambda x: x._inherent_sie,
}),
'comprepnp' : AttributesHelper(
attrs_cls=ComPrepNPAttributes,
attr_getters={
'comprep' : lambda x: x._prep._value,
}),
'cp' : AttributesHelper(
attrs_cls=CPAttributes,
lex_attrs_cls=LexCPAttributes,
attr_getters={
'cptype' : lambda x: x._type._value,
},
lex_attr_getters={
'neg' : lambda x: x._negativity,
'inhsie' : lambda x: x._inherent_sie,
},
m2m_attr_getters={
'cpreals' : lambda x: x._type._realisations,
}),
'ncp' : AttributesHelper(
attrs_cls=NCPAttributes,
lex_attrs_cls=LexNCPAttributes,
attr_getters={
'case' : lambda x: x._case._value,
'cptype' : lambda x: x._type._value,
},
lex_attr_getters={
'neg' : lambda x: x._negativity,
'inhsie' : lambda x: x._inherent_sie,
},
m2m_attr_getters={
'cpreals' : lambda x: x._type._realisations,
}),
'prepncp' : AttributesHelper(
attrs_cls=PrepNCPAttributes,
attr_getters={
'prep' : lambda x: x._prep._value,
'case' : lambda x: x._prep._case._value,
'cptype' : lambda x: x._type._value,
},
m2m_attr_getters={
'cpreals' : lambda x: x._type._realisations,
}),
'infp' : AttributesHelper(
attrs_cls=InfPAttributes,
lex_attrs_cls=LexInfPAttributes,
attr_getters={
'aspect' : lambda x: x._aspect._value,
},
lex_attr_getters={
'neg' : lambda x: x._negativity,
'inhsie' : lambda x: x._inherent_sie,
}),
'xp' : AttributesHelper(
attrs_cls=XPAttributes,
lex_attrs_cls=LexXPAttributes,
attr_getters={
'advcat' : lambda x: x._category._value,
},
lex_attr_getters={
'lex' : lambda x: x._lex,
}),
'advp' : AttributesHelper(
attrs_cls=AdvPAttributes,
lex_attrs_cls=LexAdvPAttributes,
attr_getters={
'advcat' : lambda x: x._category._value,
},
lex_attr_getters={
'deg' : lambda x: x._degree,
}),
'compar' : AttributesHelper(
attrs_cls=ComparAttributes,
lex_attrs_cls=LexComparAttributes,
attr_getters={
'comparcat' : lambda x: x._category._value,
},
lex_attr_getters={},
lex_m2m_attr_getters={
'lexes' : lambda x: x._lexes,
}),
'fixed' : AttributesHelper(
attrs_cls=FixedAttributes,
attr_getters={
'phrase' : lambda x: x._phrase,
'text' : lambda x: x._text,
}),
}
'''
attrs_cls = {
'np' : NPAttributes,
'adjp' : NPAttributes,
'prepnp' : PrepNPAttributes,
'prepadjp' : PrepNPAttributes,
'comprepnp' : ComPrepNPAttributes,
'cp' : CPAttributes,
'ncp' : NCPAttributes,
'prepncp' : PrepNCPAttributes,
'infp' : InfPAttributes,
'xp' : XPAttributes,
'advp' : XPAttributes,
'compar' : ComparAttributes,
}
'''
#-------------------------------------------------------------------------------
def empty_attributes():
attr, _ = EmptyAttributes.objects.get_or_create(empty='_')
return attr
stored_attributes = dict()
def do_get_attributes(phrase, stored_positions, lex):
helper = attrs_helpers[phrase._name]
cls = helper.lex_attrs_cls if lex else helper.attrs_cls
attr_getters = helper.lex_attr_getters if lex else helper.attr_getters
m2m_attr_getters = helper.lex_m2m_attr_getters if lex else helper.m2m_attr_getters
kwargs = { key : eval('get_{}'.format(key))(getter(phrase), stored_positions=stored_positions) for key, getter in attr_getters.items() }
m2m_attrs = { key : eval('get_{}'.format(key))(getter(phrase), stored_positions=stored_positions) for key, getter in m2m_attr_getters.items() }
if cls is not None:
if not m2m_attrs:
# atomic attributes only – uniqueness of attr objects assured by get_or_create
attrs, _ = cls.objects.get_or_create(**kwargs)
else:
# multi-valued attributes (m2m fields) – can’t use get_or_create to assure uniqueness
attrs_key = str(sorted(kwargs.items()) + sorted(m2m_attrs.items()))
if attrs_key in stored_attributes:
attrs = stored_attributes[attrs_key]
else:
attrs = cls.objects.create(**kwargs)
for key, values in m2m_attrs.items():
if values:
getattr(attrs, key).set(values)
stored_attributes[attrs_key] = attrs
return attrs
return None
def get_attributes(phrase, stored_positions):
return do_get_attributes(phrase, stored_positions, False)
def get_lex_attributes(phrase, stored_positions):
return do_get_attributes(phrase, stored_positions, True)