Position.py 4.1 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from Phrase import phrase_from_tree
from syntax.models import Position


class Function:

    def __init__(self, value):
        self._value = value

    @classmethod
    def fromTree(cls, tree):
        value = tree._children[0]._attrs['value']
        return cls(value)

# @TODO: dwa rodzaje kontroli        
class Control:

    def __init__(self, function):
        self._function = function
        
        
class Position:

    def __init__(self, function, controls, phrases, phrase_ids):
        self._function = function
        self._control = controls 
        self._phrases = phrases
        self._phrase_ids = phrase_ids

    @classmethod
    def fromTree(cls, tree):
        function = None
        controls = None
        phrases = []
        phrase_ids = {}

        for subtree in tree._children:
            if subtree._attrs['name'] == 'function':
                function = Function.fromTree(subtree)
            elif subtree._attrs['name'] == 'control':
                controls = [Control(c._attrs['value']) for c in subtree._children[0]._children]
            elif subtree._attrs['name'] == 'phrases':
                for phrase_tree in subtree._children[0]._children:
                    phrase = phrase_from_tree(phrase_tree)
                    phrases.append(phrase)
                    phrase_ids[phrase.getId()] = phrase
        
        result = cls(function, controls, phrases, phrase_ids)
        for phrase in phrases:
            phrase._position = result
        return result

    def store(self, schema):
        if self._function is not None:
            function = SyntacticFunction.objects.get(name=self._function._value)
        else:
            function = None
        if self._control is not None:
            control = Control.objects.get(name=self._control[0]._function)
        else:
            control = None
        pred_control = None
        position = schema.positions.create(function=function,
                                           control=control,
                                           pred_control=pred_control)
        for phrase in self._phrases:
            phrase.store(position)
    
    def getPhraseIds(self):
        return self._phrase_ids

    def __unicode__(self):
        if self._function is None:
            return '[' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + ']'
        else:
            return self._function._value + '([' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + '])'

    def toUnicode(self, function, phrases=None):
        pre = ''
        post = ''
        if self._control is not None:
            for control in self._control:
                pre += control._function + '('
                post += ')'
        if phrases is None:
            if self._function is None or self._function._value == 'obj':
                return pre + '[' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + ']' + post
            elif self._function._value == 'subj':
                return pre + 'subj([' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + '])' + post
            elif self._function._value == 'obj':
                return pre + 'obj([' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + '])' + post
            elif self._function._value == 'head':
                return pre + 'head([' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + '])' + post
        else:
            temp = []
            for phrase in self._phrases:
                if phrase in phrases:
                    temp.append(phrase.toUnicode(self._function))
            if self._function is None:
                return pre + '[' + ','.join(temp) + ']' + post
            elif self._function._value == 'subj':
                return pre + 'subj([' + ','.join(temp) + '])' + post   
            elif self._function._value == 'obj':
                return pre + 'obj([' + ','.join(temp) + '])' + post
            elif self._function._value == 'head':
                return pre + 'head([' + ','.join(temp) + '])' + post