ajax_prompter.py 3.05 KB
# -*- 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' % (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}