models.py 3.16 KB
# -*- coding: utf-8 -*-

from django.db import models

from settings import MORFEUSZ2

###############################################
################## plWordnet ##################
###############################################

class Synset(models.Model):
        # identyfikator ze słowosieci
    id = models.IntegerField(primary_key=True)
    
    def __unicode__(self):
        text_rep = u''
        units_in_synset = LexicalUnit.objects.filter(synset__id=self.id)
        if units_in_synset.exists():
            first_lex_unit = units_in_synset.order_by('luid')[0]
            text_rep = unicode(first_lex_unit)
        return text_rep

class LexicalUnit(models.Model):
        # identyfikator ze słowosieci (jeśli jest)
    luid = models.IntegerField(null=True)
        # podstawa jednostki leksykalnej
    base = models.CharField(max_length=150)
        # numer znaczenia
    sense = models.CharField(max_length=5)
        # część mowy
    pos = models.CharField(max_length=20)
        # wskaźnik do synsetu, w którym się znajduje
    synset = models.ForeignKey(Synset, null=True)
        # definition
    definition = models.TextField(default="")
        # glossa
    glossa = models.TextField(default="")
        # haslo w Walentym
    entry = models.ForeignKey('dictionary.Entry', null=True, related_name='meanings')
    
    def __unicode__(self):
        return u'%s-%s' % (self.base, self.sense)
    
    def actual_frames(self):
        return self.frames.filter(next__isnull=True, removed=False)

    def connected(self):
        return self.actual_frames().exists()
    
    def is_new(self):
        if self.luid < 0:
            return True
        return False
    
    def is_reflexive(self):
        reflexive = False
        base_parts = self.base.split()
        if len(base_parts) > 1:
            last_is_lemma = False
            for part in base_parts:
                if last_is_lemma and part == u'się':
                    reflexive = True
                elif last_is_lemma:
                    break
                for (_, _, (orth, base, tag, _, _)) in MORFEUSZ2.analyse(part):
                    lemma = base.split(':')[0]
                    if lemma == self.entry.name:
                        last_is_lemma = True
                        break
        return reflexive
    
    def is_multiword(self):
        base_parts = self.base.split()
        if len(base_parts) > 2 and self.is_reflexive():
            return True
        elif len(base_parts) > 1 and not self.is_reflexive():
            return True
        return False

    class Meta:
        unique_together = ('base', 'sense', 'pos',)

class Hypernymy(models.Model): # hiperonimia jest odwrotna
        # hiponim childa
    parent = models.ForeignKey(Synset, related_name='hyponym')
        # hiperonim parenta
    child = models.ForeignKey(Synset, related_name='hypernym')
    
    class Meta:
        unique_together = ('parent', 'child',)
    
class Synonymy(models.Model): # relacja jest symetryczna, dane zwielokrotnione
    parent = models.ForeignKey(Synset, related_name='synset')
    child = models.ForeignKey(Synset, related_name='synonym')
    
    class Meta:
        unique_together = ('parent', 'child',)