Skip to content

Commit d908609

Browse files
Switched to lttoolbox wrapper for analysis/__init__
analysis needs to be installed from lttoolbox use NamedTempFile and pass its path to FST.analyze()
1 parent f3093f2 commit d908609

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

apertium/analysis/__init__.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import os
2+
import tempfile
13
from streamparser import parse, LexicalUnit # noqa: F401
4+
import analysis
25

36
import apertium
47
from apertium.utils import to_alpha3_code, execute, parse_mode_file
@@ -20,21 +23,47 @@ def __init__(self, lang): # type: (Analyzer, str) -> None
2023
lang (str)
2124
"""
2225
self.analyzer_cmds = {} # type: Dict[str, List[List[str]]]
26+
self.analyzer_path = [] # type: List[str]
2327
self.lang = to_alpha3_code(lang) # type: str
2428
if self.lang not in apertium.analyzers:
2529
raise apertium.ModeNotInstalled(self.lang)
2630
else:
2731
self.path, self.mode = apertium.analyzers[self.lang]
2832

29-
def _get_commands(self): # type: (Analyzer) -> List[List[str]]
33+
def _get_path(self): # type: (Analyzer) -> List[str]
3034
"""
35+
Read mode file for automorf.bin path
36+
3137
Returns:
32-
List[List[str]]
38+
List[str]
3339
"""
3440
if self.lang not in self.analyzer_cmds:
3541
mode_path, mode = apertium.analyzers[self.lang]
36-
self.analyzer_cmds[self.lang] = parse_mode_file(mode_path+'/modes/'+mode+'.mode')
37-
return self.analyzer_cmds[self.lang]
42+
mode_path = os.path.join(mode_path, "modes", "{}.mode".format(mode))
43+
self.analyzer_cmds[self.lang] = parse_mode_file(mode_path)
44+
self.analyzer_path = [command[-1] for command in self.analyzer_cmds[self.lang]]
45+
return self.analyzer_path
46+
47+
@staticmethod
48+
def _lt_proc(input_text, automorf_path): # type: (str, str) -> str
49+
"""
50+
Reads formatted text from apertium-des and returns its analysis
51+
52+
Args:
53+
input_text (str)
54+
automorf_path (str)
55+
56+
Returns:
57+
str
58+
"""
59+
with tempfile.NamedTemporaryFile("w") as input_file, tempfile.NamedTemporaryFile("r") as output_file:
60+
input_file.write(input_text)
61+
input_file.flush()
62+
x = analysis.FST()
63+
if not x.valid():
64+
raise ValueError("FST Invalid")
65+
x.analyze(automorf_path, input_file.name, output_file.name)
66+
return output_file.read()
3867

3968
def _postproc_text(self, result): # type: (Analyzer, str) -> List[LexicalUnit]
4069
"""
@@ -61,7 +90,7 @@ def analyze(self, in_text, formatting='txt'): # type: (Analyzer, str, str) -> L
6190
List[LexicalUnit]
6291
"""
6392
apertium_des = execute(in_text, [['apertium-des{}'.format(formatting), '-n']])
64-
result = execute(apertium_des, self._get_commands())
93+
result = self._lt_proc(apertium_des, self._get_path()[0])
6594
return self._postproc_text(result)
6695

6796

0 commit comments

Comments
 (0)