Skip to content

Commit d4fcf2a

Browse files
singh-lokendrasushain97
authored andcommitted
Wrapper for lrx-proc -m (#46)
* Added: Wrapper for lrx-proc -m * Implemented changes suggested build-swig-wrapper: clone lttoolbox for building wrapper utils.py: Better name for flags
1 parent 74fcc51 commit d4fcf2a

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ python:
1010
install:
1111
- pip install pipenv
1212
- travis_retry pipenv install --dev --system
13-
- ./build-swig-wrapper.sh
1413
before_script:
1514
- wget http://apertium.projectjj.com/apt/install-nightly.sh -O - | sudo bash
1615
- sudo apt-get -f --allow-unauthenticated install apertium-all-dev
1716
- sudo apt-get -f --allow-unauthenticated install apertium-nno-nob apertium-es-en apertium-eng
17+
- ./build-swig-wrapper.sh
1818
script:
1919
- flake8 --verbose apertium
2020
- if [[ $TRAVIS_PYTHON_VERSION != 3.4 ]]; then

apertium/utils.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import os
12
import subprocess
23
import tempfile
34
from typing import List
45

6+
import lextools
57
import lttoolbox
68

79
import apertium # noqa: F401
@@ -27,32 +29,48 @@ def to_alpha3_code(code: str) -> str:
2729

2830
def execute_pipeline(inp: str, commands: List[List[str]]) -> str:
2931
"""
30-
Args:
31-
inp (str)
32-
commands (List[List[str]])
32+
Executes the given list of commands and returns the final output
3333
3434
Returns:
3535
str
3636
"""
3737
end = inp.encode()
3838
for command in commands:
39+
# On Windows platform the file can't be opened once again
40+
# The file is first opened in python for writing and then
41+
# opened again with swig wrapper
42+
# NamedTemporaryFile gets deleted (if delete=True) upon closing,
43+
# so manually delete the file afterwards
44+
input_file = tempfile.NamedTemporaryFile(delete=False)
45+
output_file = tempfile.NamedTemporaryFile(delete=False)
46+
arg = command[1][1] if len(command) == 3 else ''
47+
path = command[-1]
48+
used_wrapper = True
49+
input_file_name, output_file_name = input_file.name, output_file.name
50+
with open(input_file_name, 'w') as input_file:
51+
text = end.decode()
52+
input_file.write(text)
3953
if 'lt-proc' == command[0]:
40-
arg = command[1][1] if len(command) == 3 else ''
41-
path = command[-1]
42-
with tempfile.NamedTemporaryFile('w') as input_file, tempfile.NamedTemporaryFile('r') as output_file:
43-
text = end.decode()
44-
input_file.write(text)
45-
input_file.flush()
46-
lttoolbox.LtLocale.tryToSetLocale()
47-
fst = lttoolbox.FST()
48-
if not fst.valid():
49-
raise ValueError('FST Invalid')
50-
fst.lt_proc(arg, path, input_file.name, output_file.name)
51-
end = output_file.read().encode()
54+
lttoolbox.LtLocale.tryToSetLocale()
55+
fst = lttoolbox.FST()
56+
if not fst.valid():
57+
raise ValueError('FST Invalid')
58+
fst = lttoolbox.FST()
59+
fst.lt_proc(arg, path, input_file_name, output_file_name)
60+
elif 'lrx-proc' == command[0]:
61+
lextools.LtLocale.tryToSetLocale()
62+
lrx = lextools.LRX()
63+
lrx.lrx_proc(arg, path, input_file.name, output_file.name)
5264
else:
5365
apertium.logger.warning('Calling subprocess %s', command[0])
5466
proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
5567
end, _ = proc.communicate(end)
68+
used_wrapper = False
69+
if used_wrapper:
70+
with open(output_file_name) as output_file:
71+
end = output_file.read().encode()
72+
os.remove(input_file_name)
73+
os.remove(output_file_name)
5674
return end.decode()
5775

5876

build-swig-wrapper.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
set -e
33

44
sudo apt-get install -y swig build-essential python3-setuptools
5-
git clone -b swig_wrapper https://github.com/Vaydheesh/lttoolbox.git
5+
git clone https://github.com/apertium/lttoolbox.git
66
cd lttoolbox
77
./autogen.sh --enable-python-bindings && make
88
cd python
99
python3 setup.py install
10+
11+
git clone -b swig_wrapper https://github.com/Vaydheesh/apertium-lex-tools.git
12+
cd apertium-lex-tools
13+
./autogen.sh --enable-python-bindings && make
14+
cd python
15+
python3 setup.py install

0 commit comments

Comments
 (0)