-
Notifications
You must be signed in to change notification settings - Fork 6
/
ace_parser.py
136 lines (115 loc) · 5.53 KB
/
ace_parser.py
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
from xml.etree import ElementTree
class Parser:
def __init__(self, path):
self.entity_mentions = []
self.event_mentions = []
self.relation_mentions = []
self.parse_xml(path + '.apf.xml')
def parse_xml(self, xml_path):
tree = ElementTree.parse(xml_path)
root = tree.getroot()
for child in root[0]:
if child.tag == 'entity':
self.entity_mentions.extend(self.parse_entity_tag(child))
elif child.tag in ['value', 'timex2']:
self.entity_mentions.extend(self.parse_value_timex_tag(child))
elif child.tag == 'event':
self.event_mentions.extend(self.parse_event_tag(child))
elif child.tag == 'relation':
self.relation_mentions.extend(self.parse_relation_tag(child))
@staticmethod
def parse_entity_tag(node):
entity_mentions = []
for child in node:
if child.tag != 'entity_mention':
continue
extent = child[0]
head = child[1]
charset = extent[0]
head_charset = head[0]
entity_mention = dict()
entity_mention['entity-id'] = child.attrib['ID']
entity_mention['entity-type'] = '{}:{}'.format(node.attrib['TYPE'], node.attrib['SUBTYPE'])
entity_mention['text'] = charset.text
entity_mention['position'] = [int(charset.attrib['START']), int(charset.attrib['END'])]
entity_mention["head"] = {"text": head_charset.text,
"position": [int(head_charset.attrib['START']), int(head_charset.attrib['END'])]}
entity_mentions.append(entity_mention)
return entity_mentions
@staticmethod
def parse_relation_tag(node):
relation_mentions = []
for child in node:
if child.tag != 'relation_mention':
continue
extent = child[0]
charset = extent[0]
relation_mention = dict()
relation_mention['relation-id'] = child.attrib['ID']
relation_mention['relation-type'] = '{}:{}'.format(node.attrib['TYPE'], node.attrib['SUBTYPE'])
relation_mention['text'] = charset.text
relation_mention['position'] = [int(charset.attrib['START']), int(charset.attrib['END'])]
relation_mention['arguments'] = []
for child2 in child:
if child2.tag == 'relation_mention_argument':
extent = child2[0]
charset = extent[0]
relation_mention['arguments'].append({
'text': charset.text,
'position': [int(charset.attrib['START']), int(charset.attrib['END'])],
'role': child2.attrib['ROLE'],
'entity-id': child2.attrib['REFID'],
})
relation_mentions.append(relation_mention)
return relation_mentions
@staticmethod
def parse_event_tag(node):
event_mentions = []
for child in node:
if child.tag == 'event_mention':
event_mention = dict()
event_mention['event-id'] = child.attrib['ID']
event_mention['event_type'] = '{}:{}'.format(node.attrib['TYPE'], node.attrib['SUBTYPE'])
event_mention['arguments'] = []
for child2 in child:
if child2.tag == 'ldc_scope':
charset = child2[0]
event_mention['text'] = charset.text
event_mention['position'] = [int(charset.attrib['START']), int(charset.attrib['END'])]
if child2.tag == 'anchor':
charset = child2[0]
event_mention['trigger'] = {
'text': charset.text,
'position': [int(charset.attrib['START']), int(charset.attrib['END'])],
}
if child2.tag == 'event_mention_argument':
extent = child2[0]
charset = extent[0]
event_mention['arguments'].append({
'text': charset.text,
'position': [int(charset.attrib['START']), int(charset.attrib['END'])],
'role': child2.attrib['ROLE'],
'entity-id': child2.attrib['REFID'],
})
event_mentions.append(event_mention)
return event_mentions
@staticmethod
def parse_value_timex_tag(node):
entity_mentions = []
for child in node:
extent = child[0]
charset = extent[0]
entity_mention = dict()
entity_mention['entity-id'] = child.attrib['ID']
if 'TYPE' in node.attrib:
entity_mention['entity-type'] = node.attrib['TYPE']
if 'SUBTYPE' in node.attrib:
entity_mention['entity-type'] += ':{}'.format(node.attrib['SUBTYPE'])
if child.tag == 'timex2_mention':
entity_mention['entity-type'] = 'TIM:time'
entity_mention['text'] = charset.text
entity_mention['position'] = [int(charset.attrib['START']), int(charset.attrib['END'])]
entity_mention["head"] = {"text": charset.text,
"position": [int(charset.attrib['START']), int(charset.attrib['END'])]}
entity_mentions.append(entity_mention)
return entity_mentions