convert_derivatives.py 2.71 KB
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
import itertools
from common.util import no_history
from dictionary.models import PartOfSpeech, Gender
from patterns.models import Pattern


class Command(BaseCommand):
    help = "Convert derivatives so they don't use verb patterns"

    def handle(self, *args, **options):
        convert_derivatives()


def convert_derivatives():
    no_history()
    pact = PartOfSpeech.objects.get(symbol='pact')
    ppas = PartOfSpeech.objects.get(symbol='ppas')
    appas = PartOfSpeech.objects.get(symbol='appas')
    ger = PartOfSpeech.objects.get(symbol='ger')
    n2 = Gender.objects.get(symbol='n2')
    for pos in (pact, ppas, appas):
        pos.lexical_class_id = 'adj'
        pos.save()
    ger.lexical_class_id = 'subst'
    ger.save()
    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')
    for l in pact.lexeme_set.all():
        for lip in l.lexemeinflectionpattern_set.all():
            lip.pattern = p07
            lip.root = l.get_root(lip.pattern)
            lip.save()
    for l in itertools.chain(ppas.lexeme_set.all(), appas.lexeme_set.all()):
        for lip in l.lexemeinflectionpattern_set.all():
            ends10 = lip.pattern.endings.filter(base_form_label__symbol='10')
            for end in ends10:
                if l.entry.endswith(end.string + 'y'):
                    end10 = end
                    break
            else:
                assert False
            end12 = lip.pattern.endings.get(base_form_label__symbol='12')
            # -ty/-ci
            if end10.string.endswith('t'):
                lip.pattern = p28
            # -iony/-eni
            elif end10.string.endswith('ion') and not end12.string.endswith('ieni'):
                lip.pattern = p20
            # -ony/-eni
            elif end12.string.endswith('eni'):
                lip.pattern = p19
            # -ny/-ni
            else:
                lip.pattern = p12
            lip.root = l.get_root(lip.pattern)
            lip.save()
    for l in ger.lexeme_set.filter(entry__endswith='nie'):
        for lip in l.lexemeinflectionpattern_set.all():
            lip.pattern = p0196
            lip.gender = n2
            lip.root = l.get_root(lip.pattern, n2)
            lip.save()
    for l in ger.lexeme_set.filter(entry__endswith='cie'):
        for lip in l.lexemeinflectionpattern_set.all():
            lip.pattern = p0195
            lip.gender = n2
            lip.root = l.get_root(lip.pattern, n2)
            lip.save()