Frame.py 2.27 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from Argument import Argument

        
class Frame:

    def __init__(self, id, base, pos, opinion, meanings, arguments):
        self._id = id
        self._base = base
        self._pos = pos
        self._opinion = opinion
        self._meanings = meanings
        self._arguments = arguments

    def _resolveReferences(self, references):
        for reference in references:
            reference._to = self._arguments[reference._to]
        
    @classmethod
    def fromTree(cls, base, pos, frame_tree):
        if 'sameAs' in frame_tree._attrs:
            return cls(frame_tree._attrs['sameAs'][1:], None, None, None, None, None)
        else:
            arguments = {}
            id = frame_tree._attrs['xml:id']
            opinion = frame_tree._children[0]._children[0]._attrs['value']
            meanings = [meaning._attrs['sameAs'][1:] for meaning in frame_tree._children[1]._children[0]._children]
            for argument_tree in frame_tree._children[2]._children[0]._children:
                argument = Argument.fromTree(argument_tree)
                arguments[argument.getId()] = argument

            result = cls(id, base, pos, opinion, meanings, arguments)
            for argument in arguments.values():
                argument._frame = result
            for argument in arguments.values():
                result._resolveReferences(argument._references)
            return result

    def prolog(self, meanings, realizations):
        local_meanings = [meanings._meanings[id] for id in self._meanings if id in meanings._meanings]
        print realizations
        
        return '\n\n'.join(['sense(' + self._pos +', ' + self._base + ', ' + unicode(meaning).lower() + ').\nroles(' + unicode(meaning).lower() + ', [' + ', '.join([unicode(argument.prolog()) for argument in self._arguments.values()]) + ']).\n' + '\n'.join([realization.prolog(meaning) for realization in realizations if meaning._id in realization._frame._meanings]) for meaning in local_meanings])
    
    def getSignature(self):
        return self._base + '\t[' + ','.join([unicode(meaning) for meaning in self._meanings]) + ']'
    
    def __unicode__(self):
        return self.getSignature() + '\t[' + ','.join([unicode(argument) for argument in self._arguments.values()]) + ']'