Exemple #1
0
def translate(text, user):
    """ Takes a string and tries to find the correct translation direction
    between the two languages. If both give results, both are printed.
    It uses the translation engine specified in the users preferences.
    """
    print('starting')
    # TODO: move the translate functions to the translator classes/objects
    # somehow and select it from user settings
    lan1 = user.lan1.upper()
    lan2 = user.lan2.upper()  # 'DE', 'EN' ...
    trans1 = pydeepl.translate(text, lan1, lan2)
    trans2 = pydeepl.translate(text, lan2, lan1)
    # TODO: logic depends on user.direction
    user.count += 1
    session.add(user)
    session.commit()
    if user.direction == 1:
        return [trans1]
    elif user.direction == 2:
        return [trans2]
    else:  # auto
        if trans1.lower() == text.lower() and trans2.lower() != text.lower():
            return [trans2]
        if trans1.lower() != text.lower() and trans2.lower() == text.lower():
            return [trans1]
        else:
            return [trans1, trans2]
Exemple #2
0
 def test(self):
     for e in [
             {
                 "sentence": "I like turtles.",
                 "from_language": "EN",
                 "to_language": "ES",
                 "result": "Me gustan las tortugas."
             }]:
         # Origin language provided
         translation = pydeepl.translate(e["sentence"], e["to_language"], e["from_language"])
         self.assertEqual(translation, e["result"])
         # Using auto-detection
         translation = pydeepl.translate(e["sentence"], e["to_language"])
         self.assertEqual(translation, e["result"])
Exemple #3
0
def traduction_y_to_x(text, affichage=False,
                      from_language='EN', to_language='FR'):

    translation = pydeepl.translate(text, to_language, from_lang=from_language)
    if affichage:
        print(translation)
    return translation
    async def speak(self, *text):
        text = ' '.join([str(x) for x in text])
        lang = detectlanguage.simple_detect(text).upper()

        if lang not in self.supported:
            await self.bot.say("Sorry, I do not speak {}.".format(
                self.languages[lang.lower()]))
            return
        else:
            print(f"text={text}, lang={lang}")
            try:
                # Translate English to French and everything else to English
                desired_lang = ('EN', 'FR')[lang == 'EN']
                text = pydeepl.translate(str(text), desired_lang, lang)

            except Exception as e:  # Internal error

                await self.bot.say('Error: {}'.format(e))
                raise

            else:
                pre = (
                    'Translation from DeepL',
                    'Traduction de DeepL')[lang == 'EN']  # Basic localization
                await self.bot.say('{}```\n{}\n```'.format(pre, text))
def translation_x_to_y(text, display=False,
                       from_language='FR', to_language='EN'):

    translation = pydeepl.translate(text, to_language, from_lang=from_language)
    if display:
        print(translation)
    return translation
Exemple #6
0
def change_lang(to_translate, to_language, from_language):

    try:

        return pydeepl.translate(to_translate,
                                 to_language,
                                 from_lang=from_language)

    except Exception as e:

        s = to_translate.split("\n")

        s1 = ""

        for i in s:

            s1 = s1 + pydeepl.translate(
                i, to_language, from_lang=from_language) + "\n"

        return s1
Exemple #7
0
def traduction_x_to_y(
        text,
        affichage=False,
        from_language='FR',
        to_language='EN'):  # traduit de français à l'anglais à l'aide de Deepl
    # le meilleur traducteur en ligne actuel

    translation = pydeepl.translate(text, to_language, from_lang=from_language)
    if affichage == True:
        print(translation)
    return translation
Exemple #8
0
 def test_exceptions(self):
     sentence = 'I like turtles.'
     # Language not available
     with self.assertRaises(pydeepl.TranslationError):
         pydeepl.translate(sentence, "XX")
     # Empty text
     with self.assertRaises(pydeepl.TranslationError):
         pydeepl.translate(None, "EN")
     # Text too long
     with self.assertRaises(pydeepl.TranslationError):
         pydeepl.translate("a" * 5001, "EN")
Exemple #9
0
def translate(line, to_language, from_language=None):
    prefix = ""

    if line.startswith("> ") or line.startswith("- "):
        prefix = line[:2]
        line = line[2:]

    try:
        translation = pydeepl.translate(line,
                                        to_language,
                                        from_lang=from_language)
    except (IndexError, pydeepl.TranslationError) as e:
        raise pydeepl.TranslationError(e.message)
    if not translation:
        raise pydeepl.TranslationError

    return prefix + translation
Exemple #10
0
def translate(text: str, lang_in="DE", lang_out="EN"):
    translated = []
    commands = (
        r"^@#X\d{18,19}$",  # if it starts with @#X followed by 18 to 19 digits its just a hash --> no translation needed
        r"^@#X-\d{18,19}$")
    only_hash_pattern = re.compile("|".join(commands))
    for line in text.splitlines():
        #print(line)
        if line in {'', '\n'} or only_hash_pattern.match(line):
            translated.append(line)


