commit d90860994f04d21f3967421bcd385f9f15a07ccc Author: vaydheesh Date: Wed Jun 5 20:33:04 2019 +0000 Switched to lttoolbox wrapper for analysis/__init__ analysis needs to be installed from lttoolbox use NamedTempFile and pass its path to FST.analyze() diff --git a/apertium/analysis/__init__.py b/apertium/analysis/__init__.py index db19581..74a6368 100644 --- a/apertium/analysis/__init__.py +++ b/apertium/analysis/__init__.py @@ -1,4 +1,7 @@ +import os +import tempfile from streamparser import parse, LexicalUnit # noqa: F401 +import analysis import apertium from apertium.utils import to_alpha3_code, execute, parse_mode_file @@ -20,21 +23,47 @@ class Analyzer: lang (str) """ self.analyzer_cmds = {} # type: Dict[str, List[List[str]]] + self.analyzer_path = [] # type: List[str] self.lang = to_alpha3_code(lang) # type: str if self.lang not in apertium.analyzers: raise apertium.ModeNotInstalled(self.lang) else: self.path, self.mode = apertium.analyzers[self.lang] - def _get_commands(self): # type: (Analyzer) -> List[List[str]] + def _get_path(self): # type: (Analyzer) -> List[str] """ + Read mode file for automorf.bin path + Returns: - List[List[str]] + List[str] """ if self.lang not in self.analyzer_cmds: mode_path, mode = apertium.analyzers[self.lang] - self.analyzer_cmds[self.lang] = parse_mode_file(mode_path+'/modes/'+mode+'.mode') - return self.analyzer_cmds[self.lang] + mode_path = os.path.join(mode_path, "modes", "{}.mode".format(mode)) + self.analyzer_cmds[self.lang] = parse_mode_file(mode_path) + self.analyzer_path = [command[-1] for command in self.analyzer_cmds[self.lang]] + return self.analyzer_path + + @staticmethod + def _lt_proc(input_text, automorf_path): # type: (str, str) -> str + """ + Reads formatted text from apertium-des and returns its analysis + + Args: + input_text (str) + automorf_path (str) + + Returns: + str + """ + with tempfile.NamedTemporaryFile("w") as input_file, tempfile.NamedTemporaryFile("r") as output_file: + input_file.write(input_text) + input_file.flush() + x = analysis.FST() + if not x.valid(): + raise ValueError("FST Invalid") + x.analyze(automorf_path, input_file.name, output_file.name) + return output_file.read() def _postproc_text(self, result): # type: (Analyzer, str) -> List[LexicalUnit] """ @@ -61,7 +90,7 @@ class Analyzer: List[LexicalUnit] """ apertium_des = execute(in_text, [['apertium-des{}'.format(formatting), '-n']]) - result = execute(apertium_des, self._get_commands()) + result = self._lt_proc(apertium_des, self._get_path()[0]) return self._postproc_text(result)