From b9dbadb0d0929b02f7c9da0957a6497cd784ca46 Mon Sep 17 00:00:00 2001 From: Shruti1229 Date: Tue, 18 Jul 2023 19:43:13 +0530 Subject: [PATCH] Bug fix for monolingual docx --- backend/translation/utils.py | 24 +++++++++++++++++++++++- backend/translation/views.py | 6 ++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/backend/translation/utils.py b/backend/translation/utils.py index 8a3cdcb8..dfcb364e 100644 --- a/backend/translation/utils.py +++ b/backend/translation/utils.py @@ -84,6 +84,28 @@ def convert_to_paragraph(lines): return content +def convert_to_paragraph_monolingual(payload): + lines = [] + content = "" + translated_content = "" + sentences_count = 0 + number_of_paragraphs = math.ceil(len(payload) / 5) + count_paragraphs = 0 + for index, segment in enumerate(payload): + if "text" in segment.keys(): + lines.append(segment["target_text"]) + translated_content = translated_content + " " + segment["target_text"] + sentences_count += 1 + if sentences_count % 5 == 0: + count_paragraphs += 1 + content = content + translated_content + "\n" + "\n" + translated_content = "" + + if count_paragraphs < number_of_paragraphs: + content = content + translated_content + "\n" + "\n" + return content + + def convert_to_paragraph_bilingual(payload): lines = [] transcripted_lines = [] @@ -100,7 +122,7 @@ def convert_to_paragraph_bilingual(payload): transcripted_content = ( transcripted_content + " " + segment["text"].replace("\n", " ") ) - translated_content = translated_content + segment["target_text"] + translated_content = translated_content + " " + segment["target_text"] sentences_count += 1 if sentences_count % 5 == 0: count_paragraphs += 1 diff --git a/backend/translation/views.py b/backend/translation/views.py index 110f726c..d503e3e3 100644 --- a/backend/translation/views.py +++ b/backend/translation/views.py @@ -42,6 +42,7 @@ get_batch_translations_using_indictrans_nmt_api, convert_to_docx, convert_to_paragraph, + convert_to_paragraph_monolingual, convert_to_paragraph_bilingual, generate_translation_payload, ) @@ -188,11 +189,8 @@ def export_translation(request): filename = "translation.txt" content = convert_to_paragraph(lines) elif export_type == "docx": - for index, segment in enumerate(payload): - if "text" in segment.keys(): - lines.append(segment["target_text"]) filename = "translation.docx" - content = convert_to_paragraph(lines) + content = convert_to_paragraph_monolingual(payload) return convert_to_docx(content) elif export_type == "docx-bilingual": filename = "translation.docx"