Exemple #1
0
    def get_all_annotations(cls,
                            text: str,
                            float_digits=4) \
            -> List[DurationAnnotation]:

        all_annotations = []

        for match in cls.DURATION_PTN_RE.finditer(text.lower()):
            source_text, number_text, duration_type = match.groups()
            amount = list(get_amounts(number_text, float_digits=float_digits))
            if len(amount) != 1:
                continue
            amount = amount[0]
            if float_digits:
                amount = round(amount, float_digits)
            duration_days = cls.DURATION_MAP[cls.DURATION_TRANSLATION_MAP[duration_type]] * amount
            if duration_type == 'anniversaries':
                duration_type = 'anniversary'
            ant = DurationAnnotation(coords=match.span(),
                                     amount=amount,
                                     duration_type=duration_type,
                                     duration_days=duration_days,
                                     text=source_text.strip())
            all_annotations.append(ant)
        return all_annotations
 def get_all_annotations(
     cls,
     text: str,
     float_digits: int = 4,
 ) -> List[DurationAnnotation]:
     all_annotations: List[DurationAnnotation] = []
     for match in cls.DURATION_PTN_RE.finditer(text.lower()):
         source_text, number_text, duration_type = match.groups()
         amount = list(get_amounts(number_text, float_digits=float_digits))
         if len(amount) != 1:
             continue
         amount = amount[0]
         _duration_fraction: Fraction = cls.DURATION_MAP[duration_type]
         duration_days: Decimal = Decimal(
             (_duration_fraction.numerator * amount) /
             _duration_fraction.denominator)
         if float_digits:
             duration_days: Decimal = quantize_by_float_digit(
                 amount=duration_days, float_digits=float_digits)
         if duration_type == 'anniversaries':
             duration_type = 'anniversary'
         ant: DurationAnnotation = DurationAnnotation(
             coords=match.span(),
             amount=amount,
             duration_type=duration_type,
             duration_days=duration_days,
             text=source_text.strip())
         all_annotations.append(ant)
     return all_annotations
Exemple #3
0
    def get_all_annotations(cls,
                            text: str,
                            float_digits=4) \
            -> List[DurationAnnotation]:

        all_annotations = []
        for match in cls.DURATION_PTN_RE.finditer(text):
            capture = match.capturesdict()
            amount_text = ''.join(capture.get('num_text', ''))
            amounts = list(get_amounts(amount_text, float_digits=float_digits))
            if len(amounts) != 1:
                amount = 1
            else:
                amount = amounts[0]
            unit_name_local = ''.join(capture.get('unit_name', '')).lower()
            unit_prefix = ''.join(capture.get('unit_prefix', '')).lower()
            unit_name_local = cls.DURATION_MAP_RE.findall(unit_name_local)
            if not unit_name_local:
                continue
            unit_name_local = unit_name_local[0]
            unit_name_en = cls.DURATION_TRANSLATION_MAP.get(unit_name_local)

            amount_days = cls.DURATION_MAP[unit_name_en] * amount
            if float_digits:
                amount_days = round(amount_days, float_digits)
            ant = DurationAnnotation(coords=match.span(),
                                     text=''.join(capture.get('text', '')),
                                     amount=amount,
                                     duration_days=amount_days,
                                     duration_type_en=unit_name_en,
                                     duration_type=unit_name_local,
                                     prefix=unit_prefix,
                                     locale=cls.LOCALE)
            all_annotations.append(ant)
        return all_annotations
 def sum_annotations(
     cls,
     ant_group: List[DurationAnnotation],
 ) -> DurationAnnotation:
     coords = (ant_group[0].coords[0], ant_group[-1].coords[1])
     rst: DurationAnnotation = DurationAnnotation(
         coords, locale=ant_group[0].locale, is_complex=True)
     rst.duration_days = sum([d.duration_days for d in ant_group])
     rst.amount = rst.duration_days
     rst.duration_type = ant_group[-1].duration_type
     rst.duration_type_en = ant_group[-1].duration_type_en
     return rst
Exemple #5
0
    def get_all_annotations(cls,
                            text: str,
                            float_digits: int = 4) -> List[DurationAnnotation]:
        all_annotations: List[DurationAnnotation] = []
        for match in cls.DURATION_PTN_RE.finditer(text):
            capture = match.capturesdict()
            amount_text = ''.join(capture.get('num_text', ''))
            amounts = list(get_amounts(amount_text, float_digits=float_digits))
            if len(amounts) != 1:
                amount = Decimal('1.0')
            else:
                amount = amounts[0]
            unit_name_local = ''.join(capture.get('unit_name', '')).lower()
            unit_prefix = ''.join(capture.get('unit_prefix', '')).lower()
            unit_name_local = cls.DURATION_MAP_RE.findall(unit_name_local)
            if not unit_name_local:
                continue
            unit_name_local = unit_name_local[0]
            unit_name_en = cls.DURATION_TRANSLATION_MAP.get(unit_name_local)

            _duration_fraction: Fraction = cls.DURATION_MAP[unit_name_en]
            amount_days: Decimal = Decimal(
                (_duration_fraction.numerator * amount) /
                _duration_fraction.denominator)

            if float_digits:
                amount_days: Decimal = quantize_by_float_digit(
                    amount=amount_days, float_digits=float_digits)

            ant: DurationAnnotation = DurationAnnotation(
                coords=match.span(),
                text=''.join(capture.get('text', '')),
                amount=amount,
                duration_days=amount_days,
                duration_type_en=unit_name_en,
                duration_type=unit_name_local,
                prefix=unit_prefix,
                locale=cls.LOCALE)
            all_annotations.append(ant)
        return all_annotations