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

from syntax.models import Schema, Position
from syntax.models_phrase import PhraseType
from semantics.models import Frame
from connections.models import ArgumentConnection, SchemaHook

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 store(self, subentry, frame, schema, alternation):
        argument = self._argument.get(frame)
        connection, _ = ArgumentConnection.objects.get_or_create(argument=argument)
        position = Position.objects.get(id=self._position._db_id)
        for phrase_obj in self._phrases:
            phrase = PhraseType.objects.get(text_rep=str(phrase_obj))
            hook = SchemaHook(subentry=subentry,
                              schema=schema,
                              position=position,
                              phrase_type=phrase,
                              alternation=alternation)
            hook.save()
            connection.schema_connections.add(hook)

        
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 store(self, entry):
        alternation = 1
        frame = Frame.objects.get(id=self._frame._db_id)
        schema = Schema.objects.get(id=self._schema._db_id)
        for argument in frame.sorted_arguments():
            realizations = ArgumentConnection.objects.filter(argument=argument)
            for realization in realizations.all():
                connections = realization.schema_connections
                for connection in connections.all():
                    if connection.schema.id == schema.id:
                        if connection.alternation >= alternation:
                            alternation = connection.alternation + 1
        subentry = self._schema.getSubentry(entry)
        for ar in self._argument_realizations:
            ar.store(subentry, frame, schema, alternation)