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

from common.decorators import render, ajax, AjaxError
from common.util import suffix, cut_end
from dictionary.models import Lexeme, LexemeInflectionPattern, Pattern, \
  InflectionCharacteristic, prepare_table, visible_vocabularies, get_root, \
  ClassificationValue, Classification
from dictionary.pattern_blacklist import blacklist

commonness = Classification.objects.get(name=u'pospolitość')

LIP_ROWS = 10

def make_list(vocabularies, entry, pos, ic, cvs, bl_check):
  lips = LexemeInflectionPattern.objects.filter(
    lexeme__deleted=False, lexeme__vocabularies__in=vocabularies).distinct()
  lips = lips.filter(
    lexeme__part_of_speech__symbol=pos).exclude(lexeme__status='cand')
  if ic:
    lips = lips.filter(inflection_characteristic=ic)
  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, ic0 in bad_inflections:
      suf_lips.exclude(pattern=p0, inflection_characteristic=ic0)
    for p0, ic0 in feature_sets:
      suf_lips = suf_lips.exclude(
        pattern=p0, inflection_characteristic=ic0)
    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 ic:
        l_ic = ic
      else:
        l_ic = lip.inflection_characteristic
      if (p, l_ic) in bad_inflections: continue
      if (p, l_ic) 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, pos, p, l_ic) is not None:
        l_root = lip.lexeme.get_root(p, l_ic)
        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_ic))
        if len(chosen_lips) == LIP_ROWS:
          break
      else:
        bad_inflections.add((p, l_ic))
    if len(chosen_lips) == LIP_ROWS:
      break
  return chosen_lips

@render()
@ajax(method='get', encode_result=False)
def prompter_list(request, entry, pos_id, ic_id, commonness_ids,
                  ic_check, cv_check, bl_check):
  vocabularies = visible_vocabularies(request.user)
  if ic_check:
    ic = InflectionCharacteristic.objects.get(pk=ic_id)
  else:
    ic = None
  if cv_check: # and commonness not in ('undefined', 'None'):
    cvs = ClassificationValue.objects.filter(pk__in=commonness_ids)
  else:
    cvs = None
  lips = make_list(vocabularies, entry, pos_id, ic, cvs, bl_check)
  # zakładamy, że symbol == pk
  return {'lips': lips}