Skip to content

Commit

Permalink
Merge pull request #387 from AI4Bharat/translation_bugs
Browse files Browse the repository at this point in the history
Bug fix for monolingual docx
  • Loading branch information
Shruti1229 authored Jul 18, 2023
2 parents 1129bf5 + b9dbadb commit 29480d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
24 changes: 23 additions & 1 deletion backend/translation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions backend/translation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 29480d3

Please sign in to comment.