commit 4f23be0dfe30945604b76a64d643fc290f531f23 Author: vaydheesh Date: Sat Jun 15 01:07:32 2019 +0000 Added: 'lt-proc -g' generate rename: BuildSwigWrapper -> build-swig-wrapper diff --git a/.coveragerc b/.coveragerc deleted file mode 100644 index 4d525f7..0000000 --- a/.coveragerc +++ /dev/null @@ -1,2 +0,0 @@ -[run] -omit=apertium/swig/* \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index a4d0b3e..014be6f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ python: install: - pip install pipenv - travis_retry pipenv install --dev --system - - ./BuildSwigWrapper.sh + - ./build-swig-wrapper.sh before_script: - wget http://apertium.projectjj.com/apt/install-nightly.sh -O - | sudo bash - sudo apt-get -f --allow-unauthenticated install apertium-all-dev diff --git a/apertium/lttoolbox.py b/apertium/lttoolbox.py index d9c6461..310a947 100644 --- a/apertium/lttoolbox.py +++ b/apertium/lttoolbox.py @@ -1,5 +1,5 @@ import tempfile -from typing import ByteString # noqa: F401 +from typing import ByteString, List # noqa: F401 import lttoolbox @@ -7,52 +7,45 @@ import lttoolbox class LtProc: """ Attributes: - arg_index (int) + command (List) path (str) input_text (str) output_text (str) """ - def __init__(self, input_text: str, arg_index: str, path: str) -> None: + def __init__(self, command: List, input_text: str) -> None: """ Args: + command (List) input_text (str) - arg_index (int) - path (str) """ - self.arg_index = arg_index - self.path = path + self.command = command + self.path = command[-1] self.input_text = input_text self.output_text = '' - def analyze(self) -> None: + def execute(self) -> ByteString: """ - Reads formatted text from apertium-des and returns its analysed text + Executes the required method, depending upon the argument for lt-proc Args: self (LtProc) + Returns: - None + (ByteString) """ with tempfile.NamedTemporaryFile('w') as input_file, tempfile.NamedTemporaryFile('r') as output_file: input_file.write(self.input_text) input_file.flush() + lttoolbox.LtLocale.tryToSetLocale() fst = lttoolbox.FST() + fst.setDictionaryCaseMode(True) if not fst.valid(): raise ValueError('FST Invalid') - fst.analyze(self.path, input_file.name, output_file.name) - self.output_text = output_file.read() - - def execute(self) -> ByteString: - """ - Executes the required method, depending upon the argument for lt-proc - - Args: - self (LtProc) + if '-w' in self.command: + fst.analyze(self.path, input_file.name, output_file.name) + elif '-g' in self.command: + fst.generate(self.path, input_file.name, output_file.name) - Returns: - (ByteString) - """ - if self.arg_index == '-w': - self.analyze() + self.output_text = output_file.read() return self.output_text.encode() diff --git a/apertium/utils.py b/apertium/utils.py index f9b0a12..f6b5e55 100644 --- a/apertium/utils.py +++ b/apertium/utils.py @@ -33,19 +33,14 @@ def execute_pipeline(inp: str, commands: List[List[str]]) -> str: Returns: str """ - procs = [] end = inp.encode() - for i, command in enumerate(commands): - if 'lt-proc' in command and '-w' in command: - arg_index = command.index('-w') - automorf_path = command[-1] - ltp = lttoolbox.LtProc(end.decode(), command[arg_index], automorf_path) - end = ltp.execute() + for command in commands: + if 'lt-proc' in command and ('-w' in command or '-g' in command): + ltp = lttoolbox.LtProc(command, end.decode()) + end = ltp.execute() else: - procs.append( - subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE), - ) - end, _ = procs[i].communicate(end) + proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + end, _ = proc.communicate(end) return end.decode() diff --git a/BuildSwigWrapper.sh b/build-swig-wrapper.sh similarity index 66% rename from BuildSwigWrapper.sh rename to build-swig-wrapper.sh index d42b0fd..e80ea30 100755 --- a/BuildSwigWrapper.sh +++ b/build-swig-wrapper.sh @@ -1,7 +1,7 @@ -#! /bin/bash +#!/usr/bin/env bash sudo apt-get install -y swig build-essential python3-setuptools git clone -b swig_wrapper https://github.com/Vaydheesh/lttoolbox.git -cd lttoolbox || return +cd lttoolbox || set -e ./autogen.sh && ./configure && make -cd python || return +cd python || set -e python3 setup.py install \ No newline at end of file