Realizations.py
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! /usr/bin/python
# -*- coding: utf-8 -*-
from syntax.models import Schema, Position, 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)
def matches(self, phrases):
for phrase in phrases:
if phrase not in self._phrases:
return False
return True
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)
def findMatchingArgument(self, phrases):
for ar in self._argument_realizations:
if ar.matches(phrases):
return ar._argument
return None