Exemplo n.º 1
0
    def get_parse_time_result(self, entity: str, match: Match, reference: datetime) -> TimeResult:
        match_str: str = match.group()

        extract_result = ExtractResult()
        extract_result.start = match.start() + match_str.find(entity)
        extract_result.length = len(entity)
        extract_result.text = entity
        extract_result.type = Constants.SYS_DATETIME_TIME

        result = self.config.time_parser.parse(extract_result, reference)
        return result.data
Exemplo n.º 2
0
    def extract(self,
                source: str,
                reference: datetime = None) -> List[ExtractResult]:

        if reference is None:
            reference = datetime.now()

        result: List[ExtractResult] = list()
        if not source:
            return result

        match_source: Dict[Match, any] = dict()
        matched: List[bool] = [False] * len(source)

        collections = list(
            map(lambda x: (list(regex.finditer(x[0], source)), x[1]),
                self._regex_dict.items()))
        collections = list(filter(lambda x: len(x[0]) > 0, collections))

        for collection in collections:
            for match in collection[0]:
                for j in range(len(match.group())):
                    matched[match.start() + j] = True
                match_source[match] = collection[1]

        last = -1
        for i in range(len(source)):
            if matched[i]:
                if i + 1 == len(source) or not matched[i + 1]:
                    start = last + 1
                    length = i - last
                    text = source[start:start + length].strip()
                    src_match = next(
                        (x for x in iter(match_source)
                         if (x.start() == start and (x.end() -
                                                     x.start()) == length)),
                        None)
                    if src_match:
                        value = ExtractResult()
                        value.start = start
                        value.length = length
                        value.text = text
                        value.type = self.extractor_type_name
                        value.data = self.__get_data(match_source, src_match)
                        result.append(value)
            else:
                last = i

        return result