Skip to content

Commit

Permalink
[add] adding the --translators argument to the CLI [fix] normalizing …
Browse files Browse the repository at this point in the history
…the translator name before passing it to the string similarity search
  • Loading branch information
Animenosekai committed May 28, 2022
1 parent 8206019 commit 440e0cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions translatepy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@


def main():
dl = translatepy.Translator()

# Create the parser
parser = argparse.ArgumentParser(prog='translatepy', description='Translate, transliterate, get the language of texts in no time with the help of multiple APIs!')

parser.add_argument('--version', '-v', action='version', version=translatepy.__version__)
parser.add_argument("--translators", action="store", type=str, help="List of translators to use. Each translator name should be comma-separated.", required=False, default=None)

# subparser = parser.add_subparsers(help='Actions', dest="action", required=True)
subparser = parser.add_subparsers(help='Actions', dest="action")
Expand Down Expand Up @@ -62,6 +61,11 @@ def main():
print(NO_ACTION)
return

if args.translators is not None:
dl = translatepy.Translator(args.translators.split(","))
else:
dl = translatepy.Translator()

if args.action == 'translate':
try:
result = dl.translate(text=args.text, destination_language=args.dest_lang, source_language=args.source_lang)
Expand Down
5 changes: 4 additions & 1 deletion translatepy/utils/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import builtins
import sys
from translatepy.exceptions import UnknownTranslator
from translatepy.language import LANGUAGE_CLEANUP_REGEX

from translatepy.translators import (BingTranslate, DeeplTranslate,
GoogleTranslate, GoogleTranslateV1,
Expand All @@ -15,6 +16,7 @@
ReversoTranslate, TranslateComTranslate,
YandexTranslate)
from translatepy.translators.base import BaseTranslator
from translatepy.utils.sanitize import remove_spaces
from translatepy.utils.similarity import fuzzy_search, StringVector
from translatepy.utils._importer_data import VECTORS

Expand Down Expand Up @@ -188,7 +190,8 @@ def get_translator(translator: str, threshold: float = 90, forceload: bool = Fal
return result
except ImportError: # this also catches ErrorDuringImport
pass
alias, similarity = fuzzy_search(LOADED_VECTORS, translator)
normalized = remove_spaces(LANGUAGE_CLEANUP_REGEX.sub("", translator.lower()))
alias, similarity = fuzzy_search(LOADED_VECTORS, normalized)
similarity *= 100
result = VECTORS[alias]["t"]
if similarity < threshold:
Expand Down

0 comments on commit 440e0cb

Please sign in to comment.