js_to_obj.py
7.57 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
# -*- coding: utf-8 -*-
#Copyright (c) 2014, Bartłomiej Nitoń
#All rights reserved.
#Redistribution and use in source and binary forms, with or without modification, are permitted provided
#that the following conditions are met:
# Redistributions of source code must retain the above copyright notice, this list of conditions and
# the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions
# and the following disclaimer in the documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from dictionary.common_func import sortArguments, sortPositions
from dictionary.models import Argument, Frame, Frame_Char_Model, \
Frame_Characteristic, Position, PositionCategory, \
sortFrameChars, sortPosCatsAsStrTab
def jsFrameToObj(frame, lemma_entry):
# szukanie charakterystyk schematu
frame_characteristic_objs = []
frame_char_models = Frame_Char_Model.objects.order_by('priority')
for i in range(len(frame_char_models)):
frame_char_value = frame_char_models[i].frame_char_values.get(value=frame['characteristics'][i])
frame_char_type = frame_char_models[i].model_name
try:
frame_char_obj = Frame_Characteristic.objects.get(type=frame_char_type, value=frame_char_value)
except Frame_Characteristic.DoesNotExist:
frame_char_obj = Frame_Characteristic(type=frame_char_type, value=frame_char_value)
frame_char_obj.save()
frame_characteristic_objs.append(frame_char_obj)
positions_objs = []
for position in frame['positions']:
if len(position['arguments']) > 0:
pos_obj = jsPosToObj(position)
positions_objs.append(pos_obj)
position['id'] = pos_obj.id
sorted_positions = []
sorted_pos_dict = sortPositions(positions_objs)
last_pos_dict = None
for pos_dict in sorted_pos_dict:
sorted_positions.append(pos_dict['position'])
sort_pos_str_tab = []
for sort_pos in sorted_positions:
sort_pos_str_tab.append(sort_pos.text_rep)
text_rep = u'%s:%s' % (':'.join(frame['characteristics']),
'+'.join(sort_pos_str_tab))
try:
frame_obj = Frame.objects.get(text_rep=text_rep)
except Frame.DoesNotExist:
frame_obj = Frame(text_rep=text_rep)
frame_obj.save()
last_pos_obj = None
pos_obj_count = 0
for pos_obj in sorted_positions:
same_pos_db = Position.objects.filter(text_rep=pos_obj.text_rep).order_by('id')
if not last_pos_obj or last_pos_obj.text_rep != pos_obj.text_rep:
pos_obj_count = 1
frame_obj.positions.add(same_pos_db[0])
else:
pos_obj_count = pos_obj_count + 1
if pos_obj_count <= len(same_pos_db):
same_pos_obj = same_pos_db[pos_obj_count-1]
frame_obj.positions.add(same_pos_obj)
else:
same_pos_obj = Position(text_rep=pos_obj.text_rep)
same_pos_obj.save()
for category in pos_obj.categories.all():
same_pos_obj.categories.add(category)
for arg in pos_obj.arguments.all():
same_pos_obj.arguments.add(arg)
frame_obj.positions.add(same_pos_obj)
last_pos_obj = pos_obj
for frame_char in frame_characteristic_objs:
frame_obj.characteristics.add(frame_char)
if frame_obj.has_phraseologic_arguments():
frame_obj.phraseologic = True
frame_obj.save()
return frame_obj
def jsPosToObj(position, pos_idx=0):
categories = []
for category in position['categories']:
categories.append(category)
categories = sortPosCatsAsStrTab(categories)
arguments_objs = []
for arg in position['arguments']:
arguments_objs.append(jsArgToObj(arg))
argument_objs = sortArguments(arguments_objs)
args_str_tab = []
last_arg_obj = None
for arg_obj in argument_objs:
if not last_arg_obj or last_arg_obj.text_rep != arg_obj.text_rep:
args_str_tab.append(arg_obj.text_rep)
last_arg_obj = arg_obj
pos_text_rep = '%s{%s}' % (','.join(categories),';'.join(args_str_tab))
same_positions = Position.objects.filter(text_rep=pos_text_rep)
if len(same_positions) > 0:
pos_obj = same_positions[pos_idx]
else:
pos_obj = Position(text_rep=pos_text_rep)
pos_obj.save()
for category in categories:
category_obj = PositionCategory.objects.get(category=category)
pos_obj.categories.add(category_obj)
for arg_obj in argument_objs:
pos_obj.arguments.add(arg_obj)
return pos_obj
def jsArgToObj(argument):
arg_obj = Argument.objects.get(text_rep=argument['text_rep'])
return arg_obj
def frameObjToSerializableDict(lemma, frame, with_connections=False):
frame_opinion = ''
frame_opinions_tab = lemma.frame_opinions.filter(frame__text_rep=frame.text_rep)
if frame_opinions_tab:
frame_opinion = frame_opinions_tab[0].value.value
frame_dict = { 'id' : frame.id,
'text_rep' : frame.text_rep,
'characteristics' : [],
'positions' : [],
'opinion' : frame_opinion,
'error' : False,
'tooltip' : '',
'is_phraseologic' : frame.phraseologic}
sorted_pos_dict = sortPositions(frame.positions.all())
for position in sorted_pos_dict:
categories_str_tab = []
for category in position['position'].categories.all():
categories_str_tab.append(category.category)
categories_str_tab = sortPosCatsAsStrTab(categories_str_tab)
position_dict = {'id' : position['position'].id,
'text_rep' : position['position'].text_rep,
'categories': categories_str_tab,
'arguments' : [],
'error' : False,
'tooltip' : ''}
for argument in position['arguments']:
connections = []
if with_connections:
entry = lemma.entry_obj
connections = entry.matching_connections(frame, position['position'], argument)
argument_dict = { 'id' : argument.id,
'text_rep': argument.text_rep,
'type' : argument.type,
'error' : False,
'tooltip' : '',
'connections': connections}
position_dict['arguments'].append(argument_dict)
frame_dict['positions'].append(position_dict)
frame_char_objs = sortFrameChars(frame.characteristics.all())
for char in frame_char_objs:
frame_dict['characteristics'].append(char.value.value)
return frame_dict