def generate_subject_and_object(relation): subject_kind = relation.left_entity_kind.name object_kind = relation.right_entity_kind.name Subject = refo.Plus( ConditionPredicate(is_subj=True, kinds__has=subject_kind)) Object = refo.Plus(ConditionPredicate(is_obj=True, kinds__has=object_kind)) return Subject, Object
def test_search3(self): tab = self.a + self.b regex = tab + tab + refo.Plus(self.b) strregex = re.compile("ababb+") m = refo.search(regex, self.seq) strm = strregex.search(self.string) self._eq_span_n_stuff(m, strm)
def test_search4(self): tab = self.a + self.b regex = tab * 2 + refo.Plus(self.b, greedy=False) strregex = re.compile("ababb+?") m = refo.search(regex, self.seq) strm = strregex.search(self.string) self._eq_span_n_stuff(m, strm)
def match(self, evidence): subject_kind = evidence.left_entity_occurrence.entity.kind.name object_kind = evidence.right_entity_occurrence.entity.kind.name Subject = refo.Plus( ConditionPredicate(is_subj=True, kinds__has=subject_kind)) Object = refo.Plus( ConditionPredicate(is_obj=True, kinds__has=object_kind)) tokens_to_match = list( self.generate_tokens_to_match(evidence)) + [_EOL] for rule in self.rules: regex = rule(Subject, Object) + refo.Literal(_EOL) match = refo.match(regex, tokens_to_match) if match: return rule.answer
def test_finditer2(self): tab = self.a + self.b regex = tab * (2, None) + refo.Group(refo.Plus(self.b), "foobar") strregex = re.compile("(?:ab){2,}(b+)") xs = list(refo.finditer(regex, self.seq)) strxs = list(strregex.finditer(self.string)) xs = [x.group("foobar") for x in xs] strxs = [x.span(1) for x in strxs] self.assertListEqual(xs, strxs)
def test_match_path(self): seq = [[1, 2], # x and y [1], # x [1, 2, 3], # x, y and z [1, 2], # x and y [2, 3], # y and z [0, 4, 5], []] regexptn = refo.Star(self.y) + refo.Plus(self.x + self.z) m = refomatch(regexptn, seq, keep_path=True) self.assert_(isinstance(m, Match)) path = m.get_path() self.assertEqual([4, 1, 9, 1, 9], path)
import refo def path_function(x): def f(xs): if x in xs: return x * x return None return f x = refo.Predicate(path_function(1)) y = refo.Predicate(path_function(2)) z = refo.Predicate(path_function(3)) seq = [ [1, 2], # x and y [1], # x [1, 2, 3], # x, y and z [3], # z [0, 4, 5], [] ] regex = refo.Star(y) + refo.Plus(x + z) m = refo.match(regex, seq, keep_path=True) print(m.get_path())