Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def log_message(self, user_id, user_message, chat_id):
if user_message is None:
user_message = "[NO MESSAGE]"
try:
s = session()
language_code = english_message = ""
polarity = subjectivity = 0.0
try:
# translate to English & log the original language
translator = Translator()
translated = translator.translate(user_message)
language_code = translated.src
english_message = translated.text
# run basic sentiment analysis on the translated English string
analysis = TextBlob(english_message)
polarity = analysis.sentiment.polarity
subjectivity = analysis.sentiment.subjectivity
except Exception as e:
print("Error translating message: {}".format(e))
msg1 = Message(user_id=user_id, message=user_message, chat_id=chat_id,
language_code=language_code, english_message=english_message, polarity=polarity,
subjectivity=subjectivity)
s.add(msg1)
s.commit()
s.close()
except Exception as e:
def _auto_translate(row, translator=None, min_len=150, chunksize=2000, service_urls=[]):
"""Translate any text fields longer than min_len characters
into English.
Args:
row (dict): Row of data to evaluate.
Returns:
_row (dict): Modified row.
"""
if translator is None:
translator = Translator(service_urls=service_urls)
_row = deepcopy(row)
_row[TRANS_TAG] = False
_row[LANGS_TAG] = set()
for k, v in row.items():
if type(v) is not str:
continue
if len(v) <= min_len:
continue
result, langs = translate(v, translator, chunksize)
_row[LANGS_TAG] = _row[LANGS_TAG].union(langs)
if langs == {'en'}:
continue
_row[k] = result
_row[TRANS_TAG] = True
_row[LANGS_TAG] = list(_row[LANGS_TAG])
return _row
def translateText(text, toLanguage, fromLanguage="en"):
# This translates text into the language described in fromLanguage
translator = Translator()
# Initiates translator class
translation = translator.translate(text, dest=toLanguage, src=fromLanguage)
# .to_dict or .text work with this object
return translation.text
def translateText(text, toLanguage, fromLanguage="en"):
# This translates text into the language described in fromLanguage
translator = Translator()
# Initiates translator class
translation = translator.translate(text, dest=toLanguage)
# .to_dict or .text work with this object
return translation.text
def translateMe(word):
translatedWord = Translator().translate(word, dest='ar')
notification.notify(
title=translatedWord.origin,
message=translatedWord.text,
app_name='Targmly',
#ticker='',
#app_icon='avatar.ico',
timeout=10
)
def translate(self, source_locale, target_locale, strings):
translator = googletrans.Translator()
google_translations = translator.translate(
[string.render_text() for string in strings],
src=language_code(source_locale.language_code),
dest=language_code(target_locale.language_code),
)
return {
StringValue.from_plaintext(translation.origin): StringValue.from_plaintext(translation.text) for translation in google_translations
}
def translateText(text, toLanguage, fromLanguage="en"):
# This translates text into the language described in fromLanguage
translator = Translator()
# Initiates translator class
translation = translator.translate(text, dest=toLanguage)
# .to_dict or .text work with this object
return translation.text
def translate(request, translation_request_id):
translation_request = get_object_or_404(
TranslationRequest, id=translation_request_id
)
message_extractor = MessageExtractor(translation_request.source_locale)
for page in translation_request.pages.filter(is_completed=False):
instance = page.source_revision.as_page_object()
message_extractor.extract_messages(instance)
translator = googletrans.Translator()
google_translations = translator.translate(
list(message_extractor.messages.keys()),
src=language_code(translation_request.source_locale.language_code),
dest=language_code(translation_request.target_locale.language_code),
)
translations = {
translation.origin: translation.text for translation in google_translations
}
publish = request.POST.get("publish", "") == "on"
try:
with transaction.atomic():
message_ingestor = MessageIngestor(
translation_request.source_locale,