What I did to solve that is creating a language slot. When the user says he wants to change the language, the bot will give him 4 buttons to choose from, one for each language.
Then every response is a custom action, that checks the value of the language slot and returns the text accordingly.
Those are the functions I wrote for language detection:
lang_list = ['English', 'French', 'Arabic', 'Armenian'] # Same as slot values, language is a categorical slot
def get_lang(tracker):
lang = tracker.slots['language'].title()
return lang
def get_lang_index(tracker):
return lang_list.index(get_lang(tracker))
# dispatcher.utter_message(text = ...)
def get_text_from_lang(tracker, utter_list = []):
lang_index = get_lang_index(tracker)
if not utter_list: # No text was given for any language
utter_list.append('[NO TEXT DEFINED]')
if lang_index >= len(utter_list): # No text was given for current language
lang_index = 0
return utter_list[lang_index]
# dispatcher.utter_message(template = ...)
def get_template_from_lang(tracker, template):
return template + '_' + get_lang(tracker)
# dispatcher.utter_message(buttons = ...)
def get_buttons_from_lang(tracker, titles = [], payloads = []):
lang_index = get_lang_index(tracker)
buttons = []
if lang_index >= len(payloads): # No text was given for current language
lang_index = 0
for i in range(min(len(titles[lang_index]), len(payloads))): # Build each button
buttons.append({'title': titles[lang_index][i], 'payload': payloads[i]})
return buttons
Here are examples uses:
When the user wants to change the language, the bot will speak according to get_text_from_lang
and give 4 buttons. When using this function, the responses are hardcoded in the code.
text = get_text_from_lang(
tracker,
['Choose a language:',
'Choisissez une langue:',
':اختر لغة',
'Ընտրեք լեզու ՝'])
buttons = [
{'title': 'English', 'payload': '/set_language{"language": "English"}'},
{'title': 'Français', 'payload': '/set_language{"language": "French"}'},
{'title': 'عربي', 'payload': '/set_language{"language": "Arabic"}'},
{'title': 'հայերեն', 'payload': '/set_language{"language": "Armenian"}'}
]
dispatcher.utter_message(text = text, buttons = buttons)
When the bot should ask about a service type, it will choose a template/response defined in the domain using get_template_from_lang
and will choose which titles to display according to the language using get_buttons_from_lang
.
When using get_template_from_lang
, the domain should define get_template_from_lang_English
, get_template_from_lang_French
, etc., each containing multiple responses, and one will be taken at random like any normal utterance.
When using get_buttons_from_lang
, you provide a list of lists of titles in each language, followed by a list of payloads (each language has the same payload).
template = get_template_from_lang(tracker, 'utter_ask_service_type')
buttons = get_buttons_from_lang(
tracker,
[['Wireless', 'Internet', 'DSL Internet', 'CableVision TV'],
['Sans Fil', 'Internet', 'Internet DSL', 'CableVision TV'],
['لاسلكي','إنترنت','DSL إنترنت','تلفزيون الكابل'],
['Անլար', 'Ինտերնետ', 'DSL ինտերնետ', 'CableVision TV']],
[
'/inform_service_type{"service_type": "wireless"}',
'/inform_service_type{"service_type": "internet"}',
'/inform_service_type{"service_type": "dsl"}',
'/inform_service_type{"service_type": "cablevision"}'
])
dispatcher.utter_message(template = template, buttons = buttons)