util.py
3.2 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
# -*- coding: utf-8 -*-
from common.decorators import AjaxError
from patterns.models import BaseFormLabel
def expand_tag(tag):
if '+' in tag:
tags = tag.split('+')
expanded = []
for tag in tags:
expanded += expand_tag(tag)
return expanded
parts = tag.split(':')
variants_list = []
for part in parts:
variants_list.append(part.split('.'))
# return [':'.join(parts) for parts in expand_variants(variants_list)]
return expand_variants(variants_list)
def expand_variants(variants_list):
if not variants_list:
return [()]
expanded = []
tails = expand_variants(variants_list[1:])
for variant in variants_list[0]:
expanded += [(variant,) + tail for tail in tails]
return expanded
def compare_patterns(patterns):
pattern0, root0 = patterns[0]
bfl_pks0 = set(pattern0.endings.values_list('base_form_label', flat=True))
differing_bfls = set()
forms = dict((p, dict(
(bfl, root + e)
for bfl, e in p.endings.values_list('base_form_label', 'string')))
for p, root in patterns)
for pattern, root in patterns[1:]:
bfl_pks = set(pattern.endings.values_list('base_form_label', flat=True))
for pk in bfl_pks ^ bfl_pks0: # różnica symetryczna
differing_bfls.add(pk)
for ending in pattern.endings.all():
if ending.base_form_label.pk not in differing_bfls:
if root + ending.string != forms[pattern0][
ending.base_form_label.pk]:
differing_bfls.add(ending.base_form_label.pk)
differing_bfls = list(BaseFormLabel.objects.filter(pk__in=differing_bfls))
diffs = dict((pattern, [
(bfl, forms[pattern].get(bfl.pk))
for bfl in differing_bfls])
for pattern, root in patterns)
return diffs
def check_query_params(request, query_params):
reader = query_params.get('reader', True)
if not request.user.is_authenticated() and not reader:
raise AjaxError('access denied')
def prepare_table(table):
for row in table:
for cell in row:
if type(cell) == dict and 'forms' in cell:
cell['forms'].sort()
seen_forms = []
unique_forms = []
form_patterns = {}
for form in cell['forms']:
if form[1] not in seen_forms:
seen_forms.append(form[1])
unique_forms.append(form)
form_patterns[form[1]] = set()
form_patterns[form[1]].add(form[3])
cell['forms'] = [
{
'form': form,
'qualifiers': qualifiers,
'patterns': form_patterns[form],
}
for (key, form, qualifiers, pattern) in unique_forms]
elif type(cell) == dict and 'label' in cell:
seen_labels = []
def is_new(label):
new = label not in seen_labels
seen_labels.append(label)
return new
cell['label'] = filter(is_new, cell['label'])
return table