示例#1
0
def test_inputs():
    with pytest.raises(exceptions.LanguageNotSupportedException):
        MyMemoryTranslator(source="", target="")

    with pytest.raises(exceptions.LanguageNotSupportedException):
        MyMemoryTranslator(source="auto", target="nothing")
    m1 = MyMemoryTranslator("en", "fr")
    m2 = MyMemoryTranslator("english", "french")
    assert m1._source == m2._source
    assert m1._target == m2._target
示例#2
0
def translate_this(query, src_lang=None, to_lang='en'):
    translation = ''
    if src_lang == None: 
        src_lang = single_detection(query, api_key='66d86088869a1a46156d6bf92e8ed377') 
    elif src_lang == 'en' and to_lang == 'ar':
        translation = QCRI("9f604502f8563e060872d811b53ae401").translate(source='en', target='ar', domain="news", text=text)
    else:
        try:
            a = a / 0 # Костыль чтобы выключить пока меморитраслятор, т.к. в нем меньше список поддерживаемых языков
            mymem = MyMemoryTranslator(source=src_lang, target=to_lang, de='*****@*****.**') #
            translation = mymem.translate(query)
        except:
            translation = GoogleTranslator(source=src_lang, target=to_lang).translate(text=query)
                
    return f'{translation}'
示例#3
0
def translate(src_text, dest_langcode, src_langcode):
    global translator_to_use
    src_langcode = 'en'
    dest_langcode = 'tr'
    #print('translator_to_use', translator_to_use)
    cwd = os.getcwd()
    local_dict_file = src_langcode + '_' + dest_langcode + '.json'
    dest_text = ''
    if path.exists(cwd + '/local_dictionaries/' + local_dict_file):
        with open(cwd + '/local_dictionaries/' + local_dict_file) as json_file:
            local_dict = json.load(json_file)
            if src_text in local_dict:
                dest_text = local_dict[src_text]
    if dest_text == '':
        if translator_to_use == 'google':
            translator_to_use = 'linguee'
            try:
                #print('goog')
                dest_text = GoogleTranslator(
                    source=src_langcode,
                    target=dest_langcode).translate(src_text)
            except:
                pass
    if dest_text == '':
        if translator_to_use == 'linguee':
            translator_to_use = 'myMemory'
            try:
                #print('lingue')
                dest_text = LingueeTranslator(
                    source=src_langcode,
                    target=dest_langcode).translate(src_text)
            except:
                pass
    if dest_text == '':
        if translator_to_use == 'myMemory':
            translator_to_use = 'pons'
            try:
                #print('myMemory')
                dest_text = MyMemoryTranslator(
                    source=src_langcode,
                    target=dest_langcode).translate(src_text)
            except:
                pass
    if dest_text == '':
        if translator_to_use == 'pons':
            translator_to_use = 'google'
            try:
                #print('pons')
                dest_text = PonsTranslator(
                    source=src_langcode,
                    target=dest_langcode).translate(src_text)
            except:
                pass
    return dest_text
示例#4
0
def handle_dialog(req, res):
    user_id = req['session']['user_id']

    if req['session']['new']:
        res['response']['text'] = 'Привет! Что хочешь перевести?'
        return
    else:
        phrase = req['request']['nlu']['tokens'][2:]
        translated = MyMemoryTranslator(
            source="ru", target="en").translate(text="".join(phrase))
        res['response']['text'] = translated
示例#5
0
    def translate(translator, source, target, text, *args):

        if not text:
            return 'provide a text to translate'

        try:
            if translator == 'Google Translate':
                res = GoogleTranslator(source=source,
                                       target=target).translate(text=text)
                # if target == 'arabic':
                #     res = get_display(arabic_reshaper.reshape(res))

            elif translator == 'My Memory':
                res = MyMemoryTranslator(source=source,
                                         target=target).translate(text=text)
                # if target == 'arabic':
                #     res = get_display(arabic_reshaper.reshape(res))

            elif translator == 'Pons':
                res = PonsTranslator(source=source,
                                     target=target).translate(word=text)
                # if target == 'arabic':
                #     res = get_display(arabic_reshaper.reshape(res))

            elif translator == 'Linguee':
                res = LingueeTranslator(source=source,
                                        target=target).translate(word=text)

            else:
                return "you need to choose a translator"

            return "No translation is provided" if not res else res

        except Exception as e:
            print(e.args)
            return "No translation is provided"
示例#6
0
def mymemory():
    return MyMemoryTranslator(source="en", target='fr')
示例#7
0
from deep_translator import MyMemoryTranslator

res = MyMemoryTranslator(source='ar', target='en').translate('آخُذ اَلْباص.')

print(res)
示例#8
0
def translate2(text, source, dest):
    if text is None:
        print("Invalid Text")
    elif text != "":
        translation2 = MyMemoryTranslator(source='auto', target=desti_lang).translate(text)
    return translation2
示例#9
0
    def run(self):
        while self.running:
            img = ImageGrab.grab(bbox=(self.x1, self.y1, self.x2, self.y2))
            img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)

            new_extracted_text = pytesseract.image_to_string(
                img, lang=self.image_lang_code).strip()
            new_extracted_text = " ".join(new_extracted_text.split())
            print(f"EXTRACTED TEXT: [{new_extracted_text}]")

            if len(new_extracted_text) < 1 or len(new_extracted_text) > 4999:
                continue

            if self.current_extracted_text != new_extracted_text and new_extracted_text:
                print(
                    f"Translating: [{new_extracted_text}] of len[{len(new_extracted_text)}]"
                )
                self.current_extracted_text = new_extracted_text

                translated_text = ""
                print(self.img_lang, self.trans_lang)
                if self.translator_engine == "GoogleTranslator":
                    try:
                        translated_text = GoogleTranslator(
                            source='auto',
                            target=self.trans_lang_code).translate(
                                new_extracted_text)
                        print(f"TRANSLATED TEXT: [{translated_text}]")
                    except Exception:
                        print("unsupported by GoogleTranslate")
                elif self.translator_engine == "PonsTranslator":
                    try:
                        translated_text = PonsTranslator(
                            source=self.img_lang,
                            target=self.trans_lang).translate(
                                new_extracted_text)
                        print(f"TRANSLATED TEXT: [{translated_text}]")
                    except Exception:
                        print("unsupported by PonsTranslator")
                elif self.translator_engine == "LingueeTranslator":
                    try:
                        translated_text = LingueeTranslator(
                            source=self.img_lang,
                            target=self.trans_lang).translate(
                                new_extracted_text)
                        print(f"TRANSLATED TEXT: [{translated_text}]")
                    except Exception:
                        print("unsupported by LingueeTranslator")
                else:
                    try:
                        translated_text = MyMemoryTranslator(
                            source=self.img_lang,
                            target=self.trans_lang).translate(
                                new_extracted_text)
                        print(f"TRANSLATED TEXT: [{translated_text}]")
                    except Exception:
                        print("unsupported by MyMemoryTranslator")

                self.ui.translated_text_label.setText(translated_text)
                if self.is_text2speech_enabled:
                    engine = pyttsx3.init()
                    engine.say(translated_text)
                    engine.runAndWait()

            time.sleep(1)