saving.py
4.88 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
# -*- coding: utf-8 -*-
from common.js_to_obj import jsPosToObj
from dictionary.models import Argument
from semantics.models import Complement, LexicalUnitExamples
from semantics.saving import modify_frames, update_meanings
from wordnet.models import LexicalUnit
def get_semantic_operations(lemma, schemata_conversions):
connections = []
operations = []
for conv in schemata_conversions:
schema_operations = get_reconnect_operations_and_extend_connections(connections,
conv['obj'],
conv['js'])
operations.extend(schema_operations)
frames = lemma.entry_obj.actual_frames()
operations.extend(get_disconnect_operations(frames, connections))
return operations
def get_reconnect_operations_and_extend_connections(connections, schema, js_schema):
operations = []
for js_position in js_schema['positions']:
position = jsPosToObj(js_position)
for js_phrase_type in js_position['arguments']:
phrase_type = Argument.objects.get(id=js_phrase_type['id'])
new_connection_target = {'schema': schema,
'position': position,
'phrase_type': phrase_type}
for conn in js_phrase_type['connections']:
operations.extend(reconnect_operations(conn, new_connection_target))
conn_dict = next((conn_dict
for conn_dict in connections if conn_dict['compl'] == conn['compl']), None)
if conn_dict:
conn_dict['realizations'].extend(conn['realizations'])
else:
connections.append({'compl': conn['compl'],
'realizations': conn['realizations']})
return operations
def reconnect_operations(connection, new_target):
operations = []
compl = Complement.objects.get(id=connection['compl'])
arg_ref = create_argument_ref(compl)
for real_id in connection['realizations']:
realization = compl.realizations.get(id=real_id)
old_phrase_type_ref = create_phrase_type_ref(realization.frame, realization.position,
realization.argument, realization.alternation)
operations.append(create_operation('disconnect', arg_ref, old_phrase_type_ref))
new_phrase_type_ref = create_phrase_type_ref(new_target['schema'], new_target['position'],
new_target['phrase_type'], realization.alternation)
operations.append(create_operation('connect', arg_ref, new_phrase_type_ref))
return operations
def create_argument_ref(complement):
connected_frame = Complement.objects.get(id=complement.id)
return 'frame_%d_comp_%d_' % (connected_frame.id, complement.id)
def create_phrase_type_ref(schema, position, phrase_type, alternation):
return 'schema_%d_pos_%d_arg_%d_alt_%d_' % (schema.id, position.id,
phrase_type.id, alternation)
def create_operation(operation, arg_ref, phrase_type_ref):
return {'operation': operation, 'arg': arg_ref, 'connect': phrase_type_ref}
def get_disconnect_operations(frames, connections):
operations = []
for frame in frames:
for compl in frame.complements.all():
conn_dict = next((conn_dict
for conn_dict in connections if conn_dict['compl'] == compl.id), None)
for real in compl.realizations.all():
if not real.id in conn_dict['realizations']:
phrase_type_ref = create_phrase_type_ref(real.frame, real.position,
real.argument, real.alternation)
arg_ref = create_argument_ref(compl)
operations.append(create_operation('disconnect', arg_ref, phrase_type_ref))
return operations
def update_connections(lemma_id, reconnect_operations, user):
modify_frames(lemma_id, reconnect_operations, user)
def disconnect_all_examples_operations(lemma):
operations = []
lex_units = lemma.entry_obj.lexical_units().all()
for lu in lex_units:
lu_examples = LexicalUnitExamples.objects.filter(lexical_unit=lu)
for lu_ex in lu_examples:
example = lu_ex.example
operations.append({'operation': 'remove_example',
'unit': lu.id,
'example': example.id})
return operations
def connect_example_operation(example_dict, example_obj):
lu = LexicalUnit.objects.get(id=example_dict['lexical_unit'])
return {'operation': 'add_example', 'unit': lu.id, 'example': example_obj.id}
def reconnect_examples(operations):
update_meanings(operations)