Ejemplo n.º 1
0
    def translate(self, text: str, mark_unknown: bool = False, formatting: Optional[str] = None, deformat: str = 'txt', reformat: str = 'txt') -> str:
        """
        Args:
            text (str)
            mark_unknown (bool)
            formatting (Optional[str])
            deformat (str)
            reformat (str)

        Returns:
            str
        """
        if '{}-{}'.format(*map(to_alpha3_code, [self.l1, self.l2])) in apertium.pairs:
            pair = map(to_alpha3_code, [self.l1, self.l2])
        else:
            raise apertium.ModeNotInstalled()

        if pair is not None:
            l1, l2 = pair
            cmds = list(self._get_commands(l1, l2))
            unsafe_deformat, unsafe_reformat = self._get_format(formatting, deformat, reformat)
            deformater, reformater = self._validate_formatters(unsafe_deformat, unsafe_reformat)
            deformatted = self._get_deformat(str(deformater), text)
            output = execute_pipeline(deformatted, cmds)
            result = self._get_reformat(str(reformater), output).strip()
            return result.decode()  # type: ignore
Ejemplo n.º 2
0
    def analyze(self, in_text: str, formatting: str = 'txt') -> List[LexicalUnit]:
        """
        Runs apertium to analyze the input

        Args:
            in_text (str)
            formatting (str)

        Returns:
            List[LexicalUnit]
        """
        self._get_commands()
        deformatter = ['apertium-des{}'.format(formatting), '-n']  # type: List[str]
        if deformatter not in self.analyzer_cmds[self.lang]:
            self.analyzer_cmds[self.lang].insert(0, deformatter)
        result = execute_pipeline(in_text, self.analyzer_cmds[self.lang])  # type: str
        return self._postproc_text(result)
Ejemplo n.º 3
0
    def generate(self,
                 in_text: str,
                 formatting: str = 'none') -> Union[str, List[str]]:
        """
        Args:
            in_text (str)
            formatting (str)

        Returns:
            Union[str, List[str]]
        """
        self.lang = to_alpha3_code(self.lang)

        if self.lang in apertium.generators:
            commands = list(self._get_commands())
            result = execute_pipeline(in_text, commands)
            return result.rstrip('\x00')
        else:
            raise apertium.ModeNotInstalled(self.lang)