Meanings.py 1.94 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from meanings.models import LexicalUnit, Synset
from connections.models import POS


class Meaning:

    def __init__(self, id, name, variant):
        self._id = id
        self._name = name
        self._variant = variant

    @classmethod
    def fromTree(cls, meaning_tree):
        id = meaning_tree._attrs['xml:id']
        name = meaning_tree._children[0]._children[0]._content
        variant = meaning_tree._children[1]._children[0]._content
        return cls(id, name, variant)

    def getId(self):
        return self._id
    
    def store(self, entry, frame):
        units = LexicalUnit.objects.filter(base=self._name, sense=self._variant)
        if len(units) == 0:
            i = Synset.objects.all().order_by("-id")[0].id + 1
            synset = Synset(id=i)
            synset.save()
            unit = LexicalUnit(entry=entry,
                               base=self._name,
                               sense=self._variant,
                               pos=entry.pos.tag,
                               synset=synset)
            unit.save()
        else:
            unit = units[0]
            unit.entry = entry
            unit.save()
        frame.lexical_units.add(unit)
    
    def __unicode__(self):
        return '\'' + self._name + '\'-' + self._variant
    

class Meanings:

    def __init__(self, meanings):
        self._meanings = meanings

    @classmethod
    def fromTree(cls, meanings_tree):
        meanings = {}
        for subtree in meanings_tree._children[0]._children[0]._children:
            meaning = Meaning.fromTree(subtree)
            meanings[meaning.getId()] = meaning
        return cls(meanings)

    def getMeanings(self):
        return self._meanings

    def locate(self, meaning_id):
        return self._meanings[meaning_id]
    
    def __unicode__(self):
        return '[' + ','.join([unicode(meaning) for meaning in self._meanings.values()]) + ']'