def test_reconcile_TIMEX(self): s = Timex3XmlDocument("<root>This is some annotated text.</root>") t = Timex(type="date") t1 = Timex(id=1) t2 = Timex(id=2) t3 = Timex(id=3) t.value = "20100710" t.id = 6 t.mod = "BEFORE" t.freq = "1M" t.comment = "Test" t.quant = "EVERY" t.temporal_function = True t.document_role = "MODIFICATION_TIME" t.begin_timex = t1 t.end_timex = t2 t.context = t3 s.reconcile( [ [ ("This", "POS", set()), ("is", "POS", set()), ("some", "POS", {t}), ("annotated", "POS", {t}), ("text", "POS", {t}), (".", "POS", set()), ] ] ) self.assertEquals( str(s), xml.dom.minidom.parseString( '<root>This is <TIMEX3 tid="t6" beginPoint="t1" endPoint="t2" anchorTimeID="t3" functionInDocument="MODIFICATION_TIME" temporalFunction="true" type="DATE" value="20100710" mod="BEFORE" freq="1M" comment="Test" quant="EVERY">some annotated text</TIMEX3>.</root>' ).toxml(), )
def _timex_from_node(self, node): """ Given a TIMEX2 node, create a timex object with the values of that node """ t = Timex() if node.hasAttribute('SET'): if node.getAttribute('SET').lower() == 'yes': t.type = 'set' if node.hasAttribute('PERIODICITY'): t.value = 'P' + node.getAttribute('PERIODICITY')[1:] if node.hasAttribute('VAL'): t.value = node.getAttribute('VAL') if node.hasAttribute('MOD'): t.mod = node.getAttribute('MOD') if node.hasAttribute('GRANUALITY'): t.freq = node.getAttribute('GRANUALITY')[1:] if node.hasAttribute('COMMENT'): t.comment = node.getAttribute('COMMENT') return t
def _timex_from_node(self, node): """ Given a TIMEX2 node, create a timex object with the values of that node """ t = Timex() if node.hasAttribute('SET'): if node.getAttribute('SET').lower() == 'yes': t.type = 'set' if node.hasAttribute('PERIODICITY'): t.value = 'P' + node.getAttribute('PERIODICITY')[1:] if node.hasAttribute('VAL'): t.value = node.getAttribute('VAL') if node.hasAttribute('MOD'): t.mod = node.getAttribute('MOD') if node.hasAttribute('GRANUALITY'): t.freq = node.getAttribute('GRANUALITY')[1:] if node.hasAttribute('COMMENT'): t.comment = node.getAttribute('COMMENT') return t
def test_reconcile_TIMEX(self): s = Timex3XmlDocument('<root>This is some annotated text.</root>') t = Timex(type='date') t1 = Timex(id=1) t2 = Timex(id=2) t3 = Timex(id=3) t.value = "20100710" t.id = 6 t.mod = "BEFORE" t.freq = "1M" t.comment = "Test" t.quant = 'EVERY' t.temporal_function = True t.document_role = 'MODIFICATION_TIME' t.begin_timex = t1 t.end_timex = t2 t.context = t3 s.reconcile([[('This', 'POS', set()), ('is', 'POS', set()), ('some', 'POS', {t}), ('annotated', 'POS', {t}), ('text', 'POS', {t}), ('.', 'POS', set())]]) self.assertEqual( str(s), xml.dom.minidom.parseString( '<root>This is <TIMEX3 tid="t6" beginPoint="t1" endPoint="t2" anchorTimeID="t3" functionInDocument="MODIFICATION_TIME" temporalFunction="true" type="DATE" value="20100710" mod="BEFORE" freq="1M" comment="Test" quant="EVERY">some annotated text</TIMEX3>.</root>' ).toxml())
def test_reconcile_TIMEX(self): s = Timex2XmlDocument('<root>This is some annotated text.</root>') t = Timex(type='date') t.value = "20100710" t.mod = "BEFORE" t.freq = "1M" t.comment = "Test" s.reconcile([[('This', 'POS', set()), ('is', 'POS', set()), ('some', 'POS', {t}), ('annotated', 'POS', {t}), ('text', 'POS', {t}), ('.', 'POS', set())]]) self.assertEqual(str(s), xml.dom.minidom.parseString('<root>This is <TIMEX2 VAL="20100710" MOD="BEFORE" COMMENT="Test" GRANUALITY="G1M">some annotated text</TIMEX2>.</root>').toxml())
def test_reconcile_TIMEX(self): s = Timex2XmlDocument('<root>This is some annotated text.</root>') t = Timex(type='date') t.value = "20100710" t.mod = "BEFORE" t.freq = "1M" t.comment = "Test" s.reconcile([[('This', 'POS', set()), ('is', 'POS', set()), ('some', 'POS', {t}), ('annotated', 'POS', {t}), ('text', 'POS', {t}), ('.', 'POS', set())]]) self.assertEquals(str(s), xml.dom.minidom.parseString('<root>This is <TIMEX2 VAL="20100710" MOD="BEFORE" COMMENT="Test" GRANUALITY="G1M">some annotated text</TIMEX2>.</root>').toxml())
def apply(self, sent): """ Applies this rule to the tokenised sentence. The 'after' ordering must be checked by the caller to ensure correct rule application. sent is a list of tuples (token, POS, [timexes]) A tuple is returned where the first element is a list in the same form as sent, with additional timexes added to the 3rd element if need be, and the second element in the tuple is whether or not this rule matched anything """ senttext = self._toks_to_str(sent) if self._deliminate_numbers: senttext = self._do_deliminate_numbers(senttext) success = False # Ensure the sentence-level guards are satisfied if not self._check_guards(senttext, self._guards): return sent, success # Now see if this rule actually matches anything for match in self._match.finditer(senttext): # Now check before guards if not self._check_guards(senttext[:match.start()], self._before_guards): continue # and after guards if not self._check_guards(senttext[match.end():], self._after_guards): continue # okay, first we need to find which tokens we matched, can do this # by using our token markers ti = senttext.count('<', 0, match.start()) tj = senttext.count('<', 0, match.end()) if not self._squelch: t = Timex( self._type) # only create a new timex if not squelching if self._DEBUG: t.comment = self.id else: t = None # Add TIMEX self._set_timex_extents(t, sent, ti, tj, self._squelch) success = True return sent, success
def apply(self, sent): """ Applies this rule to the tokenised sentence. The 'after' ordering must be checked by the caller to ensure correct rule application. sent is a list of tuples (token, POS, [timexes]) A tuple is returned where the first element is a list in the same form as sent, with additional timexes added to the 3rd element if need be, and the second element in the tuple is whether or not this rule matched anything """ senttext = self._toks_to_str(sent) if self._deliminate_numbers: senttext = self._do_deliminate_numbers(senttext) success = False # Ensure the sentence-level guards are satisfied if not self._check_guards(senttext, self._guards): return sent, success # Now see if this rule actually matches anything for match in self._match.finditer(senttext): # Now check before guards if not self._check_guards(senttext[:match.start()], self._before_guards): continue # and after guards if not self._check_guards(senttext[match.end():], self._after_guards): continue # okay, first we need to find which tokens we matched, can do this # by using our token markers ti = senttext.count('<', 0, match.start()) tj = senttext.count('<', 0, match.end()) if not self._squelch: t = Timex(self._type) # only create a new timex if not squelching if self._DEBUG: t.comment = self.id else: t = None # Add TIMEX self._set_timex_extents(t, sent, ti, tj, self._squelch) success = True return sent, success
def test_reconcile_sents_attrs(self): t1 = Timex(id=1, type='date') t2 = Timex(id=2) t3 = Timex(id=3) t1.value = "20100710" t1.mod = "BEFORE" t1.freq = "1M" t1.comment = "Test" t1.granuality = "1D" t1.non_specific = True t1.quant = 'EVERY' t1.temporal_function = True t1.document_role = 'MODIFICATION_TIME' t1.begin_timex = t1 t1.end_timex = t2 t1.context = t3 d = GateDocument("""This POS B 20101010 is POS I a POS I sentence POS I . . I And POS B a POS I second POS I sentence POS I . POS I Outside POS O""") d.reconcile([[('This', 'POS', set()), ('is', 'POS', set()), ('a', 'POS', set([t1])), ('sentence', 'POS', set([t1])), ('.', '.', set())], [ ('And', 'POS', set()), ('a', 'POS', set()), ('second', 'POS', set()), ('sentence', 'POS', set()), ('.', 'POS', set()), ], [('Outside', 'POS', set())]]) self.assertEquals( str(d), """This is a id=t1,value=20100710,type=DATE,mod=BEFORE,freq=1M,quant=EVERY,temporalFunction=true,functionInDocument=MODIFICATION_TIME,beginPoint=t1,endPoint=t2,anchorTimeID=t3 sentence t1 . And a second sentence . Outside """)
def _timex_from_node(self, node): """ Given a node representing a TIMEX3 element, return a timex object representing it """ t = Timex() if node.hasAttribute('tid'): t.id = int(node.getAttribute('tid')[1:]) if node.hasAttribute('value'): t.value = node.getAttribute('value') if node.hasAttribute('mod'): t.mod = node.getAttribute('mod') if node.hasAttribute('type'): t.type = node.getAttribute('type') if node.hasAttribute('freq'): t.freq = node.getAttribute('freq') if node.hasAttribute('quant'): t.quant = node.getAttribute('quant') if node.hasAttribute('comment'): t.comment = node.getAttribute('comment') if node.getAttribute('temporalFunction'): t.temporal_function = True if node.hasAttribute('functionInDocument'): t.document_role = node.getAttribute('functionInDocument') if node.hasAttribute('beginPoint'): t.begin_timex = int(node.getAttribute('beginPoint')[1:]) if node.hasAttribute('endPoint'): t.end_timex = int(node.getAttribute('endPoint')[1:]) if node.hasAttribute('anchorTimeID'): t.context = int(node.getAttribute('anchorTimeID')[1:]) return t
def _timex_from_node(self, node): """ Given a node representing a TIMEX3 element, return a timex object representing it """ t = Timex() if node.hasAttribute('tid'): t.id = int(node.getAttribute('tid')[1:]) if node.hasAttribute('value'): t.value = node.getAttribute('value') if node.hasAttribute('mod'): t.mod = node.getAttribute('mod') if node.hasAttribute('type'): t.type = node.getAttribute('type') if node.hasAttribute('freq'): t.freq = node.getAttribute('freq') if node.hasAttribute('quant'): t.quant = node.getAttribute('quant') if node.hasAttribute('comment'): t.comment = node.getAttribute('comment') if node.getAttribute('temporalFunction'): t.temporal_function = True if node.hasAttribute('functionInDocument'): t.document_role = node.getAttribute('functionInDocument') if node.hasAttribute('beginPoint'): t.begin_timex = int(node.getAttribute('beginPoint')[1:]) if node.hasAttribute('endPoint'): t.end_timex = int(node.getAttribute('endPoint')[1:]) if node.hasAttribute('anchorTimeID'): t.context = int(node.getAttribute('anchorTimeID')[1:]) return t
def test_attr(self): t1 = Timex(id=1, type='date') t2 = Timex(id=2) t3 = Timex(id=3) t1.value = "20100710" t1.mod = "BEFORE" t1.freq = "1M" t1.comment = "Test" t1.granuality = "1D" t1.non_specific = True t1.quant = 'EVERY' t1.temporal_function = True t1.document_role = 'MODIFICATION_TIME' t1.begin_timex = t1 t1.end_timex = t2 t1.context = t3 sents = [[('The', 'DT', set()), ('first', 'JJ', {t1}), ('sentence', 'NN', set()), ('.', '.', set())], [('The', 'DT', set()), ('second', 'JJ', {t2}), ('sentence', 'NN', {t2}), ('.', '.', {t3})]] d = TempEval2Document.create(sents, 'ABC1') with open(self.filepath('timex-attr.tab')) as fd: self.assertEquals(sorted(d.get_attrs().splitlines()), sorted(fd.read().splitlines()))
def test_attr(self): t1 = Timex(id=1, type='date') t2 = Timex(id=2) t3 = Timex(id=3) t1.value = "20100710" t1.mod = "BEFORE" t1.freq = "1M" t1.comment = "Test" t1.granuality = "1D" t1.non_specific = True t1.quant = 'EVERY' t1.temporal_function = True t1.document_role = 'MODIFICATION_TIME' t1.begin_timex = t1 t1.end_timex = t2 t1.context = t3 sents = [[('The', 'DT', set()), ('first', 'JJ', {t1}), ('sentence', 'NN', set()), ('.', '.', set())], [('The', 'DT', set()), ('second', 'JJ', {t2}), ('sentence', 'NN', {t2}), ('.', '.', {t3})]] d = TempEval2Document.create(sents, 'ABC1') with open(self.filepath('timex-attr.tab')) as fd: self.assertEqual(sorted(d.get_attrs().splitlines()), sorted(fd.read().splitlines()))
def test_reconcile_sents_attrs(self): t1 = Timex(id=1, type='date') t2 = Timex(id=2) t3 = Timex(id=3) t1.value = "20100710" t1.mod = "BEFORE" t1.freq = "1M" t1.comment = "Test" t1.granuality = "1D" t1.non_specific = True t1.quant = 'EVERY' t1.temporal_function = True t1.document_role = 'MODIFICATION_TIME' t1.begin_timex = t1 t1.end_timex = t2 t1.context = t3 d = GateDocument("""This POS B 20101010 is POS I a POS I sentence POS I . . I And POS B a POS I second POS I sentence POS I . POS I Outside POS O""") d.reconcile([[('This', 'POS', set()), ('is', 'POS', set()), ('a', 'POS', set([t1])), ('sentence', 'POS', set([t1])), ('.', '.', set())], [('And', 'POS', set()), ('a', 'POS', set()), ('second', 'POS', set()), ('sentence', 'POS', set()), ('.', 'POS', set()), ], [('Outside', 'POS', set())]]) self.assertEquals(str(d), """This is a id=t1,value=20100710,type=DATE,mod=BEFORE,freq=1M,quant=EVERY,temporalFunction=true,functionInDocument=MODIFICATION_TIME,beginPoint=t1,endPoint=t2,anchorTimeID=t3 sentence t1 . And a second sentence . Outside """)