Skip to content

Commit

Permalink
Filter empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Jun 3, 2024
1 parent f0fb421 commit 8bbda19
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions libretranslate/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ def daily_limits():

return res

def unique_list(seq, extra):
seen = set({extra})
def filter_unique(seq, extra):
seen = set({extra, ""})
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]

Expand Down Expand Up @@ -661,7 +661,7 @@ def translate():
else:
hypotheses = translator.hypotheses(text, num_alternatives + 1)
translated_text = unescape(improve_translation_formatting(text, hypotheses[0].value))
alternatives = unique_list([unescape(improve_translation_formatting(text, hypotheses[i].value)) for i in range(1, len(hypotheses))], translated_text)
alternatives = filter_unique([unescape(improve_translation_formatting(text, hypotheses[i].value)) for i in range(1, len(hypotheses))], translated_text)

batch_results.append(translated_text)
batch_alternatives.append(alternatives)
Expand All @@ -685,7 +685,7 @@ def translate():
else:
hypotheses = translator.hypotheses(q, num_alternatives + 1)
translated_text = unescape(improve_translation_formatting(q, hypotheses[0].value))
alternatives = unique_list([unescape(improve_translation_formatting(q, hypotheses[i].value)) for i in range(1, len(hypotheses))], translated_text)
alternatives = filter_unique([unescape(improve_translation_formatting(q, hypotheses[i].value)) for i in range(1, len(hypotheses))], translated_text)

result = {"translatedText": translated_text}

Expand Down

0 comments on commit 8bbda19

Please sign in to comment.