fix_osc.py 2.51 KB
#-*- coding:utf-8 -*-

import sys
from django.core.management.base import BaseCommand, CommandError
from common.util import no_history, debug
from dictionary.models import Lexeme, Vocabulary, LexemeAssociation, \
                              PartOfSpeech, CrossReference

class Command(BaseCommand):
  args = 'none'
  help = 'Fixes osc'

  def handle(self, **options):
    fix_osc()

# aktualnie łączymy tylko jeśli razem z -ością przyszedł odpowiedni przymiotnik
# lub wersja niezanegowana (dla zanegowanych)

def fix_osc():
  no_history()
  morfologik = Vocabulary.objects.get(id='Morfologik')
  morf = morfologik.owned_lexemes_pk()
  existing = Lexeme.objects.filter(deleted=False)
  morf_osc = existing.filter(
    pk__in=morf, part_of_speech__symbol='subst', entry__endswith=u'ość')
  for lexeme in morf_osc:
    if lexeme.entry.endswith(u'jość'):
      base = lexeme.entry[:-4]
    else:
      base = lexeme.entry[:-3]
    options = (base + 'i', base + 'y', base)
    adjs = existing.filter(
      pk__in=morf, part_of_speech__lexical_class__symbol='adj',
      entry__in=options)
    if adjs.count() > 1:
      debug(lexeme.entry, u'Niejednoznaczny przymiotnik źródłowy')
    if adjs:
      lexeme.part_of_speech = PartOfSpeech.objects.get(symbol='osc')
      lexeme.save()
      negs = CrossReference.objects.filter(
        from_lexeme__in=adjs, to_lexeme__deleted=False, type__symbol='nieadj')
      if negs:
        # wszystkie przymiotniki z Morfologika mają negację nie+, nie nie-+
        # wygląda na to, że w M nie ma nie-...-ości...
        assert lexeme.entry.startswith('nie')
        nonnegs = existing.filter(pk__in=morf, entry=lexeme.entry[4:])
        if nonnegs.count() > 1:
          debug(lexeme.entry, u'Niejednoznaczna wersja niezanegowana')
        if not nonnegs:
          debug(lexeme.entry, u'Nie znaleziono wersji niezanegowanej')
        else:
          for l in nonnegs:
            cr = CrossReference(
              from_lexeme=lexeme, to_lexeme=l, type__symbol='nieadj')
            cr.save()
            cr = CrossReference(
              from_lexeme=l, to_lexeme=lexeme, type__symbol='adjnie')
            cr.save()
          debug(lexeme.entry, u'Dopisano jako negację osc')
      else:
        for adj in adjs:
          cr = CrossReference(
            from_lexeme=lexeme, to_lexeme=adj, type__symbol='oscadj')
          cr.save()
          cr = CrossReference(
            from_lexeme=adj, to_lexeme=lexeme, type__symbol='adjosc')
          cr.save()
        debug(lexeme.entry, u'Dopisano jako osc')