Module coreference_data
[hide private]
[frames] | no frames]

Source Code for Module coreference_data

  1  """ 
  2  This module implements a parser for the coreference layer in KAF/NAF 
  3  """ 
  4   
  5  from lxml import etree 
  6  from external_references_data import * 
  7  from span_data import Cspan 
  8   
9 -class Ccoreference:
10 """ 11 This class encapsulates a coreference object in KAF/NAF 12 """
13 - def __init__(self,node=None,type='NAF'):
14 """ 15 Constructor of the object 16 @type node: xml Element or None (to create and empty one) 17 @param node: this is the node of the element. If it is None it will create a new object 18 @type type: string 19 @param type: the type of the object (KAF or NAF) 20 """ 21 self.type = type 22 if node is None: 23 self.node = etree.Element('coref') 24 else: 25 self.node = node
26
27 - def get_node(self):
28 """ 29 Returns the node of the element 30 @rtype: xml Element 31 @return: the node of the element 32 """ 33 return self.node
34
35 - def get_id(self):
36 """ 37 Returns the identifier of the object 38 @rtype: string 39 @return: identifier of the corefence object 40 """ 41 if self.type == 'NAF': 42 return self.node.get('id') 43 elif self.type == 'KAF': 44 return self.node.get('coid')
45 46
47 - def set_id(self, this_id):
48 """ 49 Sets the identifier of the object 50 @type: string 51 @param: identifier of the corefence object 52 """ 53 if self.type == 'NAF': 54 return self.node.set('id', this_id) 55 elif self.type == 'KAF': 56 return self.node.set('coid', this_id)
57 58
59 - def get_type(self):
60 """ 61 Returns the type of the coreference object 62 @rtype: string 63 @return: type of the corefence object 64 """ 65 if self.type == 'NAF': 66 return self.node.get('type')
67 68 69
70 - def set_type(self, this_type):
71 """ 72 Sets the type of the coreference object 73 @type: string 74 @param: type of the corefence object 75 """ 76 if self.type == 'NAF': 77 return self.node.set('type', this_type)
78
79 - def add_span(self,term_span):
80 """ 81 Adds a list of term ids a new span in the references 82 @type term_span: list 83 @param term_span: list of term ids 84 """ 85 new_span = Cspan() 86 new_span.create_from_ids(term_span) 87 self.node.append(new_span.get_node())
88
89 - def get_spans(self):
90 """ 91 Iterator that returns all the span objects of the corerefence 92 @rtype: L{Cspan} 93 @return: list of span objects for the coreference object 94 """ 95 for node_span in self.node.findall('span'): 96 yield Cspan(node_span)
97
98 - def get_external_references(self):
99 """ 100 Iterator to get the external references 101 @rtype: L{CexternalReference} 102 @return: iterator for external references 103 """ 104 node = self.node.find('externalReferences') 105 if node is not None: 106 ext_refs = CexternalReferences(node) 107 for ext_ref in ext_refs: 108 yield ext_ref
109 110
111 -class Ccoreferences:
112 """ 113 This class encapsulates the coreference layer (a set of coreference objects) 114 """ 115
116 - def __init__(self,node=None, type='NAF'):
117 """ 118 Constructor of the object 119 @type node: xml Element or None (to create and empty one) 120 @param node: this is the node of the element. If it is None it will create a new object 121 @type type: string 122 @param type: the type of the object (KAF or NAF) 123 """ 124 self.type = type 125 if node is None: 126 self.node = etree.Element('coreferences') 127 else: 128 self.node = node
129
130 - def add_coreference(self,coreference):
131 self.node.append(coreference.get_node())
132 133
134 - def get_node(self):
135 """ 136 Returns the node of the element 137 @rtype: xml Element 138 @return: the node of the element 139 """ 140 return self.node
141
142 - def __get_corefs_nodes(self):
143 for coref_node in self.node.findall('coref'): 144 yield coref_node
145
146 - def get_corefs(self):
147 """ 148 Iterator that returns all the coreference objects 149 @rtype: L{Ccoreference} 150 @return: list of coreference objects (iterator) 151 """ 152 for coref_node in self.__get_corefs_nodes(): 153 yield Ccoreference(coref_node,self.type)
154
155 - def to_kaf(self):
156 """ 157 Converts the coreference layer to KAF 158 """ 159 if self.type == 'NAF': 160 for node_coref in self.__get_corefs_nodes(): 161 node_coref.set('coid',node_coref.get('id')) 162 del node_coref.attrib['id']
163
164 - def to_naf(self):
165 """ 166 Converts the coreference layer to NAF 167 """ 168 if self.type == 'KAF': 169 for node_coref in self.__get_corefs_nodes(): 170 node_coref.set('id',node_coref.get('coid')) 171 del node_coref.attrib['coid']
172