views.py
7.69 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
import datetime
from collections import defaultdict
from itertools import chain
from django.http import JsonResponse
from django.shortcuts import render
from connections.models import Entry, Subentry, ArgumentConnection
from syntax.models import NaturalLanguageDescription, Schema
from semantics.models import Frame
from .forms import FiltersForm
from .polish_strings import SCHEMA_OPINION, FRAME_OPINION
from .phrase_descriptions.descriptions import position_prop_description, phrase_description
def test(request):
return render(request, 'test.html', { 'filters_form' : FiltersForm() })
def entries(request):
return render(request, 'entries.html', { 'filters_form' : FiltersForm() })
#TODO clean this code bordello up
def filter_objects(objects, queries):
print('===================================================================')
for query in queries:
print('***', query)
objects = objects.filter(query).distinct()
print('---------------------------------------------------------------')
print(objects)
print('\n')
print('===================================================================')
return objects.distinct()
# TODO a more efficient loading of results will be implemented,
# for now truncate them to speed up testing
DEBUG_N = 100
def get_entries(request):
t1 = datetime.datetime.now()
if request.method == 'GET':
form = FiltersForm(request.GET)
if not form.is_valid():
print(form.errors)
return JsonResponse({ 'result' : [], 'errors' : form.errors })
if form.is_valid():
queries = form.get_queries()
t2 = datetime.datetime.now()
entries = filter_objects(Entry.objects.all(), queries)
# TODO remove! – this is for debug
# some entries with an obj,controlee position, >3 frames etc.
#entries = entries.filter(name__in=('dozwalać', 'dozwolić', 'obiecywać'))
entries = entries.filter(subentries__schemata__positions__phrase_types__text_rep__contains='lex')
t3 = datetime.datetime.now()
#entries_list = [(e.id, e.name, e.status.key) for e in entries]#[:500]]
entries_list = list(entries.values_list('id', 'name', 'status__key'))[:DEBUG_N]
t4 = datetime.datetime.now()
s = ' truncated to {}'.format(DEBUG_N) if len(entries_list) == DEBUG_N else ''
debug = '[{} s] [{} results{}] '.format(t2, entries.count(), s)
debug += '[filtering: {:.1} s] '.format((t2 - t1).total_seconds())
debug += '[processing: {:.1} s] '.format((t4 - t3).total_seconds())
debug += '[total time: {:.1} s] '.format((t4 - t1).total_seconds())
return JsonResponse({ 'result' : entries_list, 'debug' : debug })
return JsonResponse({})
def subentry2str(subentry):
ret = subentry.entry.name
if subentry.inherent_sie.name == 'true':
ret += ' się'
elems = []
if subentry.aspect:
elems.append(subentry.aspect.name)
if subentry.negativity:
elems.append(subentry.negativity.name)
if elems:
ret += ' ({})'.format(', '.join(elems))
if subentry.predicativity.name == 'true':
ret += ' pred.'
return ret
def position_prop2dict(prop):
return {
'str' : prop.name,
'desc' : position_prop_description(prop.name),
} if prop else {
'str' : '',
'desc' : '',
}
def get_phrase_desc(phrase, position, negativity):
print('***---***')
print(phrase.text_rep)
print(position.function, position.control, position.pred_control)
print(negativity)
for d in NaturalLanguageDescription.objects.filter(
phrase_str=phrase.text_rep,
#function=position.function,
#control=position.control,
#pred_control=position.pred_control,
#negativity=negativity,
):
print('---------')
print(d.phrase_str)
print(d.function, d.control, d.pred_control)
print(d.negativity)
return NaturalLanguageDescription.objects.get(
phrase_str=phrase.text_rep,
function=position.function,
control=position.control,
pred_control=position.pred_control,
negativity=negativity).description
def schema2dict(schema, phr2arg, negativity):
return {
'opinion' : SCHEMA_OPINION[schema.opinion.key],
'id' : str(schema.id),
'positions' : [
{
'func' : position_prop2dict(p.function),
'control' : position_prop2dict(p.control),
'p_control' : position_prop2dict(p.pred_control),
'phrases' : [
{
'str' : str(pt),
'id' : '-'.join(map(str, (schema.id, p.id, pt.id))),
'arguments' : list(phr2arg['-'.join(map(str, (schema.id, p.id, pt.id)))]),
'desc' : get_phrase_desc(pt, p, negativity),
} for pt in p.phrase_types.all()
],
} for p in schema.positions.all()
],
}
def get_prefs_list(argument):
prefs = [argument.predefined.all(), argument.synsets.all(), argument.relations.all()]
return list(chain(*map(list, prefs)))
def frame2dict(frame, arg2phr):
return {
'opinion' : FRAME_OPINION[frame.opinion.key],
'id' : str(frame.id),
'arguments' : [
{
'str' : str(a),
'id' : '-'.join(map(str, (frame.id, a.id))),
'preferences' : list(map(str, get_prefs_list(a))),
'phrases' : list(arg2phr['-'.join(map(str, (frame.id, a.id)))]),
} for a in frame.argument_set.all()
],
}
def get_entry(request):
if request.method == 'GET':
form = FiltersForm(request.GET)
eid = request.GET['entry']
if eid.isdigit() and form.is_valid():
eid = int(eid)
entry = Entry.objects.get(id=eid)
#===================================================================
phr2arg, arg2phr = defaultdict(set), defaultdict(set)
connections = ArgumentConnection.objects.filter(schema_connections__subentry__entry=entry)
for conn in connections:
arg = '-'.join(map(str, (conn.argument.frame.id, conn.argument.id)))
for hook in conn.schema_connections.all():
phr = '-'.join(map(str, (hook.schema.id, hook.position.id, hook.phrase_type.id)))
phr2arg[phr].add(arg)
arg2phr[arg].add(phr)
#===================================================================
schema_queries = []
if form.cleaned_data['filter_schema_']:
schema_queries = form.get_queries('schema')
frame_queries = []
if form.cleaned_data['filter_frame_']:
frame_queries = form.get_queries('frame')
subentries = []
for subentry in entry.subentries.all():
schemata = []
for schema in filter_objects(subentry.schemata.all(), schema_queries):
schemata.append(schema2dict(schema, phr2arg, subentry.negativity))
if schemata:
subentries.append({ 'str' : subentry2str(subentry), 'schemata' : schemata })
frame_objs = Frame.objects.filter(argument__argument_connections__schema_connections__subentry__entry=entry)
frames = [frame2dict(frame, arg2phr) for frame in filter_objects(frame_objs, frame_queries)]
return JsonResponse({ 'subentries' : subentries, 'frames' : frames })
return JsonResponse({})