ajax_prompter.py
3.34 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
# -*- coding: utf-8 -*-
from common.decorators import ajax
from common.util import suffix, cut_end
from dictionary.models import Inflection, ClassificationValue, Classification, \
Gender, Lexeme
from patterns.pattern_blacklist import blacklist
commonness = Classification.objects.get(name=u'pospolitość')
INFLECTION_ROWS = 10
def make_list(user, entry, pos, gender, cvs, bl_check):
inflections = Inflection.objects.distinct()
inflections = Inflection.filter_visible(inflections, user)
inflections = inflections.filter(
lexeme__part_of_speech__symbol=pos).exclude(
lexeme__status__in=Lexeme.HIDDEN_STATUSES)
if gender:
inflections = inflections.filter(gender=gender)
if cvs:
inflections = inflections.filter(lexeme__classificationvalue__in=cvs)
inflections = inflections.select_related('pattern', 'gender', 'lexeme')
inflections = inflections.order_by('lexeme__entry')
feature_sets = set()
bad_inflections = set()
chosen_inflections = []
for suf_len in xrange(len(entry), 0, -1):
suf = suffix(entry, suf_len)
suf_inflections = inflections.filter(lexeme__entry__endswith=suf)
if suf_len < len(entry):
suf1 = suffix(entry, suf_len + 1)
suf_inflections = suf_inflections.exclude(
lexeme__entry__endswith=suf1)
for p0, gender0 in bad_inflections:
suf_inflections.exclude(pattern=p0, gender=gender0)
for p0, gender0 in feature_sets:
suf_inflections = suf_inflections.exclude(
pattern=p0, gender=gender0)
for inflection in suf_inflections:
p = inflection.pattern
l_gender = inflection.gender
if p.name in blacklist and bl_check:
continue
if (p, l_gender) in bad_inflections:
continue
if (p, l_gender) in feature_sets:
continue
if p.get_root(entry, l_gender) is not None:
l_cvs = inflection.lexeme.classification_values(commonness)
l_root = inflection.lexeme.get_root(p, l_gender)
l_end = inflection.lexeme.entry[len(l_root):]
l_entry = u'%s·%s' % (l_root, l_end)
if len(l_end) < len(suf):
suf = suffix(l_entry, suf_len + 1)
chosen_inflections.append(
(inflection, l_cvs, cut_end(l_entry, suf), suf))
feature_sets.add((p, l_gender))
if len(chosen_inflections) == INFLECTION_ROWS:
break
else:
bad_inflections.add((p, l_gender))
if len(chosen_inflections) == INFLECTION_ROWS:
break
return chosen_inflections
@ajax(method='get', template='prompter_list.html',
permission_required='dictionary.change_lexeme')
def prompter_list(request, entry, pos_id, commonness_ids,
cv_check, bl_check, gender_check=None, gender_id=None):
gender = Gender.objects.get(pk=gender_id) if gender_check else None
if cv_check: # and commonness not in ('undefined', 'None'):
cvs = list(ClassificationValue.objects.filter(pk__in=commonness_ids))
else:
cvs = None
inflections = make_list(request.user, entry, pos_id, gender, cvs, bl_check)
# zakładamy, że symbol == pk
return {'inflections': inflections}