ChatterBot icon indicating copy to clipboard operation
ChatterBot copied to clipboard

Translation preprocessors

Open vkosuri opened this issue 8 years ago • 2 comments

@gunthercox how about adding this feature to Chatterbot?

More information https://cloud.google.com/translate/docs/translating-text#translate-translate-text-python

Any comments?

vkosuri avatar Jan 21 '17 17:01 vkosuri

This sounds like is might work well as a preprocessor.

gunthercox avatar Jan 21 '17 21:01 gunthercox

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

vkosuri avatar Jan 22 '17 03:01 vkosuri