Argument.py
5.83 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#! /usr/bin/python
# -*- coding: utf-8 -*-
from semantics.models import RelationalSelectionalPreference, PredefinedSelectionalPreference, \
SelectionalPreferenceRelation, ArgumentRole
import semantics.models
from meanings.models import Synset
class Relation:
def __init__(self, type, argument_type, to):
self._type = type
self._argument_type = argument_type
self._to = to
@classmethod
def fromTree(cls, tree):
type = tree._children[0]._children[0]._attrs['value']
argument_type = tree._children[1]._children[0]._attrs['type']
to = tree._children[1]._children[0]._attrs['sameAs'][1:]
if argument_type != 'argument':
print(argument_type, to)
raise UnknownError()
return cls(type, argument_type, to)
def store(self, frame, argument):
role = self._to._semantic_role.get()
to = frame.sorted_arguments().get(role=role)
relation = SelectionalPreferenceRelation.objects.get(key=self._type)
pref = RelationalSelectionalPreference(relation=relation, to=to)
pref.save()
argument.relations.add(pref)
def __str__(self):
return self._type + '->' + str(self._to._semantic_role)
class SelectionalPreference:
def __init__(self, type, value):
self._type = type
self._value = value
@classmethod
def forType(cls, type, values):
result = []
reference_to_solve = []
if type == 'predef':
for value in values:
result.append(cls(type, value._attrs['value']))
elif type == 'synset':
for value in values:
result.append(cls(type, value._attrs['value']))
elif type == 'relation':
for value in values:
x = Relation.fromTree(value)
result.append(cls(type, x))
reference_to_solve.append(x)
else:
print(type, values)
raise UnknownError()
return result, reference_to_solve
# return [cls(type, value._attrs['value']) for value in values]
@classmethod
def fromTree(cls, tree):
preferences = []
references = []
for subtree in tree._children[0]._children:
new_preferences, new_references = cls.forType(subtree._attrs['name'][:-1], subtree._children[0]._children)
preferences += new_preferences
references += new_references
return preferences, references
def store(self, frame, argument):
if self._type == 'predef':
pref = PredefinedSelectionalPreference.objects.get(key=self._value)
argument.predefined.add(pref)
elif self._type == 'synset':
pref = Synset.objects.get(id=self._value)
argument.synsets.add(pref)
elif self._type == 'relation':
self._value.store(frame, argument)
else:
print(type, values)
raise UnknownError()
def __str__(self):
return str(self._value)
class SemanticRole:
def __init__(self, value, attribute=None):
self._value = value
self._attribute = attribute
def get(self):
return self.store()
def store(self):
role = semantics.models.SemanticRole.objects.get(role=self._value)
if self._attribute is not None:
attribute = semantics.models.RoleAttribute.objects.get(attribute=self._attribute)
else:
attribute=None
argument_role = semantics.models.ArgumentRole.objects.get(role=role, attribute=attribute)
return argument_role
def __str__(self):
if self._attribute is None:
return self._value.lower()
else:
return '(' + self._value.lower() + '-' + self._attribute.lower() + ')'
class Argument:
def __init__(self, semantic_role, selectional_preferences, references, id):
self._frame = None
self._semantic_role = semantic_role
self._selectional_preferences = selectional_preferences
self._references = references
self._id = id
self._db_id = None
@classmethod
def fromTree(cls, tree):
id = tree._attrs['xml:id']
attribute = None
selectional_preferences = []
references = []
for subtree in tree._children:
if subtree._attrs['name'] == 'role':
role = subtree._children[0]._attrs['value']
elif subtree._attrs['name'] == 'role_attribute':
attribute = subtree._children[0]._attrs['value']
elif subtree._attrs['name'] == 'sel_prefs':
selectional_preferences, references = SelectionalPreference.fromTree(subtree)
else:
print(subtree._attrs['name'])
raise UnknownError()
semantic_role = SemanticRole(role, attribute)
return cls(semantic_role, selectional_preferences, references, id)
def getId(self):
return self._id
def get(self, frame):
role = self._semantic_role.get()
argument = semantics.models.Argument.objects.get(frame=frame, role=role)
return argument
def store(self, frame):
role = self._semantic_role.store()
argument = semantics.models.Argument(role=role,
frame=frame,
preferences_count=len(self._selectional_preferences))
argument.save()
self._db_id = argument.id
return argument
def store_preferences(self, frame, argument):
for preference in self._selectional_preferences:
preference.store(frame, argument)
def __str__(self):
return str(self._semantic_role) + '[' + ','.join([str(pref) for pref in self._selectional_preferences]) + ']'