def _break_and_wrap(text_to_tokenize_match): text_to_tokenize = text_to_tokenize_match.group(0) wrapped = "" for token in Languages.tokenize(Languages.chinese.value, text_to_tokenize): word_zh = WordZH.get_or_create_with_translator(word=token)[0] wrapped += '<span class="chinese-word"><span>' + word_zh.pinyin + '</span><span>' + token + '</span></span>' return wrapped
def post(self, request): form = TextsTranslationsForm(request.POST) if form.is_valid(): texts_translations_service.set_text_translations( form.cleaned_data['source_text'], Languages.from_string(form.cleaned_data['source_language']), form.translation_fields()) return redirect('translations:texts_translations')
def post(self, request): """ API for accessing text translations """ source_language = Languages.from_string(request.POST['source_language']) source_text = request.POST['source_text'] operation = request.POST['operation'] if operation == TranslationsOperation.get_matches.value: text_matches = texts_translations_service.get_text_matches(source_text, source_language) return JsonResponse({'matches': list(text_matches)}) elif operation == TranslationsOperation.get_translations.value: translations = texts_translations_service.get_text_translations(source_text, source_language) return JsonResponse({'translations': translations}) return HttpResponseBadRequest()
def auto_tokenize(self): """ Tokenize the business text into words, create their objects if necessary and link the business text to them. Only applied to Chinese words. """ word_model = to_word_model(self.language) if word_model == WordZH: tokens = Languages.tokenize(self.language, self.text) self.words_zh.clear() ordinal = 0 for token in tokens: word_object = word_model.get_or_create_with_translator(word=token)[0] BusinessTextWordZH.objects.create(text=self, word=word_object, ordinal=ordinal).save() ordinal += 1
def set_text_translations(self, source_text, source_language, translations): """ Set translations of the source text in source language to the specified translations :param source_text: text to translate :param source_language: language of text to translate :param translations: translations of the text to translate :return: nothing """ business_text_to_translate, _ = BusinessText.objects.get_or_create(text=source_text, language=source_language.value) business_text_to_translate.translations.clear() for translation in translations: translation_language = Languages.other_language(source_language) business_translation, _ = BusinessText.objects.get_or_create(text=translation, language=translation_language.value) business_text_to_translate.translations.add(business_translation) return
def add_translation(self, translation_text): translation_language = Languages.other_language(Languages(self.language)) business_translation, _ = BusinessText.objects.get_or_create(text=translation_text, language=translation_language.value) self.translations.add(business_translation)