ajax_prompter.py 3.23 KB
#-*- coding:utf-8 -*-

from common.decorators import ajax
from common.util import suffix, cut_end
from dictionary.models import LexemeInflectionPattern, get_root, \
    ClassificationValue, Classification, filter_visible_lips, Gender, Lexeme
from dictionary.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 = filter_visible_lips(lips, user)
    lips = lips.filter(
        lexeme__part_of_speech__symbol=pos).exclude(
        lexeme__status=Lexeme.STATUS_CANDIDATE)
    if gender:
        lips = lips.filter(gender=gender)
    if cvs:
        lips = lips.filter(lexeme__classificationvalue__in=cvs)
    else:
        lips = lips.exclude(lexeme__classificationvalue=None)
    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:
            #lip = suf_lips[0]
            p = lip.pattern
            #suf_lips = suf_lips.exclude(pattern=p)
            if p.name in blacklist and bl_check: continue
            if gender:
                l_gender = gender
            else:
                l_gender = lip.gender
            if (p, l_gender) in bad_inflections: continue
            if (p, l_gender) in feature_sets: continue
            if cvs:
                l_cvs = lip.lexeme.classification_values(commonness) & cvs
            else:
                l_cvs = lip.lexeme.classification_values(commonness)
            if get_root(entry, p, l_gender) is not None:
                l_root = lip.lexeme.get_root(p, l_gender)
                l_end = lip.lexeme.entry[len(l_root):]
                l_entry = u'%%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')
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 = 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}