Exemplo n.º 1
0
def translate_google(text = 'hello world', source_lng = 'en', dest_lng = 'ja'):
  translator = GoogleTranslator(service_urls=[
      'translate.google.com',
      'translate.google.co.kr',
      'translate.google.fr',
      'translate.google.ch',], proxies={'http': '37.26.136.181:40076', 'http':'138.197.145.103:8080', 'http': '165.227.215.71:8080', 'http':'88.198.24.108:8080'})
  result = translator.translate(text, dest=dest_lng)
  return result
Exemplo n.º 2
0
def get_translate_fn(src_lang="zh-CN", dst_lang="en"):
    """Returns a function that google-translates text from a
    source to a target language. This implementation also caches
    its results."""
    from googletrans import Translator as GoogleTranslator
    translator = GoogleTranslator()

    @lru_cache(maxsize=None)
    def google_translate(text):
        return translator.translate(text, src=src_lang, dest=dst_lang).text

    return google_translate
Exemplo n.º 3
0
 def __init__(self, source_language, destination_language, translator="microsoft"):
     self.translator_provider = translator
     if translator == "microsoft":
         self.translator = MicrosoftTranslator(
             to_lang=destination_language,
             from_lang=source_language,
             provider="microsoft",
             secret_access_key="8d9718e3499d479fb5cfee8e17e6f0d9",
         )
     if translator == "google":
         self.to_lang = destination_language
         self.from_lang = source_language
         self.translator = GoogleTranslator(
             service_urls=["translate.google.com", "translate.google.co.kr"]
         )
Exemplo n.º 4
0
def google_translate(sentence_lst):
    input_chunk_lst = make_chunks(sentence_lst)
    trans = GoogleTranslator()
    translated_sentence_lst = []

    for en_chunk in tqdm(input_chunk_lst):
        kr_chunk = trans.translate(en_chunk, src='en', dest='ko')
        kr_chunk = kr_chunk.text
        kr_sentences = kr_chunk.split("\r\n")
        if kr_sentences[-1] == "":
            kr_sentences = kr_sentences[:-1]
        time.sleep(GOOGLE_TIME_TO_SLEEP)

        translated_sentence_lst.extend(kr_sentences)

    return translated_sentence_lst
Exemplo n.º 5
0
    def __init__(self,
                 src='en',
                 dest='it',
                 disk=False,
                 write=False,
                 translation_dir='./translations'):
        """

        Args:
            src             (str ): source translation language
            dest            (str ): dest   translation language

            disk            (bool): True = load translation from disk
                                           Note: it needs vocabularies.
                                           Use generate_vocabularies() for that.
                                           Set translation_dir to the correct
                                           path where you saved your
                                           vocabularies.
                                   False = load translation from web (Google)

            write           (bool): if translations comes from web (disk=False)
                                    it writes translations on the disk. So it
                                    will generate vocabularies on the fly.
                                    Set translation_dir to the path where you
                                    want to save your vocabularies.
                                    It could overwrite your old vocabularies.
                                    if translations comes from disk (disk=True)
                                    and a file vocabulary does not exist it'll
                                    translate that one from web and then store
                                    ii on the disk.

            translation_dir (str ): dir where are located or where will be
                                    created tranlations tree with vocabularies
                                    files. Use this var in conjunction with disk
                                    or write var.
        """
        self.TRANSLATION_DIR = translation_dir
        self.src = src
        self.dest = dest
        self.disk = disk
        self.write = write
        self.gt = GoogleTranslator()
        if self.disk == True or self.write == True:
            # if write in or load from vocabularies
            # i need to know where is located on the disk
            if self.TRANSLATION_DIR is None:
                raise ValueError("You MUST set translation_dir in __init__\n")
Exemplo n.º 6
0
 def __init__(self,
              source_language,
              destination_language,
              translator="microsoft"):
     self.translator_provider = translator
     if translator == "microsoft":
         self.translator = MicrosoftTranslator(
             to_lang=destination_language,
             from_lang=source_language,
             provider="microsoft",
             secret_access_key="cf0e74ec363149678a101dc4dad90b5a",
         )
     if translator == "google":
         self.to_lang = destination_language
         self.from_lang = source_language
         self.translator = GoogleTranslator(service_urls=[
             "translate.google.com", "translate.google.co.kr"
         ])
