2020-12-19 23:40:37 +01:00
|
|
|
from argostranslate import translate
|
2021-03-11 10:01:12 +01:00
|
|
|
from polyglot.detect.base import Detector
|
2020-12-19 23:40:37 +01:00
|
|
|
|
2021-03-11 10:01:12 +01:00
|
|
|
|
|
|
|
languages = translate.load_installed_languages()
|
|
|
|
|
|
|
|
|
|
|
|
__lang_codes = [l.code for l in languages]
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
candidates.extend(Detector(t).languages)
|
2021-03-11 10:01:12 +01:00
|
|
|
|
2021-03-11 10:52:38 +01:00
|
|
|
# total read bytes of the provided text
|
|
|
|
read_bytes_total = sum(c.read_bytes for c in candidates)
|
|
|
|
|
|
|
|
# only use candidates that are supported by argostranslate
|
|
|
|
candidate_langs = list(filter(lambda l: l.read_bytes != 0 and l.code in __lang_codes, candidates))
|
|
|
|
|
|
|
|
# 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
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'confidence': 0.0,
|
|
|
|
'language': "en"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
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 = []
|
|
|
|
for lang_code in __lang_codes:
|
|
|
|
# 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)
|
|
|
|
lang.read_bytes = sum(l.read_bytes for l in lc)
|
|
|
|
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-03-11 10:52:38 +01:00
|
|
|
candidate_langs.sort(key=lambda l: (l.confidence * l.read_bytes) / read_bytes_total, reverse=True)
|
2021-03-11 10:01:12 +01:00
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'confidence': l.confidence,
|
|
|
|
'language': l.code
|
|
|
|
}
|
|
|
|
for l in candidate_langs
|
|
|
|
]
|