#        elif not line.strip():
#            translated.append('')
        else:
            translated.append(pydeepl.translate(line, lang_in, lang_out))
            time.sleep(0.6)  #problem with to many requests. not yet solved
    translated = '\n'.join(translated)
    return translated
Exemple #11
0
    def translation_executor(self, datatuple):
        sentence = datatuple[0]
        lines = datatuple[1]
        sentence = deepl.translate(sentence, self.tolang, self.fromlang).split(
            " ")  # translate the sentence

        print(self.buildstring(sentence, " ")[1:])
        totalframes = len(lines)
        framecounter = 0

        for i in range(0, len(sentence)):  # add buffer to prevent errors
            sentence.append([])

        for i in lines:  # frames
            framecounter += 1

            tallest_key = -1
            for j in lines[i]:
                if int(j) > tallest_key:
                    tallest_key = int(j)

            for j in lines[i]:  # line
                wordamount = lines[i][j]

                if framecounter == totalframes and int(
                        j
                ) == tallest_key:  # last line of the translated section
                    newline = sentence
                else:  # not the last
                    newline = sentence[:wordamount]
                    sentence = sentence[wordamount:]

                self.translated[i]['lines'][int(j)] = []
                for k in range(
                        0, len(newline)
                ):  # replace the original sentence with the translated sentence
                    if newline[k] != []:
                        self.translated[i]['lines'][int(j)].append(newline[k])
                if len(
                        self.translated[i]['lines'][int(j)]
                ) == 0:  # prevent errors, if the translated is shorter than the original sentence
                    self.translated[i]['lines'][int(j)] = " "
                newline = []
Exemple #12
0
 def translateWithErrorHandling(self, sentence):
     self.throttle()
     gracePeriod = 50
     for retry in range(3, 0, -1):
         try:
             return pydeepl.translate(sentence,
                                      self.outputLanguage,
                                      from_lang=self.inputLanguage)
         except TranslationError as error:
             print('Error trying to translate', '\"' + sentence + '\"')
             print(format(error))
             if retry and error.message.contains('unknown result'):
                 print('Sleeping for', gracePeriod,
                       'seconds before retry...')
                 time.sleep(gracePeriod)
                 gracePeriod *= 2
             else:
                 raise pydeepl.TranslationError(error)
         except IndexError as error:
             # workaround for https://github.com/EmilioK97/pydeepl/issues/2
             print('Error trying to translate', '\"' + sentence + '\"')
             print(format(error))
             raise pydeepl.TranslationError(error)
def translate_deepl(s, target='EN'):
    import pydeepl

    return '\n'.join([pydeepl.translate(ss, target)
                      for ss in s.split('.')])
Exemple #14
0
f.close()

#Splits the text into paragraphs
chunks = [chunk.strip() for chunk in chunks]
chunks = [chunk for chunk in chunks if chunk]

f = open('traduction.txt', 'w')
for chunk in chunks:

	#Are we dealing with a "real" paragraph or with a paragraph title, like "1.7.3. Extraction"?
	if chunk[0].isdigit(): 
		sentences = [chunk]
	else:
		#Joins sentences that had been split by line breaks in the PDF
		sentences = tokenizer.tokenize(chunk, realign_boundaries=True)
	
	#Translation is done sentence by sentence. If you chose to do it with whole paragraphs, in some cases only the beginning would be translated.
	for sentence in sentences:
	
		#remove this line if working with Python3
		sentence = sentence.decode('UTF-8')
		
		print(sentence+'\n\n')
		translation = pydeepl.translate(sentence, 'FR', from_lang='EN')
		print(translation+'\n---------------------------------')
		
		#Again, if working with Python3, no need to encode. f.write(translation+' ') should be fine
		f.write(translation.encode('utf-8')+' ')
	f.write('\n\n')
f.close()
Exemple #15
0
def _deepltranslate(message, language):
    return pydeepl.translate(message, language.upper(), from_lang='EN')