Exemplo n.º 7
0
def translate_sentence():
    if request.method == 'POST':
        data = json.loads(request.form['data'])

        text = data['translate_input']
        src = languages[data['language_input']]
        dest = languages[data['language_output']]

        if data['chosen_model'] == 'Google Translate':
            translator = GoogleTranslator()
            res = translator.translate(text, src=src, dest=dest)
            return jsonify(translation=res.text)

        elif data['chosen_model'] == 'My Memory':
            translator = MyMemoryTranslator(provider='mymemory',
                                            from_lang=src,
                                            to_lang=dest)
            res = translator.translate(text)
            return jsonify(translation=res)
Exemplo n.º 8
0
from googletrans import Translator as GoogleTranslator

translator = GoogleTranslator(service_urls=[
    'translate.google.com', 'translate.google.co.kr', 'translate.google.cn'
])


def translate(input_txt, dest):
    translation = translator.translate(input_txt, dest=dest)
    return translation
Exemplo n.º 9
0
import re
from typing import List, Tuple
from googletrans import Translator as GoogleTranslator

google_translator = GoogleTranslator()
CHARACTER_SIZE_PER_LINE = 40


def is_comment_out(line: str) -> bool:
    return re.match(r'^\s*//.*$', line) is not None


def is_url_comment_out(line: str) -> bool:
    return re.match(r'^\s*//\s*https?://.*', line) is not None


def get_comment(idx: int, lines: List[str]) -> Tuple[str, List[int]]:
    comment_list: List[str] = []
    idx_list: List[int] = []
    for sub_idx in list(map(lambda i: i + idx, range(len(lines) - idx))):
        line: str = lines[sub_idx]
        if is_url_comment_out(line):
            break
        elif is_comment_out(line):
            is_first_line: bool = (idx == sub_idx)
            comment: str = _extract_comment_from_line(line, is_first_line)
            comment_list.append(comment)
            idx_list.append(sub_idx)
        else:
            break
    return " ".join(comment_list), idx_list
Exemplo n.º 10
0
 def SetupTranslator(self):
   self.google_translator = GoogleTranslator()
   return True
