Blame view

semantics/saving.py 16 KB
Bartłomiej Nitoń authored
1
2
3
4
# -*- coding: utf-8 -*-

from django.db.models import Min
Bartłomiej Nitoń authored
5
from dictionary.models import Argument, Frame, Lemma, NKJP_Example, Position
Bartłomiej Nitoń authored
6
7
8
9
from semantics.change_log import store_old_versions
from semantics.models import Complement, GeneralSelectivePreference, FramePosition,\
                             LexicalUnitExamples, RelationalSelectivePreference, \
                             SelectivePreference, SelectivePreferenceRelations, \
Bartłomiej Nitoń authored
10
                             SemanticFrame, SemanticRole, FrameOpinion                          
Bartłomiej Nitoń authored
11
12
13
14
from wordnet.models import Hypernymy, LexicalUnit, Synonymy, Synset

def modify_frames(lemma_id, operations, user):
    store_old_versions(lemma_id, operations, user)
Tomasz Bartosiak authored
15
    make_operations(lemma_id, operations)
Bartłomiej Nitoń authored
16
Tomasz Bartosiak authored
17
def make_operations(lemma_id, operations):
Tomasz Bartosiak authored
18
    translation = {'unit_id': {}, 'frame_id': {}, 'complement_id': {}, 'preference_id': {}}
Tomasz Bartosiak authored
19
    entry = Lemma.objects.get(id=lemma_id).entry_obj
Bartłomiej Nitoń authored
20
21
22
23
    for operation in operations:
        if operation['operation'] == "create_frame":
            luids = [int(m['id']) for m in operation['meanings']]
            translation['frame_id'][int(operation['id'])] = create_frame(luids)
Tomasz Bartosiak authored
24
        elif operation['operation'] == "add_unit":
Tomasz Bartosiak authored
25
            translation['unit_id'][int(operation['unit']['id'])] = add_unit(entry, operation['unit'])
