def translate_de(self): str = '' for row in self.feedbackcomment: blob = TextBlobDE(row) if blob.detect_language() == "en": blob_en = blob else: blob_en = blob.translate(to="en") str = str + blob_en.string return str
def word_translate(inputtext, language): Aufgabe = { "Kopfzeile": "Name: Klasse: Datum: \n ", "Titel": "", "1. Aufgabe": "Übersetze!\n", "Hinweise": "Hier ist die Wortliste: \n", "Rätselwörter": "Hier ein paar Rätselwörter aus dem Text: \n", } doc = docx.Document() # initializing python-docx save_path = docxprint.docx_print(Doc=doc, save='word-translate') docxprint.docx_print(printText=Aufgabe["Kopfzeile"], Bold=True, Doc=doc) docxprint.docx_print(printText=Aufgabe["1. Aufgabe"], Bold=True, Doc=doc) nlp = languageload.language_load(language) docnlp = nlp(inputtext) #load to spacy #prepare to control, only unsorted and unfiltered inputtext_prepared = [] for token in docnlp: if str(token).isalpha() == True: inputtext_prepared.append(str(token)) inputtext_prepared = " \n ".join(inputtext_prepared) print(inputtext_prepared) blob = TextBlobDE(inputtext_prepared) translation = blob.translate( from_lang='en', to="de") # bg - bulgarisch, de - deutsch, en - englisch print(translation) wordlist_translated = translation.split("\n") inputtext_prepared = inputtext_prepared.split("\n") print(len(inputtext_prepared), len(wordlist_translated)) print(inputtext_prepared) print(wordlist_translated) result = [] for i in range(len(wordlist_translated)): result.append(inputtext_prepared[i].lower() + "\t-" + wordlist_translated[i]) result = list(set(result)) result.sort() docxprint.docx_print(printText=inputtext, Doc=doc) docxprint.docx_print(printText=Aufgabe["Hinweise"], Bold=True, Doc=doc) for translation in result: print(translation) docxprint.docx_print(printText=translation, Doc=doc) doc.save(save_path)