1
+ import os
2
+ import tempfile
1
3
from streamparser import parse , LexicalUnit # noqa: F401
4
+ import analysis
2
5
3
6
import apertium
4
7
from apertium .utils import to_alpha3_code , execute , parse_mode_file
@@ -20,21 +23,47 @@ def __init__(self, lang): # type: (Analyzer, str) -> None
20
23
lang (str)
21
24
"""
22
25
self .analyzer_cmds = {} # type: Dict[str, List[List[str]]]
26
+ self .analyzer_path = [] # type: List[str]
23
27
self .lang = to_alpha3_code (lang ) # type: str
24
28
if self .lang not in apertium .analyzers :
25
29
raise apertium .ModeNotInstalled (self .lang )
26
30
else :
27
31
self .path , self .mode = apertium .analyzers [self .lang ]
28
32
29
- def _get_commands (self ): # type: (Analyzer) -> List[List[ str] ]
33
+ def _get_path (self ): # type: (Analyzer) -> List[str]
30
34
"""
35
+ Read mode file for automorf.bin path
36
+
31
37
Returns:
32
- List[List[ str] ]
38
+ List[str]
33
39
"""
34
40
if self .lang not in self .analyzer_cmds :
35
41
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 ()
38
67
39
68
def _postproc_text (self , result ): # type: (Analyzer, str) -> List[LexicalUnit]
40
69
"""
@@ -61,7 +90,7 @@ def analyze(self, in_text, formatting='txt'): # type: (Analyzer, str, str) -> L
61
90
List[LexicalUnit]
62
91
"""
63
92
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 ] )
65
94
return self ._postproc_text (result )
66
95
67
96
0 commit comments