|
| 1 | +fromxml.etreeimportElementTree |
| 2 | + |
| 3 | + |
| 4 | +classParser: |
| 5 | +def__init__(self,path): |
| 6 | +self.entity_mentions= [] |
| 7 | +self.event_mentions= [] |
| 8 | +self.relation_mentions= [] |
| 9 | +self.parse_xml(path+'.apf.xml') |
| 10 | + |
| 11 | +defparse_xml(self,xml_path): |
| 12 | +tree=ElementTree.parse(xml_path) |
| 13 | +root=tree.getroot() |
| 14 | +forchildinroot[0]: |
| 15 | +ifchild.tag=='entity': |
| 16 | +self.entity_mentions.extend(self.parse_entity_tag(child)) |
| 17 | +elifchild.tagin ['value','timex2']: |
| 18 | +self.entity_mentions.extend(self.parse_value_timex_tag(child)) |
| 19 | +elifchild.tag=='event': |
| 20 | +self.event_mentions.extend(self.parse_event_tag(child)) |
| 21 | +elifchild.tag=='relation': |
| 22 | +self.relation_mentions.extend(self.parse_relation_tag(child)) |
| 23 | + |
| 24 | +@staticmethod |
| 25 | +defparse_entity_tag(node): |
| 26 | +entity_mentions= [] |
| 27 | + |
| 28 | +forchildinnode: |
| 29 | +ifchild.tag!='entity_mention': |
| 30 | +continue |
| 31 | +extent=child[0] |
| 32 | +head=child[1] |
| 33 | +charset=extent[0] |
| 34 | +head_charset=head[0] |
| 35 | + |
| 36 | +entity_mention=dict() |
| 37 | +entity_mention['entity-id']=child.attrib['ID'] |
| 38 | +entity_mention['entity-type']='{}:{}'.format(node.attrib['TYPE'],node.attrib['SUBTYPE']) |
| 39 | +entity_mention['text']=charset.text |
| 40 | +entity_mention['position']= [int(charset.attrib['START']),int(charset.attrib['END'])] |
| 41 | +entity_mention["head"]= {"text":head_charset.text, |
| 42 | +"position": [int(head_charset.attrib['START']),int(head_charset.attrib['END'])]} |
| 43 | + |
| 44 | +entity_mentions.append(entity_mention) |
| 45 | + |
| 46 | +returnentity_mentions |
| 47 | + |
| 48 | +@staticmethod |
| 49 | +defparse_relation_tag(node): |
| 50 | +relation_mentions= [] |
| 51 | + |
| 52 | +forchildinnode: |
| 53 | +ifchild.tag!='relation_mention': |
| 54 | +continue |
| 55 | +extent=child[0] |
| 56 | +charset=extent[0] |
| 57 | + |
| 58 | +relation_mention=dict() |
| 59 | +relation_mention['relation-id']=child.attrib['ID'] |
| 60 | +relation_mention['relation-type']='{}:{}'.format(node.attrib['TYPE'],node.attrib['SUBTYPE']) |
| 61 | +relation_mention['text']=charset.text |
| 62 | +relation_mention['position']= [int(charset.attrib['START']),int(charset.attrib['END'])] |
| 63 | +relation_mention['arguments']= [] |
| 64 | +forchild2inchild: |
| 65 | +ifchild2.tag=='relation_mention_argument': |
| 66 | +extent=child2[0] |
| 67 | +charset=extent[0] |
| 68 | +relation_mention['arguments'].append({ |
| 69 | +'text':charset.text, |
| 70 | +'position': [int(charset.attrib['START']),int(charset.attrib['END'])], |
| 71 | +'role':child2.attrib['ROLE'], |
| 72 | +'entity-id':child2.attrib['REFID'], |
| 73 | + }) |
| 74 | +relation_mentions.append(relation_mention) |
| 75 | + |
| 76 | +returnrelation_mentions |
| 77 | + |
| 78 | +@staticmethod |
| 79 | +defparse_event_tag(node): |
| 80 | +event_mentions= [] |
| 81 | +forchildinnode: |
| 82 | +ifchild.tag=='event_mention': |
| 83 | +event_mention=dict() |
| 84 | +event_mention['event-id']=child.attrib['ID'] |
| 85 | +event_mention['event_type']='{}:{}'.format(node.attrib['TYPE'],node.attrib['SUBTYPE']) |
| 86 | +event_mention['arguments']= [] |
| 87 | +forchild2inchild: |
| 88 | +ifchild2.tag=='ldc_scope': |
| 89 | +charset=child2[0] |
| 90 | +event_mention['text']=charset.text |
| 91 | +event_mention['position']= [int(charset.attrib['START']),int(charset.attrib['END'])] |
| 92 | +ifchild2.tag=='anchor': |
| 93 | +charset=child2[0] |
| 94 | +event_mention['trigger']= { |
| 95 | +'text':charset.text, |
| 96 | +'position': [int(charset.attrib['START']),int(charset.attrib['END'])], |
| 97 | + } |
| 98 | +ifchild2.tag=='event_mention_argument': |
| 99 | +extent=child2[0] |
| 100 | +charset=extent[0] |
| 101 | +event_mention['arguments'].append({ |
| 102 | +'text':charset.text, |
| 103 | +'position': [int(charset.attrib['START']),int(charset.attrib['END'])], |
| 104 | +'role':child2.attrib['ROLE'], |
| 105 | +'entity-id':child2.attrib['REFID'], |
| 106 | + }) |
| 107 | +event_mentions.append(event_mention) |
| 108 | +returnevent_mentions |
| 109 | + |
| 110 | +@staticmethod |
| 111 | +defparse_value_timex_tag(node): |
| 112 | +entity_mentions= [] |
| 113 | + |
| 114 | +forchildinnode: |
| 115 | +extent=child[0] |
| 116 | +charset=extent[0] |
| 117 | + |
| 118 | +entity_mention=dict() |
| 119 | +entity_mention['entity-id']=child.attrib['ID'] |
| 120 | + |
| 121 | +if'TYPE'innode.attrib: |
| 122 | +entity_mention['entity-type']=node.attrib['TYPE'] |
| 123 | +if'SUBTYPE'innode.attrib: |
| 124 | +entity_mention['entity-type']+=':{}'.format(node.attrib['SUBTYPE']) |
| 125 | +ifchild.tag=='timex2_mention': |
| 126 | +entity_mention['entity-type']='TIM:time' |
| 127 | + |
| 128 | +entity_mention['text']=charset.text |
| 129 | +entity_mention['position']= [int(charset.attrib['START']),int(charset.attrib['END'])] |
| 130 | + |
| 131 | +entity_mention["head"]= {"text":charset.text, |
| 132 | +"position": [int(charset.attrib['START']),int(charset.attrib['END'])]} |
| 133 | + |
| 134 | +entity_mentions.append(entity_mention) |
| 135 | + |
| 136 | +returnentity_mentions |