ajax_prompter.py
3.05 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
# -*- coding: utf-8 -*-
from common.decorators import ajax
from common.util import suffix, cut_end
from dictionary.models import LexemeInflectionPattern, \
ClassificationValue, Classification, Gender, Lexeme
from patterns.pattern_blacklist import blacklist
commonness = Classification.objects.get(name=u'pospolitość')
LIP_ROWS = 10
def make_list(user, entry, pos, gender, cvs, bl_check):
lips = LexemeInflectionPattern.objects.distinct()
lips = LexemeInflectionPattern.filter_visible(lips, user)
lips = lips.filter(
lexeme__part_of_speech__symbol=pos).exclude(
lexeme__status__in=Lexeme.HIDDEN_STATUSES)
if gender:
lips = lips.filter(gender=gender)
if cvs:
lips = lips.filter(lexeme__classificationvalue__in=cvs)
lips = lips.select_related('pattern', 'gender', 'lexeme')
lips = lips.order_by('lexeme__entry')
feature_sets = set()
bad_inflections = set()
chosen_lips = []
for suf_len in xrange(len(entry), 0, -1):
suf = suffix(entry, suf_len)
suf_lips = lips.filter(lexeme__entry__endswith=suf)
if suf_len < len(entry):
suf1 = suffix(entry, suf_len + 1)
suf_lips = suf_lips.exclude(lexeme__entry__endswith=suf1)
for p0, gender0 in bad_inflections:
suf_lips.exclude(pattern=p0, gender=gender0)
for p0, gender0 in feature_sets:
suf_lips = suf_lips.exclude(pattern=p0, gender=gender0)
for lip in suf_lips:
p = lip.pattern
l_gender = lip.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 = lip.lexeme.classification_values(commonness)
l_root = lip.lexeme.get_root(p, l_gender)
l_end = lip.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_lips.append((lip, l_cvs, cut_end(l_entry, suf), suf))
feature_sets.add((p, l_gender))
if len(chosen_lips) == LIP_ROWS:
break
else:
bad_inflections.add((p, l_gender))
if len(chosen_lips) == LIP_ROWS:
break
return chosen_lips
@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
lips = make_list(request.user, entry, pos_id, gender, cvs, bl_check)
# zakładamy, że symbol == pk
return {'lips': lips}