Argument.py
3.71 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
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 __unicode__(self):
return self._type + '->' + unicode(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 __unicode__(self):
return unicode(self._value)
class SemanticRole:
def __init__(self, value, attribute=None):
self._value = value
self._attribute = attribute
def __unicode__(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
@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 prolog(self):
return unicode(self._semantic_role)
def __unicode__(self):
return unicode(self._semantic_role) + '[' + ','.join([unicode(pref) for pref in self._selectional_preferences]) + ']'