def test_constraint_annotation(self): ant = ConstraintAnnotation(coords=(1, 41), constraint='when you are dead', post='never drive a car') self.assertEqual('en', ant.locale) s = ant.__repr__() self.assertGreater(len(s), 0) cite = ant.get_cite() self.assertEqual('/en/constraint/when you are dead/never drive a car', cite)
def get_constraint_annotations(text: str, strict=False) \ -> Generator[ConstraintAnnotation, None, None]: """ Find possible constraints in natural language. :param text: :param strict: :return: """ # Iterate through all potential matches for sentence in get_sentence_list(text): for match in RE_CONSTRAINT.finditer(sentence.lower()): # Get individual group matches captures = match.capturesdict() num_pre = len(captures["pre"]) num_post = len(captures["post"]) # Skip if strict and empty pre/post if strict and (num_pre + num_post == 0): continue # Setup fields constraint = captures.get("constraint").pop().lower() pre = "".join(captures["pre"]) post = "".join(captures["post"]) if num_post == 0 and num_pre == 1: combined = "{0} {1}".format(pre, constraint).lower().strip() if combined in CONSTRAINT_PHRASES: constraint = combined ant = ConstraintAnnotation(coords=match.span(), constraint=constraint, pre=pre, post=post) yield ant