versi 0.1.1 translate from file and save result to file

This commit is contained in:
Nestero 2022-01-05 14:10:36 +07:00
parent 22b9c7f804
commit d6eba3b84b
3 changed files with 33 additions and 4 deletions

View File

@ -10,7 +10,7 @@ pip install pylingva
## CLI ## CLI
```shell ```shell
$ translate -h $ translate -h
usage: translate [-h] [-s SOURCE] [-t TARGET] [-txt TEXT] [-ll] usage: translate [-h] [-s SOURCE] [-t TARGET] [-txt TEXT] [-ll] [-f FILE] [-o OUTPUT]
options: options:
-h, --help show this help message and exit -h, --help show this help message and exit
@ -22,9 +22,18 @@ options:
Text to translate Text to translate
-ll, --list-languages -ll, --list-languages
List Languages support List Languages support
-f FILE, --file FILE Path file .txt to translate
-o OUTPUT, --output OUTPUT
Output file translation result
$ translate -s auto -t id -txt "How are You ?" $ translate -s auto -t id -txt "How are You ?"
Apa kabar ? Apa kabar ?
$ translate -s auto -t id -txt "How are You ?" -o output.txt
Translation result saved in output.txt
$ translate -s auto -t id -f input.txt -o output.txt
Translation result saved in output.txt
``` ```
## Python ## Python
### List languages ### List languages

View File

@ -4,14 +4,17 @@ from pylingva import pylingva
import argparse import argparse
def translate(): def translate():
translate = pylingva()
arg = argparse.ArgumentParser() arg = argparse.ArgumentParser()
arg.add_argument("-s", "--source", type=str, help="Source Language to translate") arg.add_argument("-s", "--source", type=str, help="Source Language to translate")
arg.add_argument("-t", "--target", type=str, help="Target Language to translate") arg.add_argument("-t", "--target", type=str, help="Target Language to translate")
arg.add_argument("-txt", "--text", type=str, help="Text to translate") arg.add_argument("-txt", "--text", type=str, help="Text to translate")
arg.add_argument("-ll", "--list-languages", help="List Languages support", action="store_true") arg.add_argument("-ll", "--list-languages", help="List Languages support", action="store_true")
arg.add_argument("-f", "--file", help="Path file .txt to translate")
arg.add_argument("-o", "--output", help="Output file translation result")
args = arg.parse_args() args = arg.parse_args()
translate = pylingva()
if args.list_languages: if args.list_languages:
lang = translate.languages() lang = translate.languages()
print("{:<25} {:<25}".format('Name', 'Code')) print("{:<25} {:<25}".format('Name', 'Code'))
@ -19,6 +22,23 @@ def translate():
x = key x = key
y = value y = value
print("{:<25} {:<25}".format(x, y)) print("{:<25} {:<25}".format(x, y))
elif args.file != None and args.output == None:
with open(args.file, "r") as f:
t = f.read()
result = translate.translate(args.source, args.target, t)
print(result)
elif args.output != None and args.file == None:
result = translate.translate(args.source, args.target, args.text)
with open(args.output, "w") as o:
o.write(result)
print("Translation result saved in", args.output)
elif args.file != None and args.output != None:
with open(args.file, "r") as f:
t = f.read()
result = translate.translate(args.source, args.target, t)
with open(args.output, "w") as o:
o.write(result)
print("Translation result saved in", args.output)
else: else:
result = translate.translate(args.source, args.target, args.text) result = translate.translate(args.source, args.target, args.text)
print(result) print(result)

View File

@ -7,7 +7,7 @@ with open("README.md", "r") as des:
setup( setup(
name='pylingva', name='pylingva',
packages=find_packages(), packages=find_packages(),
version='0.1.0', version='0.1.1',
entry_points={'console_scripts': ['translate = pylingva.cli:translate']}, entry_points={'console_scripts': ['translate = pylingva.cli:translate']},
description='Simple translator using Lingva Translate API', description='Simple translator using Lingva Translate API',
url='https://gitlab.com/nesstero/pylingva', url='https://gitlab.com/nesstero/pylingva',