Realizations.py 2.16 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-


class ArgumentRealization:

    def __init__(self, argument, position, phrases):
        self._argument = argument
        self._position = position
        self._phrases = phrases

    @classmethod
    def fromTree(cls, tree, arguments, all_phrases):
        argument = arguments[tree._children[0]._children[0]._attrs['sameAs'][1:]]
        phrases = []
        for subtree in tree._children[1]._children[0]._children:
            phrases.append(all_phrases[subtree._attrs['sameAs'][1:]])
        position = phrases[0]._position
        return cls(argument, position, phrases)

    def prolog(self):
        return self._position.toUnicode(None, self._phrases) + '/' + unicode(self._argument._semantic_role)
    
    def __unicode__(self):
        return self._position.toUnicode(None, self._phrases) + "(" + unicode(self._argument._semantic_role) + ")"

        
class FrameRealization:

    def __init__(self, frame, schema, argument_realizations):
        self._frame = frame
        self._schema = schema
        self._argument_realizations = argument_realizations
        
    @classmethod
    def fromTree(cls, tree, arguments, phrases):
        argument_realizations = []
        for subtree in tree._children[0]._children[0]._children:
            argument_realizations.append(ArgumentRealization.fromTree(subtree, arguments, phrases))
        frame = argument_realizations[0]._argument._frame
        schema = argument_realizations[0]._phrases[0]._position._schema
        return cls(frame, schema, argument_realizations)

    def prolog(self, meaning):
        fake = []
        if self._schema.hasInherentSie():
            fake += ['[sie]/lemma']
        return 'realization(' + unicode(meaning).lower() + ',[' + ','.join(fake +  [argument_realization.prolog() for argument_realization in self._argument_realizations]) + ']).'
    
    def __unicode__(self):
        fake = []
        if self._schema.hasInherentSie():
            fake += ['[sie](Lemma)']
        return unicode(self._schema._id) + '\t' + self._frame.getSignature() + '\t [' + ','.join(fake + [unicode(argument_realization) for argument_realization in self._argument_realizations]) + ']'