Semantics.py 2.15 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from importer.Frame import Frame
from importer.Realizations import FrameRealization


class Semantics:

    def __init__(self, base, pos, frames, realizations):
        self._frames = frames
        self._base = base
        self._pos = pos
        self._realizations = realizations

    @classmethod
    def fromTree(cls, base, pos, frames_tree, all_frames, meanings, syntax, realizations_tree):
        arguments = {}
        
        frames = []
        for frame_subtree in frames_tree._children[0]._children[0]._children:
            frame = Frame.fromTree(base, pos, frame_subtree)
            if frame._base is None:
                frame = all_frames[frame._id]
            frames.append(frame)
            arguments.update(frame._arguments)

        realizations = [FrameRealization.fromTree(subtree, arguments, syntax._phrases) for subtree in realizations_tree._children[0]._children[0]._children]
        
        return cls(base, pos, frames, realizations)

    def store(self, entry, meanings):
        for frame in self._frames:
            frame.store(entry, meanings)
        for realization in self._realizations:
            realization.store(entry)
        entry.frames_count = len(self._frames)
    
    def getPhraseIds(self):
        return self._phrases

    def findFrame(self, meaning):
        for frame in self._frames:
            if meaning in frame._meanings:
                realizations = []
                for realization in self._realizations:
                    if realization._frame._id == frame._id:
                        realizations.append(realization)
                return frame, realizations
        return None
    
    # def preferencesToUnicode(self):
    #     return '\n'.join([unicode(frame) for frame in self._frames])

    # def realizationsToUnicode(self):
    #     return '\n'.join([unicode(realization) for realization in self._realizations])

    # def prolog(self, meanings):
    #     return '\n\n'.join([frame.prolog(meanings, self._realizations) for frame in self._frames])
    
    # def __unicode__(self):
    #     return '\n'.join([unicode(frame) for frame in self._frames])