-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslations.py
35 lines (30 loc) · 1.14 KB
/
translations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Main translator file: Functions to translate and detect runes."""
from constants import logger
from tables import english_table, futhark_table
def translate(query, is_futhark=False):
"""Translate a string from Futhark to and from English."""
log_message = 'Translating to {}...'
if is_futhark is True:
log_message = log_message.format('English')
translation = query.translate(english_table)
else:
log_message = log_message.format('Futhark')
query = query.lower()
translation = query.translate(futhark_table)
logger.info(log_message)
return translation
def detect_translate(query):
"""Detect the language and translate accordingly."""
log_message = 'Detected {} text.'
average_codepoint = sum(
ord(char) for char in query
) / len(query)
if average_codepoint < 1000:
log_message = log_message.format('English')
logger.info(log_message)
translation = translate(query)
else:
log_message = log_message.format('Futhark')
logger.info(log_message)
translation = translate(query, is_futhark=True)
return translation