Blame view

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

from common.js_to_obj import jsPosToObj
Bartłomiej Nitoń authored
4
from dictionary.models import Argument
Bartłomiej Nitoń authored
5
6
7
8
9
10
11
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 = []
Bartłomiej Nitoń authored
12
    frames = lemma.entry_obj.actual_frames()
Bartłomiej Nitoń authored
13
    for conv in schemata_conversions:
Bartłomiej Nitoń authored
14
15
        schema_operations = get_reconnect_operations_and_extend_connections(frames,
                                                                            connections,
Bartłomiej Nitoń authored
16
17
18
19
20
21
                                                                            conv['obj'], 
                                                                            conv['js'])
        operations.extend(schema_operations)
    operations.extend(get_disconnect_operations(frames, connections))
    return operations
Bartłomiej Nitoń authored
22
def get_reconnect_operations_and_extend_connections(frames, connections, schema, js_schema):
Bartłomiej Nitoń authored
23
    operations = []
Bartłomiej Nitoń authored
24
    used_poss_ids = [] 
Bartłomiej Nitoń authored
25
    for js_position in js_schema['positions']:
Bartłomiej Nitoń authored
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        if len(js_position['arguments']) > 0:
            position = get_position(schema, js_position, used_poss_ids)
            for js_phrase_type in js_position['arguments']:
                phrase_type = Argument.objects.get(text_rep=js_phrase_type['text_rep'])
                new_connection_target = {'schema': schema,
                                         'position': position,
                                         'phrase_type': phrase_type}
                for conn in js_phrase_type['connections']:
                    operations.extend(reconnect_operations(frames, 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']})
Bartłomiej Nitoń authored
42
43
    return operations
Bartłomiej Nitoń authored
44
45
46
def get_position(schema, js_position, used_poss_ids):
    position = jsPosToObj(js_position)
    same_poss = schema.positions.filter(text_rep=position.text_rep)
Bartłomiej Nitoń authored
47
    unused_same_poss = same_poss.exclude(id__in=used_poss_ids).order_by('id')
Bartłomiej Nitoń authored
48
49
50
51
    position = unused_same_poss[0]
    used_poss_ids.append(position.id)
    return position
Bartłomiej Nitoń authored
52
def reconnect_operations(frames, connection, new_target):
Bartłomiej Nitoń authored
53
54
    operations = []
    compl = Complement.objects.get(id=connection['compl'])
Bartłomiej Nitoń authored
55
56
    frame = frames.get(complements=compl)
    arg_ref = create_argument_ref(frame, compl)
Bartłomiej Nitoń authored
57
58
59
60
61
62
    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)
        new_phrase_type_ref = create_phrase_type_ref(new_target['schema'], new_target['position'],
                                                     new_target['phrase_type'], realization.alternation)
Bartłomiej Nitoń authored
63
64
65
        if new_phrase_type_ref != old_phrase_type_ref:
            operations.append(create_operation('disconnect', arg_ref, old_phrase_type_ref))
            operations.append(create_operation('connect', arg_ref, new_phrase_type_ref))
Bartłomiej Nitoń authored
66
67
    return operations
Bartłomiej Nitoń authored
68
69
def create_argument_ref(frame, complement):
    return 'frame_%d_comp_%d_' % (frame.id, complement.id)
Bartłomiej Nitoń authored
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

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():
Bartłomiej Nitoń authored
85
                if not conn_dict or not real.id in conn_dict['realizations']:
Bartłomiej Nitoń authored
86
87
                    phrase_type_ref = create_phrase_type_ref(real.frame, real.position,
                                                             real.argument, real.alternation)
Bartłomiej Nitoń authored
88
                    arg_ref = create_argument_ref(frame, compl)
Bartłomiej Nitoń authored
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
                    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}
Bartłomiej Nitoń authored
111
112
113
114
def disconnect_example_operation(example_dict, example_obj):
    lu = LexicalUnit.objects.get(id=example_dict['lexical_unit'])
    return {'operation': 'remove_example', 'unit': lu.id, 'example': example_obj.id}
Bartłomiej Nitoń authored
115
116
117
def reconnect_examples(operations):
    update_meanings(operations)