util.py
1.78 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
# -*- coding: utf-8 -*-
from dictionary.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