Exemple #16
0
def main(argv, to_language=TO_LANGUAGE, from_language=FROM_LANGUAGE):
    """ Main function. Use the arguments of the command line (sys.argv). """
    # TODO use docopt to handle the command line arguments! Cf. http://docopt.org/
    # TODO can docopt handle a cli documentation with ansicolortags tags in it? Cf. http://ansicolortags.rtfd.io/
    # Manual handing of the command line arguments
    if "-h" in argv or "--help" in argv:
        printc("""
<green>deepl.py<white> --help|-h | -f file | [--from LANG] [--to LANG] text

A simple Python script translate a text from a language to another language, using DeepL translator (https://www.deepl.com/translator).

<u>Examples:<U>
<black>$ deepl.py --help<white>
Print this help message!

<black>$ deepl.py -f test.txt<white>
Translate this text file.

<black>$ deepl.py "I like using command line to translate my text."<white>
J'aime utiliser la ligne de commande pour traduire mon texte.

<black>$ deepl.py --to ES "I like using command line to translate my text."<white>
Me gusta usar la línea de comandos para traducir mi texto.

<magenta>Copyleft 2017 Lilian Besson (License MIT)<white>
<b>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.<reset><white>
""")
        return 0

    if "--to" in argv:
        try:
            i = argv.index("--to")
            to_language = argv[i + 1]
            del argv[i], argv[i]
            # printc("<green>Using destination language {}<reset>...".format(to_language))
        except Exception as e:
            print(e)
            printc(
                "<red>Ignored exception, using default destination language {}...<reset>"
                .format(to_language))

    if "--from" in argv:
        try:
            i = argv.index("--from")
            from_language = argv[i + 1]
            del argv[i], argv[i]
            # printc("<green>Using destination language {}<reset>...".format(from_language))
        except Exception as e:
            print(e)
            printc(
                "<red>Ignored exception, using default source language {}...<reset>"
                .format(from_language))

    if "-f" in argv:
        try:
            with open(argv[argv.index("-f") + 1], 'r') as filename:
                text = "".join(filename.readlines())[:-1]
        except Exception as e:
            print(e)
            printc(
                "<red>Trying to use the rest of the arguments to send the text message...<white>"
            )
            text = "".join(argv)
    else:
        if argv:
            # Text
            if isinstance(argv, list):
                text = "".join(argv)
            elif isinstance(argv, str):
                text = argv
            else:
                printc(
                    "<Warning>argv seems to be of unknown type (not list, not str, but {}) ..."
                    .format(type(argv)))
                text = argv
            text = text.replace("\\n", "\n")
            # Durty hack to have true new lines in the message
        else:
            text = "I like using command line to translate my text."

    # print("text = '{}'".format(text))  # DEBUG
    results = []
    for t in text.splitlines():
        if t.isspace() or len(t) == 0:
            results.append(t)
        else:
            results.append(
                translate(t, to_lang=to_language, from_lang=from_language))
    result = "\n".join(results)
    print(result)
    return result
Exemple #17
0
def _deepltranslate(message, language):
    return pydeepl.translate(message, language.upper(), from_lang='EN')
Exemple #18
0
    # find last occurrence of a punctuation mark in the words
    index_last_punctuation = -1
    for i in reversed(list(range(len(words)))):
        if re.match("[.!?]", words[i]):
            index_last_punctuation = i
            break
    # if there is no punctuation in this sentence, then add the complete subtitle line to string sentence
    if (index_last_punctuation == -1):
        sentence += " ".join(words)
    else:
        sentence += (" ".join(words[0:index_last_punctuation + 1]) + " ")
        if (args["in_lang"] == None):
            from_lang = "auto"
        else:
            from_lang = args["in_lang"]
        translated_sentence = pydeepl.translate(sentence, args['translateto'], from_lang=from_lang)
        # Insert translated sentences into the subtitle file.
        list_trans_sents = translated_sentence.split()  # translated sentences in list form
        list_trans_sents.insert(int(len(list_trans_sents)/2), "\n")
        width = int(1 + len(list_trans_sents) / (index - begin_index + 1))  # number of words per line
        for i in list(range(index - begin_index + 1)):
            phrase = " ".join(list_trans_sents[i * width:width * (i + 1)])
            print(phrase)
            lines[index].text += phrase + "\n"
        # If the sentence ends with a punctuation mark, then the sentence string should be empty
        # Else the sentence string should contain the text up to the end of the string

        if index_last_punctuation + 1 == len(words):
            sentence = ""
            begin_index = index + 1
        else:
 def translate_sentence(sentence):
     translation = pydeepl.translate(sentence, "EN")
     #print("Original sentence :", sentence)
     #print("Translated Sentence :", translation)
     return translation
 def translate(self, sentence, to_language='EN', from_language='ES'):
     import pydeepl
     translation = pydeepl.translate(sentence,
                                     to_language,
                                     from_lang=from_language)
     return translation
Exemple #21
0
import pydeepl
import io
import sys

import pydeepl

from_language = sys.argv[1]
to_language = sys.argv[2]
sentence = sys.argv[3]

translation = pydeepl.translate(sentence, to_language, from_lang=from_language)
print(translation)

exit(0)
Exemple #22
0
 def deepl_translate(self):
     for row in self.feedbackcomment:
         translation = pydeepl.translate(row, 'DE', from_lang='EN')
         print(translation)