ChatterBot
ChatterBot copied to clipboard
Translation preprocessors
@gunthercox how about adding this feature to Chatterbot?
More information https://cloud.google.com/translate/docs/translating-text#translate-translate-text-python
Any comments?
This sounds like is might work well as a preprocessor.
If it is feasible to add preprocesser i will make PR, otherwise i will add this to chatterbot-tricks
def translate_text(chatbot, statement, target):
"""Translates text into the target language.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
from google.cloud import translate
translate_client = translate.Client()
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(
statement.text,
target_language=target)
# Input language: result['input']
# Translated language: result['translatedText']
# Detected source language: result['detectedSourceLanguage']
statement.text = str(result['translatedText'])
return statement