2021-03-11 12:32:26 +01:00
|
|
|
import string
|
|
|
|
|
2020-12-19 23:40:37 +01:00
|
|
|
from argostranslate import translate
|
2021-03-12 16:53:09 +01:00
|
|
|
from polyglot.detect.base import Detector, UnknownLanguage
|
2021-03-11 12:32:26 +01:00
|
|
|
from polyglot.transliteration.base import Transliterator
|
2020-12-19 23:40:37 +01:00
|
|
|
|
2022-03-04 16:24:29 +01:00
|
|
|
__languages = None
|
2021-03-11 10:01:12 +01:00
|
|
|
|
2022-03-04 09:23:11 +01:00
|
|
|
def load_languages():
|
2022-03-04 16:24:29 +01:00
|
|
|
global __languages
|
|
|
|
|
|
|
|
if __languages is None or len(__languages) == 0:
|
2022-04-30 13:15:54 +02:00
|
|
|
__languages = translate.get_installed_languages()
|
2022-03-04 16:24:29 +01:00
|
|
|
|
|
|
|
return __languages
|
2021-03-11 10:01:12 +01:00
|
|
|
|
|
|
|
def detect_languages(text):
|
2021-03-11 10:52:38 +01:00
|
|
|
# detect batch processing
|
|
|
|
if isinstance(text, list):
|
|
|
|
is_batch = True
|
|
|
|
else:
|
|
|
|
is_batch = False
|
|
|
|
text = [text]
|
2021-03-11 10:01:12 +01:00
|
|
|
|
|
|
|
# get the candidates
|
2021-03-11 10:52:38 +01:00
|
|
|
candidates = []
|
|
|
|
for t in text:
|
2021-03-12 16:53:09 +01:00
|
|
|
try:
|
2021-08-02 07:06:56 +02:00
|
|
|
d = Detector(t).languages
|
|
|
|
for i in range(len(d)):
|
|
|
|
d[i].text_length = len(t)
|
|
|
|
candidates.extend(d)
|
2021-05-18 05:51:33 +02:00
|
|
|
except UnknownLanguage:
|
2021-03-12 16:53:09 +01:00
|
|
|
pass
|
2021-03-11 10:01:12 +01:00
|
|
|
|
2021-03-11 10:52:38 +01:00
|
|
|
# total read bytes of the provided text
|
2021-08-02 07:06:56 +02:00
|
|
|
text_length_total = sum(c.text_length for c in candidates)
|
2021-03-11 10:52:38 +01:00
|
|
|
|
2022-03-04 09:23:11 +01:00
|
|
|
# Load language codes
|
|
|
|
languages = load_languages()
|
2022-03-04 16:24:29 +01:00
|
|
|
lang_codes = [l.code for l in languages]
|
2022-03-04 09:23:11 +01:00
|
|
|
|
2021-03-11 10:52:38 +01:00
|
|
|
# only use candidates that are supported by argostranslate
|
2021-05-18 05:41:02 +02:00
|
|
|
candidate_langs = list(
|
2022-03-04 16:24:29 +01:00
|
|
|
filter(lambda l: l.text_length != 0 and l.code in lang_codes, candidates)
|
2021-05-18 05:41:02 +02:00
|
|
|
)
|
2021-03-11 10:52:38 +01:00
|
|
|
|
|
|
|
# this happens if no language could be detected
|
2021-03-11 10:01:12 +01:00
|
|
|
if not candidate_langs:
|
|
|
|
# use language "en" by default but with zero confidence
|
2021-05-18 05:41:02 +02:00
|
|
|
return [{"confidence": 0.0, "language": "en"}]
|
2021-03-11 10:01:12 +01:00
|
|
|
|
2021-03-11 10:52:38 +01:00
|
|
|
# for multiple occurrences of the same language (can happen on batch detection)
|
|
|
|
# calculate the average confidence for each language
|
|
|
|
if is_batch:
|
|
|
|
temp_average_list = []
|
2022-03-04 16:24:29 +01:00
|
|
|
for lang_code in lang_codes:
|
2021-03-11 10:52:38 +01:00
|
|
|
# get all candidates for a specific language
|
|
|
|
lc = list(filter(lambda l: l.code == lang_code, candidate_langs))
|
|
|
|
if len(lc) > 1:
|
|
|
|
# if more than one is present, calculate the average confidence
|
|
|
|
lang = lc[0]
|
|
|
|
lang.confidence = sum(l.confidence for l in lc) / len(lc)
|
2021-08-02 07:06:56 +02:00
|
|
|
lang.text_length = sum(l.text_length for l in lc)
|
2021-03-11 10:52:38 +01:00
|
|
|
temp_average_list.append(lang)
|
|
|
|
elif lc:
|
|
|
|
# otherwise just add it to the temporary list
|
|
|
|
temp_average_list.append(lc[0])
|
|
|
|
|
|
|
|
if temp_average_list:
|
|
|
|
# replace the list
|
|
|
|
candidate_langs = temp_average_list
|
|
|
|
|
2021-03-11 10:01:12 +01:00
|
|
|
# sort the candidates descending based on the detected confidence
|
2021-05-18 05:41:02 +02:00
|
|
|
candidate_langs.sort(
|
2021-08-02 07:06:56 +02:00
|
|
|
key=lambda l: (l.confidence * l.text_length) / text_length_total, reverse=True
|
2021-05-18 05:41:02 +02:00
|
|
|
)
|
2021-03-11 10:01:12 +01:00
|
|
|
|
2021-05-18 05:41:02 +02:00
|
|
|
return [{"confidence": l.confidence, "language": l.code} for l in candidate_langs]
|
2021-03-11 12:32:26 +01:00
|
|
|
|
|
|
|
|
2022-09-23 13:59:13 +02:00
|
|
|
def improve_translation_formatting(source, translation, improve_punctuation=True):
|
|
|
|
source = source.strip()
|
|
|
|
|
|
|
|
if not len(source):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
if improve_punctuation:
|
|
|
|
source_last_char = source[len(source) - 1]
|
|
|
|
translation_last_char = translation[len(translation) - 1]
|
|
|
|
|
|
|
|
punctuation_chars = ['!', '?', '.', ',', ';']
|
|
|
|
if source_last_char in punctuation_chars:
|
|
|
|
if translation_last_char != source_last_char:
|
|
|
|
if translation_last_char in punctuation_chars:
|
|
|
|
translation = translation[:-1]
|
|
|
|
|
|
|
|
translation += source_last_char
|
|
|
|
elif translation_last_char in punctuation_chars:
|
|
|
|
translation = translation[:-1]
|
|
|
|
|
|
|
|
if source.islower():
|
|
|
|
return translation.lower()
|
|
|
|
|
|
|
|
if source.isupper():
|
|
|
|
return translation.upper()
|
|
|
|
|
|
|
|
if source[0].islower():
|
|
|
|
return translation[0].lower() + translation[1:]
|
|
|
|
|
|
|
|
if source[0].isupper():
|
|
|
|
return translation[0].upper() + translation[1:]
|
|
|
|
|
|
|
|
return translation
|
|
|
|
|
|
|
|
|
2021-03-11 12:32:26 +01:00
|
|
|
def __transliterate_line(transliterator, line_text):
|
|
|
|
new_text = []
|
|
|
|
|
|
|
|
# transliteration is done word by word
|
|
|
|
for orig_word in line_text.split(" "):
|
|
|
|
# remove any punctuation on the right side
|
|
|
|
r_word = orig_word.rstrip(string.punctuation)
|
|
|
|
r_diff = set(char for char in orig_word) - set(char for char in r_word)
|
|
|
|
# and on the left side
|
|
|
|
l_word = orig_word.lstrip(string.punctuation)
|
|
|
|
l_diff = set(char for char in orig_word) - set(char for char in l_word)
|
|
|
|
|
|
|
|
# the actual transliteration of the word
|
|
|
|
t_word = transliterator.transliterate(orig_word.strip(string.punctuation))
|
|
|
|
|
|
|
|
# if transliteration fails, default back to the original word
|
|
|
|
if not t_word:
|
|
|
|
t_word = orig_word
|
|
|
|
else:
|
2022-09-23 14:20:22 +02:00
|
|
|
t_word = improve_translation_formatting(orig_word.strip(string.punctuation), t_word, improve_punctuation=False)
|
|
|
|
|
2021-03-11 12:32:26 +01:00
|
|
|
# add back any stripped punctuation
|
|
|
|
if r_diff:
|
2021-05-18 05:41:02 +02:00
|
|
|
t_word = t_word + "".join(r_diff)
|
2021-03-11 12:32:26 +01:00
|
|
|
if l_diff:
|
2021-05-18 05:41:02 +02:00
|
|
|
t_word = "".join(l_diff) + t_word
|
2021-03-11 12:32:26 +01:00
|
|
|
|
|
|
|
new_text.append(t_word)
|
|
|
|
|
|
|
|
# rebuild the text
|
|
|
|
return " ".join(new_text)
|
|
|
|
|
|
|
|
|
|
|
|
def transliterate(text, target_lang="en"):
|
|
|
|
# initialize the transliterator from polyglot
|
|
|
|
transliterator = Transliterator(target_lang=target_lang)
|
|
|
|
|
|
|
|
# check for multiline string
|
|
|
|
if "\n" in text:
|
|
|
|
lines = []
|
|
|
|
# process each line separate
|
|
|
|
for line in text.split("\n"):
|
|
|
|
lines.append(__transliterate_line(transliterator, line))
|
|
|
|
|
|
|
|
# rejoin multiline string
|
|
|
|
return "\n".join(lines)
|
|
|
|
else:
|
|
|
|
return __transliterate_line(transliterator, text)
|