PhraseAttributes.py
9.79 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
from syntax.models_phrase import *
from syntax.models import Position
def get_case(name):
return Case.objects.get(name=name)
def get_prep(name):
prep, _ = Preposition.objects.get_or_create(name=name)
return prep
def get_comprep(name):
prep, _ = ComplexPreposition.objects.get_or_create(name=name)
return prep
def get_cptype(name):
cptype, _ = CPType.objects.get_or_create(name=name)
return cptype
def get_cpreals(names):
cpreals = []
if names:
for name in names:
cpreal, _ = CPRealisation.objects.get_or_create(name=name)
cpreals.append(cpreal)
return cpreals
def get_aspect(name):
return PhraseAspect.objects.get(name=name)
def get_neg(name):
return PhraseNegativity.objects.get(name=name)
def get_inhsie(name):
return PhraseInherentSie.objects.get(name=name)
def get_advcat(name):
return AdverbialCategory.objects.get(name=name)
def get_comparcat(name):
comparcat, _ = ComparCategory.objects.get_or_create(name=name)
return comparcat
def get_num(name):
return Number.objects.get(name=name)
def get_gend(name):
return Gender.objects.get(name=name)
def get_deg(name):
return Degree.objects.get(name=name)
def store_phrase(phrase):
dummy_postition = Position.objects.get(id=1)
phrase.store(dummy_postition)
# TODO? a better solution: make store() return the stored object?
# this is ugly: retrieving the just-stored phrase by its text_rep...
return PhraseType.objects.get(text_rep=str(phrase))
# for compar
def get_lexes(lexes):
return [store_phrase(lex) for lex in lexes]
# for lex(xp), lex(advp)
def get_lex(phrase):
return store_phrase(phrase)
# for fixed phrases
def get_phrase(phrase):
return store_phrase(phrase)
def get_text(text):
return text
#-------------------------------------------------------------------------------
'''
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,
}),
'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
def do_get_attributes(phrase_type, phrase_obj, lex):
helper = attrs_helpers[phrase_type]
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_obj)) for key, getter in attr_getters.items() }
m2m_attrs = { key : eval('get_{}'.format(key))(getter(phrase_obj)) for key, getter in m2m_attr_getters.items() }
if cls is not None:
attrs, _ = cls.objects.get_or_create(**kwargs)
for key, values in m2m_attrs.items():
if values:
getattr(attrs, key).set(values)
print(attrs)
return attrs
return None
def get_attributes(phrase_type, phrase_obj, **kwargs):
return do_get_attributes(phrase_type, phrase_obj, False, **kwargs)
def get_lex_attributes(phrase_type, phrase_obj, **kwargs):
return do_get_attributes(phrase_type, phrase_obj, True, **kwargs)