phraseology_generator.py
10.3 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
# -*- coding: utf-8 -*-
from dictionary.models import sort_arguments, sort_positions, sortatributes
from settings import MORFEUSZ2
from copy import deepcopy
def lexicalisation(argument, subj, base, negativity, reference=None):
b = argument.type
if b == 'fixed':
return (get_words(sortatributes(argument)[-1]), [])
attributes = sortatributes(argument)
lexicalisation_type = attributes[0].values.all()[0].argument.type
lexicalisation_parameters = sortatributes(attributes[0].values.all()[0].argument)
if lexicalisation_type == 'xp': # xp(...)[np/prepnp], ...
lexicalisation_type = lexicalisation_parameters[0].values.all()[0].argument.type
lexicalisation_parameters = sortatributes(lexicalisation_parameters[0].values.all()[0].argument)
if lexicalisation_type == 'np': # np(case), number, nouns, atr
nps = get_nps(get_case(lexicalisation_parameters[0], subj, negativity), get_number(attributes[1], subj), get_words(attributes[2]), attributes[3])
return (nps, get_verb(base, get_number(attributes[1], subj), subj))
elif lexicalisation_type == 'prepnp': #prepnp(prep, case), number, nouns, atr
prepnps = get_prepnps(get_preposition(lexicalisation_parameters[0]), get_case(lexicalisation_parameters[1], subj, negativity), get_number(attributes[1], subj), get_words(attributes[2]), attributes[3])
return (prepnps, [])
elif lexicalisation_type == 'adjp': # adjp(case), number, gender, degree, adjectives, atr
adjps = get_adjps(get_case(lexicalisation_parameters[0], subj, negativity, reference), get_number(attributes[1], subj, reference), get_gender(attributes[2], reference), get_degree(attributes[3]), get_words(attributes[4]), attributes[5])
return (adjps, get_verb(base, get_number(attributes[1], subj), subj))
elif lexicalisation_type == 'prepadjp': #prepadjp(prep, case), number, gender, degree, adjectives, atr
prepadjps = get_prepadjps(get_preposition(lexicalisation_parameters[0]), get_case(lexicalisation_parameters[1], subj, False, reference), get_number(attributes[1], subj, reference), get_gender(attributes[2], reference), get_degree(attributes[3]), get_words(attributes[4]), attributes[5])
return (prepadjps, [])
elif lexicalisation_type == 'infp':
infps = get_infps(get_aspect(lexicalisation_parameters[0]), get_words(attributes[2]), attributes[4])
return (infps, [])
else:
return ([], [])
return ([], [])
def is_subj(categories):
for cat in categories:
if cat.category == u'subj':
return True
return False
def get_preposition(attribute):
return attribute.values.all()[0].parameter.type.name
def get_numerals(attribute):
return get_words(attribute)
def get_words(attribute):
words = [word.text[1:-1] for word in attribute.values.all()]
return words
def get_aspect(attribute):
return attribute.values.all()[0].parameter.type.name
def get_case(attribute, is_subj, negativity, reference=None):
case = attribute.values.all()[0].parameter.type.name
if case == u'str':
if is_subj:
case = u'nom'
elif negativity:
case = u'gen'
else:
case = u'acc'
elif case == u'agr' and reference is not None:
_, tag = reference
case = tag.split(':')[2]
return case
def get_number(attribute, is_subj, reference=None):
number = attribute.values.all()[0].parameter.type.name
if number == u'_':
if is_subj:
number = u'sg'
elif number == u'agr' and reference is not None:
_, tag = reference
number = tag.split(':')[1]
return number
def get_gender(attribute, reference=None):
gender = attribute.values.all()[0].parameter.type.name
if gender == u'_':
gender = u'n'
elif gender == u'n':
gender = u'n1'
elif gender == u'm':
gender = u'm1'
elif gender == u'agr' and reference is not None:
_, tag = reference
gender = tag.split(':')[3]
return gender
def get_degree(attribute):
degree = attribute.values.all()[0].parameter.type.name
if degree == u'_':
degree = u'pos'
return degree
def get_nps(case, number, nouns, atr):
result = []
for noun in nouns:
options = [(interp.orth, interp.getTag(MORFEUSZ2)) for interp in MORFEUSZ2.generate(noun.encode('utf8'))]
if case != u'_':
filtered = []
for option in options:
(orth, tag) = option
if u':' + case + u':' in tag:
filtered.append(option)
options = filtered
if number != u'_':
filtered = []
for option in options:
(orth, tag) = option
if u':' + number + u':' in tag:
filtered.append(option)
options = filtered
result += options
return dependents(atr, result)
def get_prepnps(prep, case, number, nouns, _atr):
# ala["ma"] = kot
nps = get_nps(case, number, nouns, _atr)
return [prep + ' ' + np for np in nps]
def get_infps(aspect, verbs, atr):
result = []
for verb in verbs:
options = [(interp.orth, interp.getTag(MORFEUSZ2)) for interp in MORFEUSZ2.generate(verb.encode('utf8'))]
filtered = []
for option in options:
(orth, tag) = option
if u'inf:' in tag:
filtered.append(option)
options = filtered
if aspect != u'_':
for option in options:
(orth, tag) = option
if u':' + aspect + u':' in tag:
filtered.append(option)
options = filtered
result += options
return dependents(atr, result)
def get_adjps(case, number, gender, degree, adjectives, atr):
result = []
for adjective in adjectives:
options = [(interp.orth, interp.getTag(MORFEUSZ2)) for interp in MORFEUSZ2.generate(adjective.encode('utf8'))]
filtered = []
for option in options:
(orth, tag) = option
if u'adj:' in tag:
filtered.append(option)
options = filtered
if case != u'_':
filtered = []
for option in options:
(orth, tag) = option
if u':' + case + u':' in tag:
filtered.append(option)
options = filtered
if number != u'_':
filtered = []
for option in options:
(orth, tag) = option
if u':' + number + u':' in tag:
filtered.append(option)
options = filtered
if gender != u'_':
filtered = []
for option in options:
(orth, tag) = option
if u':' + gender + u':' in tag or u'.' + gender + u':' in tag or u':' + gender + u'.' in tag or u'.' + gender + u'.' in tag:
filtered.append(option)
options = filtered
if degree != u'_':
filtered = []
for option in options:
(orth, tag) = option
if u':' + degree in tag:
filtered.append(option)
options = filtered
result += options
return dependents(atr, result)
def get_prepadjps(prep, case, number, gender, degree, adjectives, _atr):
adjps = get_adjps(case, number, gender, degree, adjectives, _atr)
return [prep + ' ' + adjp for adjp in adjps]
def get_verb(inf, number, is_subj):
if not is_subj:
return None
else:
options = [(interp.orth, interp.getTag(MORFEUSZ2)) for interp in MORFEUSZ2.generate(inf.encode('utf8'))]
filtered = []
for option in options:
(orth, tag) = option
if u'fin:' in tag and u':' + number + ':' in tag and u':ter:' in tag:
filtered.append(option)
options = filtered
return [orth for orth, _ in options]
def dependents(atr, options):
if atr.selection_mode.name == u'ratr' or atr.selection_mode.name == u'ratr1':
result = []
for option in options:
result += phrase(option, atr.values.all())
return result
else:
return [orth for orth, _ in options]
def phrase(head, dependents):
modifiers = {'pre': [], 'post': []}
for dependent in dependents:
values = []
type = None
for argument in dependent.position.arguments.all():
if argument.type == u'fixed':
type = argument.type
elif argument.type == u'lex':
type = sortatributes(argument)[0].values.all()[0].argument.type
value, _ = lexicalisation(argument, False, '', False, head)
values += value
if type == 'adjp':
modifiers['pre'].append(values)
else:
modifiers['post'].append(values)
pre = []
for permutation in permutations(modifiers['pre']):
pre += cartesian(permutation)
pre = [' '.join(words) for words in pre]
pre = list(set(pre))
post = []
for permutation in permutations(modifiers['post']):
post += cartesian(permutation)
post = [' '.join(words) for words in post]
post = list(set(post))
orth, _ = head
result = []
if len(pre) == 0 and len(post) == 0:
result.append(orth)
for prefix in pre:
for suffix in post:
if prefix == '' and suffix == '':
if len(pre) == 1 and len(post) == 1:
result.append(orth)
elif prefix == '':
result.append(orth + ' ' + suffix)
elif suffix == '':
result.append(prefix + ' ' + orth)
else:
result.append(prefix + ' ' + orth + ' ' + suffix)
return result
def cartesian(llist):
if len(llist) == 0:
result = [[]]
else:
result = []
tail = cartesian(llist[1:])
for element in llist[0]:
tailcopy = deepcopy(tail)
for cart in tailcopy:
cart.insert(0, element)
result.append(cart)
result += tail
return result
def permutations(llist):
if len(llist) == 0:
result = [[]]
else:
result = []
perms = permutations(llist[1:])
for perm in perms:
for i in range(0, len(perm) + 1):
permcopy = deepcopy(perm)
permcopy.insert(i, llist[0])
result.append(permcopy)
return result