def test_hypotheses(self): lang_one = translate.Language("en", "English") lang_two = translate.Language("es", "Spanish") t1 = translate.IdentityTranslation(lang_one) t2 = translate.IdentityTranslation(lang_two) composite = translate.CompositeTranslation(t1, t2) output = composite.hypotheses("This is some text", 1) assert len(output) == 1 assert isinstance(output[0], translate.Hypothesis) assert output[0].score == 0 assert output[0].value == "This is some text"
def test_hypotheses(self): lang = translate.Language("es", "Spanish") identity_translation = translate.IdentityTranslation(lang) returned = identity_translation.hypotheses("this is some text", 1) for single_return in returned: assert isinstance(single_return, translate.Hypothesis) assert single_return.score == 0 assert single_return.value == "this is some text"
def main(): """Run Argos Translate command line interface""" # Parse args parser = argparse.ArgumentParser(description="Open-source offline translation.\n") parser.add_argument( "text", nargs="?", metavar="TEXT", help="The text to translate. Read from standard input if missing.", ) parser.add_argument( "--from-lang", "-f", help="The code for the language to translate from (ISO 639-1)", ) parser.add_argument( "--to-lang", "-t", help="The code for the language to translate to (ISO 639-1)" ) args = parser.parse_args() from_and_to_lang_provided = args.from_lang is not None and args.to_lang is not None # Get text to translate if args.text: # argos-translate-cli --from-lang en --to-lang es "Text to translate" text_to_translate = args.text elif from_and_to_lang_provided: # echo "Text to translate" | argos-translate-cli --from-lang en --to-lang es text_to_translate = "".join(sys.stdin) else: # argos-translate parser.print_help() return # Perform translation if from_and_to_lang_provided: installed_languages = { lang.code: lang for lang in translate.load_installed_languages() } if args.from_lang not in installed_languages: parser.error("{!r} is not an installed language.".format(args.from_lang)) if args.to_lang not in installed_languages: parser.error("{!r} is not an installed language.".format(args.to_lang)) from_lang = installed_languages[args.from_lang] to_lang = installed_languages[args.to_lang] translation = from_lang.get_translation(to_lang) if translation is None: parser.error( f"No translation installed from {args.from_name} to {args.to_name}" ) else: translation = translate.IdentityTranslation("") # Print translation print(translation.translate(text_to_translate))
def main(): # Parse args parser = argparse.ArgumentParser() parser.add_argument('text', nargs='?', help='The text to translate') parser.add_argument( '--from-lang', help='The code for the language to translate from (ISO 639-1)') parser.add_argument( '--to-lang', help='The code for the language to translate to (ISO 639-1)') args = parser.parse_args() from_and_to_lang_provided = args.from_lang != None and args.to_lang != None # Get text to translate if args.text: # argos-translate-cli --from-lang en --to-lang es "Text to translate" text_to_translate = args.text elif from_and_to_lang_provided: # echo "Text to translate" | argos-translate-cli --from-lang en --to-lang es text_to_translate = '' for line in sys.stdin: text_to_translate += line else: # argos-translate parser.print_help() return # Perform translation if from_and_to_lang_provided: installed_languages = translate.load_installed_languages() from_lang_index = None for i, lang in enumerate(installed_languages): if lang.code == args.from_lang: from_lang_index = i break to_lang_index = None for i, lang in enumerate(installed_languages): if lang.code == args.to_lang: to_lang_index = i break from_lang = installed_languages[from_lang_index] to_lang = installed_languages[to_lang_index] translation = from_lang.get_translation(to_lang) if translation == None: sys.exit('No translation installed from {} to {}'.format( args.from_name, args.to_name)) else: translation = translate.IdentityTranslation('') # Print translation print(translation.translate(text_to_translate))
def main(): # Parse args parser = argparse.ArgumentParser( description='Open-source offline translation.\n') parser.add_argument('text', nargs='?', metavar='TEXT', help='The text to translate. Read from standard input if missing.') parser.add_argument('--from-lang', '-f', help='The code for the language to translate from (ISO 639-1)') parser.add_argument('--to-lang', '-t', help='The code for the language to translate to (ISO 639-1)') args = parser.parse_args() from_and_to_lang_provided = args.from_lang is not None and args.to_lang is not None # Get text to translate if args.text: # argos-translate-cli --from-lang en --to-lang es "Text to translate" text_to_translate = args.text elif from_and_to_lang_provided: # echo "Text to translate" | argos-translate-cli --from-lang en --to-lang es text_to_translate = ''.join(sys.stdin) else: # argos-translate parser.print_help() return # Perform translation if from_and_to_lang_provided: installed_languages = { lang.code: lang for lang in translate.load_installed_languages()} if args.from_lang not in installed_languages: parser.error('{!r} is not an installed language.'.format( args.from_lang)) if args.to_lang not in installed_languages: parser.error('{!r} is not an installed language.'.format( args.to_lang)) from_lang = installed_languages[args.from_lang] to_lang = installed_languages[args.to_lang] translation = from_lang.get_translation(to_lang) if translation is None: parser.error(f'No translation installed from {args.from_name} to {args.to_name}') else: translation = translate.IdentityTranslation('') # Print translation print(translation.translate(text_to_translate))
def main(): # Parse args parser = argparse.ArgumentParser() parser.add_argument('text', help='The text to translate') parser.add_argument( '--from-lang', help='The code for the language to translate from (ISO 639-1)') parser.add_argument( '--to-lang', help='The code for the language to translate to (ISO 639-1)') args = parser.parse_args() # Perform translation if args.from_lang != None and args.to_lang != None: installed_languages = translate.load_installed_languages() from_lang_index = None for i, lang in enumerate(installed_languages): if lang.code == args.from_lang: from_lang_index = i break to_lang_index = None for i, lang in enumerate(installed_languages): if lang.code == args.to_lang: to_lang_index = i break from_lang = installed_languages[from_lang_index] to_lang = installed_languages[to_lang_index] translation = from_lang.get_translation(to_lang) if translation == None: sys.exit('No translation installed from {} to {}'.format( args.from_name, args.to_name)) else: translation = translate.IdentityTranslation('') # Print translation print(translation.translate(args.text))