models.py 6.96 KB
# -*- coding: utf-8 -*-
from django.db.models import Model, CharField, IntegerField, ForeignKey, \
    ManyToManyField, TextField, BooleanField, Manager
from django.utils.translation import ugettext_lazy as _

from common.util import GroupDict
from common.models import NotDeletedManager


class InflectionType(Model):
    symbol = CharField(primary_key=True, max_length=16, db_column='czm')
    color_scheme = IntegerField()
    full_name = CharField(max_length=128)

    def __unicode__(self):
        return self.symbol

    class Meta:
        db_table = 'czescimowy'


class BaseFormLabel(Model):
    symbol = CharField(max_length=32, blank=True, db_column='efobaz')
    lexical_class = ForeignKey(InflectionType)
    index = IntegerField()

    def __unicode__(self):
        return '%s/%s' % (self.symbol, self.lexical_class.symbol)

    class Meta:
        db_table = 'efobazy'
        unique_together = ['symbol', 'lexical_class']
        ordering = ['index']


class DummyEnding(object):
    def __init__(self, string):
        self.index = 0
        self.string = string
        from dictionary.models import Qualifier
        self.qualifiers = Qualifier.objects.none()


class PatternType(Model):
    lexical_class = ForeignKey(
        InflectionType, db_column='czm', verbose_name=_(u'inflection type'))
    # typ wzoru (np. dla rzeczowników: odmiana męska, żeńska lub nijaka)
    symbol = CharField(
        max_length=32, blank=True, db_column='wtyp',
        verbose_name=_(u'pattern type'))
    base_form_labels = ManyToManyField(BaseFormLabel)

    def dummy_base_endings(self):
        bfl_dict = dict(
            (bfl, [DummyEnding(bfl.symbol or self.lexical_class_id)])
            for bfl in self.base_form_labels.all())
        return bfl_dict

    @classmethod
    def options(cls):
        pattern_types = GroupDict()
        for pt in cls.objects.all():
            pattern_types.add(pt.lexical_class.symbol, (pt.id, unicode(pt)))
        return sorted(pattern_types.items())

    def __unicode__(self):
        return self.symbol.replace('"', "''") or '[%s]' % self.lexical_class_id
        # '%s (%s)' % (self.symbol, self.lexical_class_id)

    class Meta:
        db_table = 'typywzorow'
        ordering = ['symbol']


class Pattern(Model):
    STATUS_NEW = 'nowy'
    STATUS_CONFIRMED = 'conf'
    STATUS_CANDIDATE = 'cand'
    STATUS_CHOICES = (
        (STATUS_NEW, _(u'new')),
        (STATUS_CONFIRMED, _(u'confirmed')),
        (STATUS_CANDIDATE, _(u'candidate')),
    )
    HIDDEN_STATUSES = (STATUS_CANDIDATE, STATUS_NEW)
    name = CharField(
        max_length=32, unique=True, db_column='w_id', verbose_name=_(u'name'))
    old_name = CharField(
        max_length=32, verbose_name=_(u'old name'), null=True)
    type = ForeignKey(
        PatternType, db_column='typ', verbose_name=_(u'type'))
    # rdzeń przykładowej formy hasłowej
    example = CharField(
        max_length=64, db_column='przyklad', verbose_name=_(u'example'),
        blank=True)
    basic_form_ending = CharField(
        max_length=32, db_column='zakp', blank=True,
        verbose_name=_(u'basic form ending'))
    status = CharField(
        max_length=8, choices=STATUS_CHOICES, verbose_name=_(u'status'))
    comment = TextField(
        blank=True, db_column='komentarz', verbose_name=_(u'comment'))
    deleted = BooleanField(default=False)

    objects = NotDeletedManager()
    all_objects = Manager()

    def ending_set(self, subroot='', tag_prefix=None):
        endings = self.endings
        if tag_prefix:
            endings = endings.filter(
                base_form_label__symbol__startswith=tag_prefix)
        return set(subroot + e
                   for e in endings.values_list('string', flat=True))

    def base_endings(self, label_filter=None):
        bfls = self.type.base_form_labels.all()
        from dictionary.models import Ending
        endings = Ending.objects.filter(
            base_form_label__patterntype=self.type, pattern=self) \
            .select_related('base_form_label').prefetch_related('qualifiers')
        if label_filter is not None:
            endings = endings.filter(
                base_form_label__symbol__regex=label_filter)
        bfl_dict = GroupDict((bfl, []) for bfl in bfls)
        for ending in endings:
            bfl_dict.add(ending.base_form_label, ending)
        return bfl_dict

    def create_example(self, gender):
        from dictionary.models import Lexeme, PatternExample
        lexemes = Lexeme.objects.filter(
            patterns=self,
            lexemeinflectionpattern__gender=gender)
        reader_lexemes = Lexeme.filter_reader(lexemes)
        if reader_lexemes:
            lexeme = reader_lexemes[0]
            PatternExample.objects.create(
                lexeme=lexeme, pattern=self, gender=gender)

    def get_example(self, gender, refresh=False):
        from dictionary.models import PatternExample
        examples = PatternExample.objects.filter(pattern=self, gender=gender)
        if not examples or refresh:
            examples.delete()
            self.create_example(gender)
            examples = PatternExample.objects.filter(
                pattern=self, gender=gender)
        if examples:
            lexeme = examples.get().lexeme
            example_lips = lexeme.lexemeinflectionpattern_set.filter(
                pattern=self, gender=gender)
            if example_lips:
                return lexeme, example_lips[0].root
            else:
                return self.get_example(gender, refresh=True)

    def is_public(self):
        return self.status not in Pattern.HIDDEN_STATUSES

    def get_root(self, basic_form, gender, use_pattern_ending=False):
        ends = []
        basic_endings = []
        if gender:
            bfl = gender.basic_form_label
            basic_endings = self.endings.filter(base_form_label=bfl)
        elif self.type.lexical_class.symbol == 'v':
            basic_endings = self.endings.filter(base_form_label__symbol='5')
        elif self.type.lexical_class.symbol == 'num':
            basic_endings = self.endings.filter(
                base_form_label__symbol__in=('1', '1,z1'))
        else:
            use_pattern_ending = True
        if use_pattern_ending:
            ends.append(self.basic_form_ending)
        else:
            ends += [e.string for e in basic_endings]
        good_ends = [end for end in ends if basic_form.endswith(end)]
        assert len(set(good_ends)) <= 1  # inaczej rdzeń nie jest jednoznaczny
        if good_ends:
            return basic_form[:len(basic_form) - len(good_ends[0])]
        else:
            if not use_pattern_ending:
                return self.get_root(
                    basic_form, gender, use_pattern_ending=True)
            else:
                return None

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'wzory'
        ordering = ['name']
        permissions = (
            ('view_pattern', _(u'Can view patterns')),
        )


def reader_patterns(patterns):
    return patterns.exclude(status__in=Pattern.HIDDEN_STATUSES)