Пример #1
0
    def generate(self, item: DictionaryItem = None, morphemes: Tuple[Morpheme, ...] = None,
                 candidates: Tuple[StemTransition, ...] = None) -> Tuple['WordGenerator.Result', ...]:
        if item:
            candidates_st: Tuple[StemTransition, ...] = self.stem_transitions.get_transitions_for_item(item)
            return self.generate(candidates=candidates_st, morphemes=morphemes)
        # no item means generate(List<StemTransition> candidates, List<Morpheme> morphemes) is called
        paths: List['WordGenerator.GenerationPath'] = []

        for candidate in candidates:
            search_path: SearchPath = SearchPath.initial_path(candidate, " ")
            # morphemes_in_path: Tuple[Morpheme]
            if len(morphemes) > 0:
                if morphemes[0] == search_path.current_state.morpheme:
                    morphemes_in_path = morphemes[1:]
                else:
                    morphemes_in_path = morphemes
            else:
                morphemes_in_path = ()

            paths.append(WordGenerator.GenerationPath(search_path, morphemes_in_path))

        # search graph
        result_paths: Tuple['WordGenerator.GenerationPath'] = self.search(paths)
        result: List['WordGenerator.Result'] = []

        for path in result_paths:
            analysis = SingleAnalysis.from_search_path(path.path)
            result.append(WordGenerator.Result(analysis.surface_form(), analysis))

        return tuple(result)
Пример #2
0
    def analyze(self, inp: str) -> Tuple[SingleAnalysis, ...]:
        if self.debug_mode:
            raise NotImplementedError("Debug mode is not implemented")

        candidates = self.stem_transitions.get_prefix_matches(
            inp, self.ascii_tolerant)

        paths: List[SearchPath] = []

        for candidate in candidates:
            length = len(candidate.surface)
            tail = inp[length:]
            paths.append(SearchPath.initial_path(candidate, tail))

        result_paths: Tuple[SearchPath] = self.search(paths)
        result: List[SingleAnalysis] = []

        for path in result_paths:
            analysis: SingleAnalysis = SingleAnalysis.from_search_path(path)
            result.append(analysis)

        return tuple(result)