def test_ratio_annotation(self): ant = RatioAnnotation(coords=(2, 20), left=11, right=2, ratio=11/2.0) self.assertEqual('en', ant.locale) cite = ant.get_cite() self.assertEqual('/en/ratio/11/2', cite)
def get_ratio_annotations(text: str, float_digits=4) \ -> Generator[RatioAnnotation, None, None]: for match in RATIO_PTN_RE.finditer(text.lower()): source_text, ratio_1_text, ratio_2_text = match.groups() amount_1 = list(get_amounts(ratio_1_text, float_digits=float_digits)) amount_2 = list(get_amounts(ratio_2_text, float_digits=float_digits)) if len(amount_1) != 1 or len(amount_2) != 1: continue amount_1 = amount_1[0] amount_2 = amount_2[0] if amount_1 == 0 or amount_2 == 0: continue if float_digits: amount_1 = round(amount_1, float_digits) amount_2 = round(amount_2, float_digits) total = float(amount_1) / amount_2 ant = RatioAnnotation(coords=match.span(), text=source_text.strip(), left=amount_1, right=amount_2, ratio=total) yield ant