auto_derivatives.py 3.41 KB
# -*- coding: utf-8 -*-
from django.db.models import Max
from dictionary.models import Ending, Lexeme, LexemeInflectionPattern, \
    Pattern, Gender

P07 = Pattern.objects.get(name='P07')
P28 = Pattern.objects.get(name='P28')
P12 = Pattern.objects.get(name='P12')
P19 = Pattern.objects.get(name='P19')
P20 = Pattern.objects.get(name='P20')
P0196 = Pattern.objects.get(name='0196')
P0195 = Pattern.objects.get(name='0195')
n2 = Gender.objects.get(symbol='n2')

def lexeme_derivatives(lexeme):
    if lexeme.part_of_speech.symbol == 'v':
        proper = lexeme.lexemeattributevalue_set.filter(
            attribute__name=u'właściwy', value__in=('', '(Q)'))
        if proper:
            trans = lexeme.lexemeattributevalue_set.filter(
                attribute__name=u'przechodniość', value='T')
            q_trans = lexeme.lexemeattributevalue_set.filter(
                attribute__name=u'przechodniość', value='qT')
            imperf = lexeme.lexemeattributevalue_set.filter(
                attribute__name=u'aspekt').exclude(value='dk')
            lips = list(lexeme.lexemeinflectionpattern_set.all())
            for lip in lips:
                pattern = lip.pattern
                endings3 = Ending.objects.filter(
                    pattern=pattern, base_form_label__symbol='3')
                endings10 = Ending.objects.filter(
                    pattern=pattern, base_form_label__symbol='10')
                endings11 = Ending.objects.filter(
                    pattern=pattern, base_form_label__symbol='11')
                endings12 = Ending.objects.filter(
                    pattern=pattern, base_form_label__symbol='12')
                for ending in endings11:
                    yield ('ger', lip.root + ending.string + 'ie')
                if trans or q_trans:
                    pos = 'ppas' if trans else 'appas'
                    for ending in endings10:
                        for ending12 in endings12:
                            yield (pos, lip.root + ending.string + 'y',
                                   lip.root + ending12.string)
                if imperf:
                    for ending in endings3:
                        yield ('pact', lip.root + ending.string + 'cy')


def create_derivative(lexeme, part_of_speech, entry, pl=None):
    next_id = Lexeme.objects.aggregate(Max('id'))['id__max'] + 1
    der = Lexeme(
        id=next_id, entry=entry, part_of_speech_id=part_of_speech,
        status=lexeme.status, owner_vocabulary=lexeme.owner_vocabulary)
    der.fix_homonym_number()
    der.save()
    lexeme.owner_vocabulary.add_lexeme(der)
    lip = LexemeInflectionPattern(lexeme=der)
    if part_of_speech in ('ppas', 'appas'):
        # -ty/-ci
        if entry.endswith('ty'):
            lip.pattern = P28
        # -iony/-eni
        elif entry.endswith('iony') and not pl.endswith('ieni'):
            lip.pattern = P20
        # -ony/-eni
        elif entry.endswith('eni'):
            lip.pattern = P19
        # -ny/-ni
        else:
            lip.pattern = P12
    elif part_of_speech == 'pact':
        lip.pattern = P07
    elif part_of_speech == 'ger':
        lip.gender = n2
        if entry.endswith('cie'):
            lip.pattern = P0195
        else: # -nie
            lip.pattern = P0196
    lip.root = lip.get_root()
    lip.save()
    # skopiować atrybuty, które mają sens
    # imiesłowy -> dać nieobecne opcjonalne formy
    # skopiować kwalifikatory?
    # odsyłacze w obie strony
    pass