wmt-2020-pl-en/translate

45 lines
1.5 KiB
Plaintext
Raw Normal View History

2015-06-05 16:43:05 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import sys
2015-06-05 16:43:05 +02:00
from googletrans import translator
PY3 = sys.version_info > (3, 1)
2015-06-05 16:43:05 +02:00
def main():
parser = argparse.ArgumentParser(
description='Python Google Translator as a command-line tool')
parser.add_argument('text', help='The text you want to translate.')
parser.add_argument('-d', '--dest', default='en',
help='The destination language you want to translate. (Default: en)')
parser.add_argument('-s', '--src', default='auto',
help='The source language you want to translate. (Default: auto)')
parser.add_argument('-c', '--detect', action='store_true', default=False,
help='')
2015-06-05 16:43:05 +02:00
args = parser.parse_args()
if args.detect:
result = translator.detect(args.text)
result = """
[{lang}, {confidence}] {text}
""".strip().format(text=args.text,
lang=result.lang, confidence=result.confidence)
print(result)
return
2015-06-05 16:43:05 +02:00
result = translator.translate(args.text, dest=args.dest, src=args.src)
text = result.text if PY3 else result.text.encode('utf-8', 'ignore')
pronunciation = result.pronunciation if PY3 else result.pronunciation.encode('utf-8', 'ignore')
2015-06-05 16:43:05 +02:00
result = """
[{src}] {original}
->
[{dest}] {text}
[pron.] {pronunciation}
2015-07-16 19:54:59 +02:00
""".strip().format(src=result.src, dest=result.dest, original=result.origin,
text=text,
pronunciation=pronunciation)
2015-06-05 16:43:05 +02:00
print(result)
if __name__ == '__main__':
main()