Esempio n. 1
0
def test_inputs():
    with pytest.raises(exceptions.LanguageNotSupportedException):
        LingueeTranslator(source="", target="")

    with pytest.raises(exceptions.LanguageNotSupportedException):
        LingueeTranslator(source="auto", target="nothing")

    l1 = LingueeTranslator("en", "fr")
    l2 = LingueeTranslator("english", "french")
    assert l1._source == l2._source
    assert l1._target == l2._target
Esempio n. 2
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
Esempio n. 3
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"
Esempio n. 4
0
from deep_translator import GoogleTranslator, PonsTranslator, LingueeTranslator

# examples using google translate

english_text = 'happy coding'
chinese_text = '這很好'

result_german = GoogleTranslator(source='english',
                                 target='german').translate(text=english_text)
result_french = GoogleTranslator(source='auto',
                                 target='french').translate(text=chinese_text)

print(
    f"original english text: {english_text} | translated text: {result_german}"
)  # result: fröhliche Codierung
print(
    f"original chinese text: {chinese_text} | translated text: {result_french}"
)  # result: C' est bon

# examples using linguee:
text = 'cute'
translated = LingueeTranslator(source='english',
                               target='german').translate(word=text)
print("Using Linguee ==> the translated text: ", translated)

# examples using pons:
text = 'good'
translated = PonsTranslator(source='english',
                            target='arabic').translate(word=text)
print("using Pons ==> the translated text: ", translated)
Esempio n. 5
0
def linguee():
    return LingueeTranslator(source="english", target='french')
Esempio n. 6
0
def test_one_character_words():
    assert LingueeTranslator(source='es', target='en').translate('y') == 'and'
Esempio n. 7
0
def translate3(text, source, dest):
    if text is None:
        print("Invalid Text")
    elif text != "":
        translation3 = LingueeTranslator(source='auto', target=desti_lang).translate(text)
    return translation3
Esempio n. 8
0
from deep_translator import LingueeTranslator

res = LingueeTranslator(source='de', target='en').translate('laufen',
                                                            return_all=False)

print(res)
Esempio n. 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)