Exemplo n.º 11
0
    def bulk_generate_vocabularies(cls,
                                   TRANSLATION_DIR='./translations',
                                   INDEX={},
                                   src_dest=[
                                       ('en', 'it'),
                                   ],
                                   delay=2 * 60):
        """Create translations (vocabularies) for all the classes in INDEX

        A translations request is sent for all the classes in a module.
        A delay is applied before proceding wiht the next requests.

        SummaryModel.__t = {
                            "__t_from" : "From",
                            "__t_to" : "To",
                            "__t_parts" : "Parts",
                            "__t_locations" : "Locations",
                            "__t_available_languages" : "Available Languages",
                           }

        SessionModel.__t = {
                            "__t_starts" : "Starts",
                            "__t_end" : "End",
                            "__t_break" : "Breaks",
                            "__t_sesion_location" : "Session Location",
                            "__t_participants": "Participants",
                            "__t_signatures": "SIGNATURES",
                            "__t_part": "Part",
                            "__t_observations": "Observations",
                           }

        src_dest:

            [('en','it'),('en','fr'),]

        self.INDEX:
            A dict where keys are modules and values classes in that module

            {'sign_in_sheets_translator': ['SummaryModel', 'SessionModel']}

        __t_index:
            A dict containing __t for each class in a module

            {
                'SummaryModel': {
                    '__t_from': 'From',
                    '__t_to': 'To',
                    '__t_parts': 'Parts',
                    '__t_locations': 'Locations',
                    '__t_available_languages': 'Available Languages'
                },
                'SessionModel': {
                    '__t_starts': 'Starts',
                    '__t_end': 'End',
                    '__t_break': 'Breaks',
                    '__t_sesion_location':'Session Location',
                    '__t_participants': 'Participants',
                    '__t_signatures': 'SIGNATURES',
                    '__t_part': 'Part',
                    '__t_observations': 'Observations'
                }
            }

        inverted_index:

            {
                'From': ['__t_from'],
                'To': ['__t_to'],
                'Parts': ['__t_parts'],
                'Locations': ['__t_locations'],
                'Available Languages': ['__t_available_languages'],
                'Starts': ['__t_starts'],
                'End': ['__t_end'],
                'Breaks': ['__t_break'],
                'Session Location': ['__t_sesion_location'],
                'Participants': ['__t_participants'],
                'SIGNATURES': ['__t_signatures'],
                'Part': ['__t_part'],
                'Observations': ['__t_observations']
            }

        list_of_words:
            All the words to be translated

            ['From', 'To', 'Parts', 'Locations', 'Available Languages',
             'Starts', 'End', 'Breaks', 'Session Location', 'Participants',
             'SIGNATURES', 'Part', 'Observations']


        translated:
            A dict containing all the translations for all
            the classes in a module

        (en-it)
            {
                '__t_from': 'A partire dal',
                '__t_to': 'A',
                '__t_parts': 'Parti',
                '__t_locations': 'sedi',
                '__t_available_languages': 'Lingue disponibili',
                '__t_starts': 'inizia',
                '__t_end': 'Fine',
                '__t_break': 'Pause',
                '__t_sesion_location': 'Posizione della sessione',
                '__t_participants': 'I partecipanti',
                '__t_signatures': 'FIRME',
                '__t_part': 'Parte',
                '__t_observations': 'osservazioni'
            }
        """
        def get_module_name_and_path(absolute_path):
            splitted = absolute_path.split('/')
            module_name = splitted[-1]
            path = "/".join(splitted[:-1])
            return module_name, path

        for elem in src_dest:
            for a_module in INDEX:
                if "/" in a_module:
                    module_name, path = get_module_name_and_path(a_module)
                    if path not in sys.path:
                        sys.path.append(path)
                else:
                    module_name = a_module
                list_clsses_in_a_module = INDEX[a_module]
                inverted_index = {}
                translated = {}
                list_of_words = []
                __t_index = {}
                for a_class in list_clsses_in_a_module:
                    import_statement = "from {a_module} import {a_class}".format(
                        a_module=module_name, a_class=a_class)
                    exec(import_statement)
                    class_attribute_name = "_{}__t".format(a_class)
                    __t = getattr(eval(a_class), class_attribute_name)
                    __t_index[a_class] = __t
                    for key in __t:
                        if __t[key] in inverted_index:
                            inverted_index[__t[key]].append(key)
                        else:
                            inverted_index[__t[key]] = []
                            inverted_index[__t[key]].append(key)
                        list_of_words.append(__t[key])

                # at this point list_of_words contains all texts to be
                # translated for all classes in a module (see INDEX).
                # Let's traduce all those ones

                # uncomment for debug
                #print("data for module: {}\n".format(a_module))
                #print("__t_index: \n{}\n".format(json.dumps(__t_index, indent=4, sort_keys=True)))
                #print("inverted_index: \n{}\n".format(json.dumps(inverted_index, indent=4, sort_keys=True)))

                #translations = self.gt.translate(list_of_words, src=elem[0],dest=elem[1])

                translations = GoogleTranslator().translate(list_of_words,
                                                            src=elem[0],
                                                            dest=elem[1])
                for response in translations:
                    original_keys = inverted_index[response.origin]
                    for orignal_key in original_keys:
                        translated[orignal_key] = response.text

                #print("translated: \n{}\n".format(json.dumps(translated, indent=4, sort_keys=True)))

                for a_class in __t_index:
                    # rebuild __t dict with translated text
                    # for each class in a module
                    translated__t = {}
                    original__t = __t_index[a_class]
                    for key in original__t:
                        translated__t[key] = translated[key]

                    #print("{}.original__t: \n{}\n".format(a_class,json.dumps(original__t, indent=4, sort_keys=True)))
                    #print("{}.translated__t: \n{}\n".format(a_class, json.dumps(translated__t, indent=4, sort_keys=True)))
                    # wirte translation for a class
                    cls.write_translations(TRANSLATION_DIR,
                                           original__t,
                                           translated__t,
                                           a_class,
                                           src=elem[0],
                                           dest=elem[1])

                # to avoid ip banning from bigg
                print(
                    "Please wait, Translator is running. It'll take a while...\n"
                )
                time.sleep(delay)
Exemplo n.º 12
0
 def __init__(self, src, dst):
     self.googleTranslator = GoogleTranslator()
     self.src = src
     self.dst = dst
Exemplo n.º 13
0
 def __init__(self):
     self._translator = GoogleTranslator()
Exemplo n.º 14
0
 def __init__(self, **kwargs):
     self._translator = GoogleTranslator()