Bartłomiej Nitoń authored
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
        elif operation['operation'] == "remove_frame":
            if int(operation['id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['id'])]
            else:
                frame_id = int(operation['id'])
            remove_frame(frame_id)
        elif operation['operation'] == "add_argument": 
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            roles = [int(r) for r in operation['role']]
            translation['complement_id'][int(operation['id'])] = add_argument(frame_id, roles)
        elif operation['operation'] == "remove_argument":
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            if int(operation['complement_id']) in translation['complement_id']:
                complement_id = translation['complement_id'][int(operation['complement_id'])]
            else:
                complement_id = int(operation['complement_id'])
            remove_argument(frame_id, complement_id)
        elif operation['operation'] == "connect":
            frame_data = operation['arg'].split('_')
            if int(frame_data[1]) in translation['frame_id']:
                frame_id = translation['frame_id'][int(frame_data[1])]
            else:
                frame_id = int(frame_data[1])
            if int(frame_data[3]) in translation['complement_id']:
                complement_id = translation['complement_id'][int(frame_data[3])]
            else:
                complement_id = int(frame_data[3])
            schema_data = operation['connect'].split('_')
            schema_id = int(schema_data[1])
            position_id = int(schema_data[3])
            argument_id = int(schema_data[5])
            alternation = int(schema_data[7])
            connect(frame_id, complement_id, schema_id, position_id, argument_id, alternation)
        elif operation['operation'] == "disconnect":
            frame_data = operation['arg'].split('_')
            if int(frame_data[1]) in translation['frame_id']:
                frame_id = translation['frame_id'][int(frame_data[1])]
            else:
                frame_id = int(frame_data[1])
            if int(frame_data[3]) in translation['complement_id']:
                complement_id = translation['complement_id'][int(frame_data[3])]
            else:
                complement_id = int(frame_data[3])
            schema_data = operation['connect'].split('_')
            schema_id = int(schema_data[1])
            position_id = int(schema_data[3])
            argument_id = int(schema_data[5])
            alternation = int(schema_data[7])
            disconnect(frame_id, complement_id, schema_id, position_id, argument_id, alternation)
        elif operation['operation'] == "assign_role":
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            if int(operation['complement_id']) in translation['complement_id']:
                complement_id = translation['complement_id'][int(operation['complement_id'])]
            else:
                complement_id = int(operation['complement_id'])
            roles = [int(r) for r in operation['role']]
            assign_role(frame_id, complement_id, roles)
        elif operation['operation'] == "change_units":
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
Tomasz Bartosiak authored
97
            luids = [translation['unit_id'][int(m)] if int(m) in translation['unit_id'] else int(m) for m in operation['units']]
Bartłomiej Nitoń authored
98
            change_units(frame_id, luids)
Bartłomiej Nitoń authored
99
100
101
102
103
104
105
        elif operation['operation'] == "set_opinion":
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            opinion = operation['opinion']
            set_opinion(frame_id, opinion)        
Bartłomiej Nitoń authored
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
        elif operation['operation'] == "add_preference":
            # {operation: 'add_preference', frame_id: frame_id, complement_id: complement_id, preference_id: preference_id, preference: preference}
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            if int(operation['complement_id']) in translation['complement_id']:
                complement_id = translation['complement_id'][int(operation['complement_id'])]
            else:
                complement_id = int(operation['complement_id'])
            preference_id = add_preference(frame_id, complement_id, operation['preference']['type'], operation['preference']['content'])
            translation['preference_id'][operation['preference_id']] = preference_id
        elif operation['operation'] == "remove_preference":
            # {operation: 'remove_preference', frame_id: frame_id, complement_id: complement_id, preference_id: preference_id}
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            if int(operation['complement_id']) in translation['complement_id']:
                complement_id = translation['complement_id'][int(operation['complement_id'])]
            else:
                complement_id = int(operation['complement_id'])
            if operation['preference_id'] in translation['preference_id']:
                preference_id = translation['preference_id'][operation['preference_id']]
            else:
                preference_id = (operation['preference_id'][0], int(operation['preference_id'][1:]))

            remove_preference(frame_id, complement_id, preference_id)
        else:
            pass

def create_frame(luids):
    frame = SemanticFrame()
    frame.save()
    for id in luids:
        lu = LexicalUnit.objects.get(id=id)
        frame.lexical_units.add(lu)
    return frame.id

def add_argument(frame_id, roles):
    if validate_roles(roles):
        frame = SemanticFrame.objects.get(id=frame_id)
        complement = Complement(frame=frame)
        complement.save()
        frame.complements.add(complement)
        role_objects = []
        for r in roles:
            role_objects.append(SemanticRole.objects.get(id=r))
        complement.roles = role_objects
        return complement.id

def remove_frame(frame_id):
    frame = SemanticFrame.objects.get(id=frame_id)
    frame.removed = True
    frame.save()

def remove_argument(frame_id, complement_id):
    Complement.objects.get(id=complement_id).delete()

def connect(frame_id, complement_id, schema_id, position_id, argument_id, alternation):
    schema = Frame.objects.get(id=schema_id)
    position = Position.objects.get(id=position_id)
    argument = Argument.objects.get(id=argument_id)
    fpas = FramePosition.objects.filter(frame=schema, position=position, argument=argument, alternation=alternation)
    if len(fpas) > 0:
        fpa = fpas[0]
    else:
        fpa = FramePosition(frame=schema, position=position, argument=argument, alternation=alternation)
        fpa.save()
    complement = Complement.objects.get(id=complement_id)
    complement.realizations.add(fpa)

def disconnect(frame_id, complement_id, schema_id, position_id, argument_id, alternation):
    schema = Frame.objects.get(id=schema_id)
    position = Position.objects.get(id=position_id)
    argument = Argument.objects.get(id=argument_id)
    fpas = FramePosition.objects.filter(frame=schema, position=position, argument=argument, alternation=alternation)
    if len(fpas) > 0:
        fpa = fpas[0]
    else:
        return
    complement = Complement.objects.get(id=complement_id)
    complement.realizations.remove(fpa)

def assign_role(frame_id, complement_id, roles):
    if validate_roles(roles):
        role_objects = []
        for r in roles:
            role_objects.append(SemanticRole.objects.get(id=r))
        complement = Complement.objects.get(id=complement_id)
        complement.roles = role_objects

def validate_roles(roles):
    role_objects = []
    for r in roles:
        role_objects.append(SemanticRole.objects.get(id=r))
    if len(role_objects) > 2:
        return False
    ok = False
    for r in role_objects:
        if not r.color == None:
            ok = not ok
    return ok

def change_units(frame_id, luids):
    frame = SemanticFrame.objects.get(id=frame_id)
    frame.lexical_units = []
    for id in luids:
        lu = LexicalUnit.objects.get(id=id)
        frame.lexical_units.add(lu)
Bartłomiej Nitoń authored
217
218
219
220
221
222
def set_opinion(frame_id, opinion):
    frame = SemanticFrame.objects.get(id=frame_id)
    frame_opinion = FrameOpinion.objects.get(short=opinion)
    frame.opinion = frame_opinion
    frame.save()
Bartłomiej Nitoń authored
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
# preference_id = add_preference(frame_id, complement_id, operation['preference']['type'], operation['preference']['content'])
def add_preference(frame_id, complement_id, preference_type, preference_content):

    complement = Complement.objects.get(id=complement_id)
    if complement.selective_preference is None:
        sp = SelectivePreference()
        sp.save()
        complement.selective_preference = sp
        complement.save()

    if preference_type == 'g':    
        general = GeneralSelectivePreference.objects.get(id=int(preference_content))
        complement.selective_preference.generals.add(general)
        return ('g', general.id)
    elif preference_type == 's':
        synset = Synset.objects.get(id=int(preference_content))
        complement.selective_preference.synsets.add(synset)
        return ('s', synset.id)
    elif preference_type == 'r':
        relation = SelectivePreferenceRelations.objects.get(id=int(preference_content['relation']))
        argument = [int(a) for a in preference_content['to'].split(',')]
        frame = SemanticFrame.objects.get(id=frame_id)
        candidates = Complement.objects.filter(frame=frame)
        found = None
        for c in candidates:
            if len(c.roles.all()) == len(argument):
                roles = [r.id for r in c.roles.all()]
                ok = True
                for a in argument:
                    if a not in roles:
                        ok = False
                if ok:
                    found = c
                    break
        if found is not None:
            rsp = RelationalSelectivePreference(relation=relation, to=found)
            rsp.save()
            complement.selective_preference.relations.add(rsp)
            return ('r', rsp.id)
        else:
            return -1
    else:
        return -1

# remove_preference(frame_id, complement_id, preference)
def remove_preference(frame_id, complement_id, preference):
    preference_type, preference_id = preference
    if preference_type == 'g':
        complement = Complement.objects.get(id=complement_id)
        g = complement.selective_preference.generals.get(id = int(preference_id))
        complement.selective_preference.generals.remove(g)
    elif preference_type == 's':
        complement = Complement.objects.get(id=complement_id)
        s = complement.selective_preference.synsets.get(id = int(preference_id))
        complement.selective_preference.synsets.remove(s)
    elif preference_type == 'r':
        complement = Complement.objects.get(id=complement_id)
        r = complement.selective_preference.relations.get(id = int(preference_id))
        complement.selective_preference.relations.remove(r)
Bartłomiej Nitoń authored
284
def update_meanings(lemma_id, operations):
Bartłomiej Nitoń authored
285
    translation = {}
Bartłomiej Nitoń authored
286
    entry = Lemma.objects.get(id=lemma_id).entry_obj
Bartłomiej Nitoń authored
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    for operation in operations:
        if operation['operation'] == "set_glossa":
            if int(operation['unit']) in translation:
                unit = translation[int(operation['unit'])]
            else:
                unit = int(operation['unit'])
            set_glossa(unit, operation['value'])
        elif operation['operation'] == "add_example":
            if int(operation['unit']) in translation:
                unit = translation[int(operation['unit'])]
            else:
                unit = int(operation['unit'])
            add_example(unit, operation['example'])
        elif operation['operation'] == "remove_example": 
            if int(operation['unit']) in translation:
                unit = translation[int(operation['unit'])]
            else:
                unit = int(operation['unit'])
            remove_example(unit, operation['example'])
        elif operation['operation'] == "add_unit":
Bartłomiej Nitoń authored
307
            translation[operation['unit']['id']] = add_unit(entry, operation['unit'])
Bartłomiej Nitoń authored
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
        elif operation['operation'] == "remove_unit":
            luid = int(operation['luid'])
            if luid in translation:
                remove_unit(translation[luid])
            else:
                remove_unit(luid)
        else:
            pass

def set_glossa(unit_id, new_glossa):
    unit = LexicalUnit.objects.get(id=unit_id)
    unit.glossa = new_glossa
    unit.save()

def add_example(unit_id, example_id):
    unit = LexicalUnit.objects.get(id=unit_id)
    nkjp_example = NKJP_Example.objects.get(id=example_id)
    lue = LexicalUnitExamples(example=nkjp_example, lexical_unit=unit)
    lue.save()

def remove_example(unit_id, example_id):
    unit = LexicalUnit.objects.get(id=unit_id)
    nkjp_example = NKJP_Example.objects.get(id=example_id)
    lue = LexicalUnitExamples.objects.get(example=nkjp_example, lexical_unit=unit)
    lue.delete()
Bartłomiej Nitoń authored
334
def add_unit(entry, unit): # returns new id
Bartłomiej Nitoń authored
335
336
337

    s1 = Synset(id=(min(Synset.objects.all().aggregate(Min('id'))['id__min'], 0) - 1))
    s1.save()
Bartłomiej Nitoń authored
338
    lu = LexicalUnit(entry=entry, base=unit['base'], sense=unit['sense'], pos=unit['pos'], glossa=unit['glossa'], luid=-1, synset=s1)
Bartłomiej Nitoń authored
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    lu.save()


    if int(unit['relation']) == 1:
        s2 = Synset.objects.get(id=int(unit['to']))
        r = Synonymy(parent=s1, child=s2)
        r.save()
        r = Synonymy(parent=s2, child=s1)
        r.save()
    elif int(unit['relation']) == 0:
        s2 = Synset.objects.get(id=int(unit['to']))
        r = Hypernymy(parent=s1, child=s2)
        r.save()
    else:
        pass

    return lu.id

def remove_unit(luid):
    lu = LexicalUnit.objects.get(id=luid)
    if lu.luid is not None and lu.luid >= 0:
        return
    else:
        lu.delete()