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

from django.db.models import AutoField, BooleanField, CharField, \
                             FloatField, ForeignKey,  Model, OneToOneField, \
                             PositiveIntegerField, TextField, URLField
from django.db.models.fields.related import ManyToManyField


class Meaning(Model):
    id = AutoField(primary_key=True)
    plWN_synset = PositiveIntegerField(blank=True, null=True)
    wikilink = URLField(max_length=400, blank=True, null=True)
    description = TextField(blank=True, null=True)
    categories = ManyToManyField('Category', related_name='meanings')
    domains = ManyToManyField('Domain', related_name='meanings')

    def add_domains(self, dom_names):
        for name in dom_names:
            domain_obj, _ = Domain.objects.get_or_create(name=name)
            self.domains.add(domain_obj)

    def __unicode__(self):
        return ' = '.join(['<%s>' % expr.text if expr.main_expression else expr.text
                           for expr in self.expressions.all()])


def get_or_create_meaning(plWN_synset, wikilink):
    created = False
    meaning = None
    if plWN_synset:
        try:
            meaning = Meaning.objects.get(plWN_synset=plWN_synset)
        except Meaning.DoesNotExist:
            pass
    if wikilink and meaning == None:
        try:
            meaning = Meaning.objects.get(wikilink=wikilink)
        except Meaning.DoesNotExist:
            pass
    if meaning == None:
        meaning = Meaning.objects.create(plWN_synset=plWN_synset, wikilink=wikilink)
        created = True
    return meaning, created

    
class Expression(Model):
    id = AutoField(primary_key=True)
    text = TextField()
    meaning = ForeignKey('Meaning', related_name='expressions')
    score = FloatField()
    NKJP_freq = PositiveIntegerField()
    is_catchword = BooleanField(default=False)
    main_expression = ForeignKey('Expression', related_name='mentions',
                                 blank=True, null=True)

    def __unicode__(self):
        return self.text


class SourceLink(Model):
    expression = OneToOneField('Expression', related_name='link')
    source = ForeignKey('Source', related_name='links')
    exact_link = URLField(max_length=400, blank=True, null=True)


class Source(Model):
    key = CharField(max_length=16, primary_key=True)
    name = CharField(max_length=32)
    description = TextField(blank=True, null=True)
    url = URLField(blank=True, null=True)

    class Meta:
        ordering = ['name']

    def __unicode__(self):
        return self.name


class Segment(Model):
    position_in_expr = PositiveIntegerField()
    expression = ForeignKey('Expression', related_name='segments')
    orth = CharField(max_length=64)
    base = CharField(max_length=64)
    ctag = CharField(max_length=8)
    msd = CharField(max_length=64)
    is_head = BooleanField(default=False)

    def __unicode__(self):
        return self.orth


class Category(Model):
    key = CharField(max_length=16, primary_key=True)
    name = CharField(max_length=32)
    description = TextField(blank=True, null=True)

    class Meta:
        ordering = ['name']

    def __unicode__(self):
        return self.name


class Domain(Model):
    id = AutoField(primary_key=True)
    name = CharField(max_length=32)
    description = TextField(blank=True, null=True)

    class Meta:
        ordering = ['name']

    def __unicode__(self):
        return self.name