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

    with pytest.raises(exceptions.LanguageNotSupportedException):
        PonsTranslator(source="auto", target="nothing")
    l1 = PonsTranslator("en", "fr")
    l2 = PonsTranslator("english", "french")
    assert l1._source == l2._source
    assert l1._target == l2._target
示例#2
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"
示例#3
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)
示例#4
0
from deep_translator import PonsTranslator

res = PonsTranslator(source='en', target='de').translate('good',
                                                         return_all=False)

print(res)
示例#5
0
def pons():
    return PonsTranslator(source="english", target='french')
示例#6
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)