util.py
3.95 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
# -*- coding: utf-8 -*-
from dictionary.models import Gender, Inflection, Lexeme, LexemeAttribute
from dictionary.util import make_filter_url
def pattern_filter_rule(pattern):
return {
'field': 'pattern_name',
'op': 'eq',
'data': pattern.name
}
def value_filter_rule(value):
return {
'field': 'extra-%s' % value.attribute.id,
'op': 'in',
'data': [str(value.id)],
}
def get_detailed_counts(pattern, inflections, filter_urls=None):
"""
:param pattern:
:param inflections:
:param filter_urls: possible values: None, 'reader', 'edit'
"""
if pattern.type.inflection_type_id == 'subst':
for gender in Gender.objects.all():
gender_count = Inflection.lexeme_count(
inflections.filter(gender=gender))
if gender_count > 0:
example = pattern.get_example(gender)
if example:
example_qs = Lexeme.objects.filter(id=example[0].id)
if not Lexeme.filter_reader(example_qs):
example = pattern.get_example(gender, refresh=True)
if filter_urls:
filter_url = make_filter_url([
pattern_filter_rule(pattern),
{
'field': 'gender',
'op': 'in',
'data': [str(gender.id)],
}
], filter_urls == 'reader')
else:
filter_url = None
else:
filter_url = None
if filter_urls:
yield gender.symbol, gender_count, example, filter_url, ()
else:
yield gender.symbol, gender_count, example
elif pattern.type.inflection_type_id == 'v':
aspect_attr = LexemeAttribute.objects.get(name='aspekt')
quasi_attr = LexemeAttribute.objects.get(name='właściwy')
quasi_value = quasi_attr.values.get(value='Q')
aspects = aspect_attr.values.all()
lexeme_ids = list(
inflections.values_list('lexeme', flat=True).order_by().distinct())
lexemes = Lexeme.objects.filter(id__in=lexeme_ids)
for aspect in aspects:
aspect_lexemes = lexemes.filter(lexemeattributevalue=aspect)
aspect_count = len(aspect_lexemes)
if aspect_count > 0:
example_lexeme = aspect_lexemes.first()
example_root = example_lexeme.inflection_set.filter(
pattern=pattern).first().root
example = (example_lexeme, example_root)
quasi_lexemes = aspect_lexemes.filter(
lexemeattributevalue=quasi_value)
quasi_count = len(quasi_lexemes)
if quasi_count > 0:
quasi_lexeme = quasi_lexemes.first()
quasi_root = quasi_lexeme.inflection_set.filter(
pattern=pattern).first().root
quasi_example = (quasi_lexeme, quasi_root)
else:
quasi_example = None
if filter_urls:
filter_url = make_filter_url([
pattern_filter_rule(pattern),
value_filter_rule(aspect),
], filter_urls == 'reader')
quasi_filter_url = make_filter_url([
pattern_filter_rule(pattern),
value_filter_rule(aspect),
value_filter_rule(quasi_value),
], filter_urls == 'reader')
extras = (quasi_count, quasi_example, quasi_filter_url)
yield (aspect.value, aspect_count, example,
filter_url, extras)
else:
yield aspect.value, aspect_count, example
else:
return