Ejemplo n.º 1
0
    def parse_duration_with_ago_and_later(source: str, reference: datetime,
                                          duration_extractor: DateTimeExtractor,
                                          duration_parser: DateTimeParser,
                                          unit_map: Dict[str, str],
                                          unit_regex: Pattern,
                                          utility_configuration: DateTimeUtilityConfiguration)\
            -> DateTimeResolutionResult:
        result = DateTimeResolutionResult()

        if duration_extractor:
            duration_extract = duration_extractor.extract(source, reference)
        else:
            return result

        if not duration_extract:
            return result

        duration = next(iter(duration_extract))
        pr = duration_parser.parse(duration, reference)

        if not pr:
            return result

        match = regex.search(unit_regex, source)
        if not match:
            return result

        after_str = source[duration.start + duration.length:]
        before_str = source[0:duration.start]
        src_unit = match.group(Constants.UNIT)
        duration_result: DateTimeResolutionResult = pr.value
        num_str = duration_result.timex[0:len(duration_result.timex) -
                                        1].replace(Constants.UNIT_P,
                                                   '').replace(
                                                       Constants.UNIT_T, '')
        num = int(num_str)

        mode = AgoLaterMode.DATE
        if pr.timex_str.__contains__("T"):
            mode = AgoLaterMode.DATETIME

        if pr.value:
            return AgoLaterUtil.get_ago_later_result(pr, num, unit_map,
                                                     src_unit, after_str,
                                                     before_str, reference,
                                                     utility_configuration,
                                                     mode)

        return result
Ejemplo n.º 2
0
    def parse_duration_with_ago_and_later(
            source: str, reference: datetime,
            duration_extractor: DateTimeExtractor,
            duration_parser: DateTimeParser, unit_map: Dict[str, str],
            unit_regex: Pattern,
            utility_configuration: DateTimeUtilityConfiguration,
            mode: AgoLaterMode) -> DateTimeResolutionResult:
        result = DateTimeResolutionResult()
        duration_extract = duration_extractor.extract(source, reference)

        if not duration_extract:
            return result

        duration = next(iter(duration_extract))
        pr = duration_parser.parse(duration, reference)

        if not pr:
            return result

        match = regex.search(unit_regex, source)
        if not match:
            return result

        after_str = source[duration.start + duration.length:]
        before_str = source[0:duration.start]
        src_unit = match.group('unit')
        duration_result: DateTimeResolutionResult = pr.value
        num_str = duration_result.timex[0:len(duration_result.timex) -
                                        1].replace('P', '').replace('T', '')
        num = int(num_str)

        if not num:
            return result

        return AgoLaterUtil.get_ago_later_result(pr, num, unit_map, src_unit,
                                                 after_str, before_str,
                                                 reference,
                                                 utility_configuration, mode)