oracle intelligent bots techexchange€¦ · oracle intelligent bots - techexchange 6 enabling...

21
Oracle Intelligent Bots – TechExchange 1 Oracle Intelligent Bots TechExchange Article. Building Single Base-Language Bots Frank Nimphius, Marcelo Jabali - April 2018 Contributors: Grant Ronald, Dan Nguyen, Martin Cookson, Tamer Qumhieh, Linus Hakansson Chatbot support for multiple languages is a worldwide requirement. Almost every country has the need for supporting foreign languages, be it to support immigrants, refugees, tourists, or even employees crossing borders on a daily basis for their jobs. According to the Linguistic Society of America1, as of 2009, 6,909 distinct languages were classified, a number that since then has been grown. Although no bot needs to support all languages, you can tell that for developers building multi-language bots, understanding natural language in multiple languages is a challenge, especially if the developer does not speak all of the languages he or she needs to implement support for. This article explores Oracle's approach to multi language support in chatbots. It explains the tooling and practices for you to use and follow to build bots that understand and "speak" foreign languages. 1 https://www.linguisticsociety.org/content/how-many-languages-are-there-world

Upload: others

Post on 25-May-2020

22 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots–TechExchange

1

OracleIntelligentBots

TechExchange

Article.

Building Single Base-Language Bots Frank Nimphius, Marcelo Jabali - April 2018 Contributors: Grant Ronald, Dan Nguyen, Martin Cookson, Tamer Qumhieh, Linus Hakansson

Chatbot support for multiple languages is a worldwide requirement. Almost every country has the need for supporting foreign languages, be it to support immigrants, refugees, tourists, or even employees crossing borders on a daily basis for their jobs.

According to the Linguistic Society of America1, as of 2009, 6,909 distinct languages were classified, a number that since then has been grown. Although no bot needs to support all languages, you can tell that for developers building multi-language bots, understanding natural language in multiple languages is a challenge, especially if the developer does not speak all of the languages he or she needs to implement support for.

This article explores Oracle's approach to multi language support in chatbots. It explains the tooling and practices for you to use and follow to build bots that understand and "speak" foreign languages.

1 https://www.linguisticsociety.org/content/how-many-languages-are-there-world

Page 2: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 2

BUILDING MULTI LANGUAGE BOTS IN ENGLISH .............................................................................. 3 HOW GOOD ARE SINGLE BASE-LANGUAGE BOTS? ..................................................................................... 3

ENABLING TRANSLATION SERVICES FOR A BOT ............................................................................ 4 REGISTER ONE OR MORE TRANSLATION SERVICES ..................................................................................... 4 REFERENCE A TRANSLATION SERVICE IN THE BOT SETTINGS ...................................................................... 5

ENABLING TRANSLATIONS FOR A BOT ............................................................................................. 6 ENABLING AUTO-TRANSLATION .................................................................................................................. 6 SETTING THE UI COMPONENT “TRANSLATE” PROPERTY ............................................................................. 6

BOT LANGUAGE COMPONENTS .......................................................................................................... 7 SYSTEM.DETECTLANGUAGE ....................................................................................................................... 7 SYSTEM.TRANSLATEINPUT ......................................................................................................................... 8 DIRECT INPUT TRANSLATION ....................................................................................................................... 8 STRING TRANSLATION FROM A SOURCE VARIABLE ....................................................................................... 8 SYSTEM.TRANSLATEOUTPUT ..................................................................................................................... 9

RESOURCE BUNDLES ........................................................................................................................... 9 CREATING RESOURCE BUNDLES .............................................................................................................. 10 USING RESOURCE BUNDLES IN A CONVERSATION ..................................................................................... 12 ABOUT AUTO-TRANSLATION OF RESOURCE BUNDLES ............................................................................... 13

WHAT YOU SHOULD KNOW ABOUT PROFILE.LOCALE AND PROFILE.LANGUAGETAG ........... 13

TRANSLATION OVERVIEW AND COMPARISON ............................................................................... 15

TRANSLATING CUSTOM COMPONENT RESPONSES ...................................................................... 15 STORING DATA IN A CONTEXT VARIABLE .................................................................................................. 16 SAVING MESSAGES IN A CONTEXT VARIABLE ............................................................................................ 17 SENDING RESPONSES DIRECTLY TO THE USER ......................................................................................... 18

TRANSLATION STRATEGIES AND PRACTICES ............................................................................... 19 OPT-IN VS. OPT-OUT ................................................................................................................................. 19 USE RESOURCE BUNDLES FOR PROMPTS AND RESPONSES ...................................................................... 19 USE RESOURCE BUNDLES FOR CONTENT THAT SHOULD NOT BE TRANSLATED .......................................... 19 TEST YOUR TRANSLATION SERVICE USING BACKWARD TRANSLATIONS .................................................... 20 BE AWARE OF THE LIMITATIONS OF LANGUAGE DETECTION ...................................................................... 20 LIMIT THE SET OF LANGUAGES SUPPORTED BY A BOT .............................................................................. 21 SAVE AND REMEMBER .............................................................................................................................. 21

SUMMARY ............................................................................................................................................. 21

Page 3: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 3

Building Multi Language Bots in English There exist two broad approaches for developers to build multi-language bots.

Native language bots use a specific language for its utterances, entities, prompts, titles, descriptions and labels. To support multiple languages you create separate bots, each using a language specific natural language processing engine for the intent resolution and entity extraction that you train and test with language specific utterances.

Single Base-Language are bots that serve multiple languages from a single base language for natural language understanding, entity extraction as well as for designing prompts, titles, descriptions and labels during development. A translation service is used at runtime to detect and translate foreign language user input into the base language for intent resolution and entity extraction. Response messages as well as label and prompts are either auto-translated to the detected user language or reference language specific strings in resource bundles. From a bot user perspective the bot appears as a native language bot.

As of today, Oracle Intelligent Bots have superior support for the English language. So whatever the language-processing engine receives in English it knows to handle the best. From a development perspective, creating bots in English is more likely to give better results. As such, Oracle Intelligent Bots’ approach to multi-language chatbot applications is to build single base-language in English and use translation services and resource bundles to support foreign languages.

Note: It is possible to also build native language bots with Oracle Intelligent Bots today, which may require you to provide more training data than for English. Oracle continues to work on native language support for intent resolution and entity extraction as well as translation service support.

How good are Single Base-Language Bots? “If no mistake have you made, yet losing you are … a different game you should play" - Yoda

The above quote from Star Wars' Yoda does not use correct grammar yet you still understand it. And even if Yoda had left more words out, the sentence still could be understood.

“If no mistake you made, yet losing … a different game play" – Yoda (revised)

While grammar is important for speaking, vocabulary is key when it comes to understanding, as the human brain is able to compensate for missing words and grammar mistakes.

Natural language processing behaves in a similar way to the human brain in that it can rely less on grammar in its understanding. So a sentence doesn't need to be perfect to be understood by the intent resolution and entity extraction.

For building single base-language this means that the intent engine understands foreign language input if the translation service does a good job in translating the words. Both, Google and Microsoft translation services provide outstanding translation support.

Note: We'll get back to this in the "Translation Strategies and Practices" section later in this paper. While the order of words matters less for the intent resolution it does for entity extraction.

Page 4: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 4

Enabling Translation Services for a Bot Oracle Intelligent Bots uses configurable translation services to translate user messages into English, the bot base language, and bot response to the preferred user language. The preferred user language can be detected from user input or read from the user profile settings. It is also possible to have a user select a preferred language from a list in case language support should be limited to a specific set of languages.

To enable the translation feature for a bot you need to

• Register one or more translation services with the Oracle Intelligent Bots cloud instance

• Select a translation service in the bot settings

Register One or More Translation Services At current, Oracle Intelligent Bots supports two translation services, Google and Microsoft for which you need to bring your own license.

Note: You can reduce the cost of translation by using resource bundles and by saving the user detected language in a user scope variable.

The Base URLs for these services are

• Google - https://translation.googleapis.com/language/translate/v2

• Microsoft - https://api.microsofttranslator.com/V2/Http.svc

To register a translation service in Oracle Intelligent Bots, you select the menu icon on the upper right corner of the dashboard and choose the "Translation Services" menu item (Figure 1).

Figure 1: Registration Menu for Translation Services

You create new translation service registrations by pressing the "+ Service" button shown in Figure 2 and providing the translation service base URL and authorization key.

Page 5: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 5

Figure 2: Translation Service Configuration Dialog

Reference a Translation Service in the Bot Settings To enable translation for a bot you need to enable one of the registered translation services from the bot's "Settings" panel.

For this, select the "Settings" icon in the left hand menu (highlighted in blue in Figure 3) and select the "General" tab. In here, you now select a value for the "Translation Service" field.

Figure 3: Enabling Translation Service for a Bot

Page 6: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 6

Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens by itself. To leverage a configured translation services in your bot conversations you either need to enable auto-translation or set the translate property on a UI system component to true.

Enabling Auto-Translation When enabled for a bot, auto-translation impacts all conversations defined in BotML for a bot. With auto-translation enabled, Oracle Intelligent Bots translates all component label, title, description, prompt and text strings.

The auto-translate feature is disabled by default. To enable auto-translation, you define a context variable with the name autoTranslate of type boolean and set its value to true. The example below shows how to define the autoTranslate context variable and how to use the System.SetVariable to set the variable value to true.

variables: autoTranslate: "boolean" … states: … enableAutotranslation: component: "System.SetVariable" properties: variable: "autoTranslate" value: true transitions: {}

You can enable or disable auto-translation at any time in a conversation. Note however that a change in the auto-translation setting only impacts conversations that happen after the change. It’s not that a change in the translation setting will change previous user-bot and bot-user conversations that may still show in the messenger window.

Setting the UI Component “translate” Property Every user interface and input component has a translate property that can be used to override the global auto-translation setting for a component. If, for example, auto-translation is enabled for a bot, then setting the "translate" property to false on a component will exclude its prompt, title, description, label and text strings from translation.

Conversely, if the auto-translate feature is not enabled for a bot but a component's "translate" property is set to true, then the component prompt, title, description, label and text string is translated into the detected user language using the configured translation service. Input components translate user input into English.

If the "autoTranslate" variable is set to false, which also is the default, then no auto-translation occurs on the component unless the "translate" property explicitly is set to true.

If you set "autoTranslate" to true then the translate property implicitly is set to true as well, which means that all component label, title, description, prompt and text strings will be translated.

Table 1 below shows the different combinations of the "autoTranslate" context variable and the component "translate" property settings along with their impact to string translation.

Page 7: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 7

autoTranslate variable setting Component "translate" property

setting Component prompt, label, text, title, description, input translated?

true not set yes

true true yes

true false no

false not set no

false false no

false true yes

Table 1: Combination of "autoTranslate" context variable setting and "translate" component property setting and their impact to translation

Bot Language Components Oracle Intelligent Bots provides specific language components that you can use to detect a bot user's preferred language and to translate user input and output messages.

System.DetectLanguage The System.DetectLanguage component uses the configured translation service to detect the user language based on a provided input string. As a result, the "profile.languageTag" variable is set to the locale string of the detected user language.

Note: The "profile.languageTag" takes precedence over the "profile.locale" variable, which holds the language setting detected in the messenger used by a user.

The System.DetectLanguage component detects the user language either from direct user input, as shown in the code sample below, or from content saved in a source variable.

… detectLanguageFromDirectInput: component: "System.DetectLanguage"

To detect language from content saved in a context variable, you use the component source property, as shown in the code below.

context: variables: sourceString: “string” … states: … detectLanguageFromSourceVariable: component: "System.DetectLanguage" properties: source: "sourceString"

Page 8: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 8

System.TranslateInput The System.TranslateInput component leverages the configured translation service to translate the user input of any language to English. For this, there is no need to set or detect the user language prior to using the component. The translation service is able to tell the user language by the string provided for translation. The result always is returned in English. To use the component in BotML, you have two options:

1. Direct input translation

2. String translation from a source variable

Direct Input Translation The System.TranslateInput component is an input component comparable to the System.Text component. The difference between System.Text and System.TranslateInput is that for System.TranslateInput the user input that is saved in a referenced context variable is always English for while the System.Text component holds the user value in the language it is provided.

context: variables: translatedString: “string” sourceString: “string” … states: … translateInput: component: "System.TranslateInput" properties: variable: "translatedString"

The BotML sample code above waits for a user to provide text input on a messenger console to then translate the input and save it into the transatedString variable. If, for example the user types the German string “Hallo, ich bin ein Mensch”, then the content in the translatedString variable is “Hello, I am a human”.

String Translation from a Source Variable Consider the BotML definition below in which two variables, sourceString and translatedString, are defined as context variables. The sourceString variable holds the user input, for example added through a System.Text component. At the end of the System.TranslateInput component processing, the translatedString variable holds the English text.

context: variables: translatedString: “string” sourceString: “string” … states: … translateInputString: component: “System.TranslateInput”

Page 9: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 9

properties: source: “sourceString” variable: “translatedString” …

Note that System.TranslateInput does not translate data objects or data arrays that were saved in a context variable through a custom component. For example, assume the following object to be assigned to a variable: {“product”: “scissors”, “color”: “silver”}. Passing the variable to the System.TranslateInput component does not translate the "scissors" and "silver" string values. Instead the content gets translated when a variable object property is referenced from a component that uses auto-translation.

System.TranslateOutput The System.TranslateOutput component translates any string to the bot-user's language, which may be the language detected by the System.DetectLanguage component or a language setting in the user profile.locale variable.

context: variables: translatedString: “string” sourceString: “string” … states: … translateOutputString: component: “System.TranslateOutput” properties: source: “sourceString” variable: “translatedString”

Resource Bundles There are two possibly problems with auto translation: The first is that the translation my not be perfect. And second, sometimes a word or sentence could be translated differently based on context.

In German, to give an example, the "Sie" and "Du" makes a difference when speaking to someone. "Sie" is more formal than "Du" and is used e.g. in customer conversations, when talking to strangers, when talking to people deserving respect, or when talking to people who are not a friend or family. In English there is only "you" to address a person. Chances therefore are very likely that messages use "Du" where it should be "Sie" when translated by Google or Microsoft APIs. Similar nuances in the translation apply for other languages as well.

Instead of auto-translating bot responses, you can use resource bundles in Oracle Intelligent Bots. Resource bundles are language templates that you provide at design time for the languages supported by a bot. Optionally, at runtime, you can add data content to resource bundle strings.

The benefit of using resource bundles is that you, as the bot designer, control the translated strings. Using a translation service to translate user messages, chances are that the translated string does not match your preferred tone or business wording.

Page 10: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 10

Another benefit of resource bundles is that they don't require a translation service, which means that they are free to use at runtime.

Like in Java, a resource bundle holds key-string pairs for the default language and any other language supported in a bot. Resource bundle strings can be parameterized allowing bot designers to insert values referenced from context variables at runtime.

Note: Resource bundles in Oracle Intelligent Bots are defined on the individual bot level. At current there is no option available to share resource bundles across bots.

Creating Resource Bundles To get started with resource bundles, select the Resource Bundle menu icon of your bot. Figure 4 shows the Recourse Bundle menu icon highlighted in "blue". Click the green "+ Bundle" button to start creating resource bundles.

Figure 4: Getting started with resource bundles

You first create key-value pairs for the default language, which we recommend to be English to go along with the default language used by the auto-translation feature.

Page 11: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 11

Figure 5: Defining a first language key and string value

Once the first resource is created you can use the green "+ Key" button to create more. Figure 6 shows a resource message containing parameters for bot designers to pass values in at runtime.

Note: Given the state names in a conversation are unique, it helps if you use key names that align to the state names. In the examples shown in Figure 5 and 4 the key names are the same as the state names, except that the key names always start with an uppercase letter.

Figure 6: Creating a resource bundle with parameters

Page 12: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 12

To create a translation, select a message bundle entry and click the "+ Language" button. In the opened dialog, define or select a language code and provide the translated text. Figure 7 shows a translation to German for the "AskBalanceAccountPrompt" key.

Figure 7: Creating a translation

Using Resource Bundles in a Conversation With the resource bundles in place, you can now enable the use of resource bundles for a conversation and then reference resource keys from UI components.

To enable resource bundles for a conversation you need to define a context variable of type "resourcebundle".

variables: … rb: "resourcebundle"

Note: The name of the resource bundle variable is up to you. In this paper we use "rb".

Next you then reference resource bundle keys from components in the dialog flow. For example, a message string that does not contain parameters can be referenced as shown below

Page 13: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 13

askBalancesAccountType: component: "System.List" properties: options: "${accountType.type.enumValues}" prompt: "${rb('AskBalancesAccountType')}" variable: "accountType" transitions: {}

Note: An alternative option to reference the message bundle key is "${rb.AskBalancesAccountType}. This however only works for resource bundle strings that don't use parameters

For resource bundle strings that contain parameters you use the ${rb('key_name','${p0.value}', '$(p1.value)', …)} synatx where "p0" and "p1" need to be substituted with the context variable names that hold the values for the configured parameters. If you want to add two variable references, like when welcoming a user, you can do this as follows: ${rb("WelcomeKey, '${profile.firstName} ${profile.lastName}') .

Note: Though referencing context variables in resource bundle parameters is a more common use case, it is also possible to directly type text that should be added to the resource string.

The sample below references the "DisputeResponse" message bundle key and passes two parameters values. The parameter values are read from the "disputeID" and "reason" attributes of a data object saved in the "dispute" context variable.

disputeResponse: component: "System.Output" properties: text: "${rb('DisputeResponse', '${dispute.value.disputeID}', '${dispute.value.reason}')}" transitions: return: "instantAppOutput"

About auto-translation of Resource Bundles Resource bundles are applied based on the profile.locale or profile.languageTag setting. Content referenced at runtime through parameters is displayed as is. However, if a resource bundle is referenced from an output component that has its translate property set to "true", then the whole resource bundle text will be translated to the user language. So resource bundles can be used in cases where you need strong control over the response message but also want to translate content that id dynamically added, for example information queried from a custom component,

What You Should Know About profile.locale and profile.languageTag Component message and label translations, as well as the use of resource bundles, depend on knowing what the user language is. The user language is saved in two profile variables, profile.locale and profile.languageTag. The profile.languageTag variable is set by the System.DetectLanguage component, whereas the messenger client that is used for the bot conversation sets the profile.locale variable.

Page 14: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 14

What you should know is that you can set the values of the profile variables using the System.SetVariable component. The examples below show the code required to set the value of each of the variables and how to even set the value of one for the other.

Example: 1

setLocaleToGerman: component: "System.SetVariable" properties: variable: "profile.locale" value: "de"

Example: 2

setLanguageTagToGerman: component: "System.SetVariable" properties: variable: "profile.languageTag" value: "de"

Example: 3

setLanguageTagToVariableValue: component: "System.SetVariable" properties: variable: "profile.languageTag" value: "${language_preference_var.value}"

Example: 4

setLocaleToLanguageTag: component: "System.SetVariable" properties: variable: "profile.locale" value: "${profile.languageTag}"

Example: 5

setLanguageTagToLocale: component: "System.SetVariable" properties: variable: "profile.languageTag" value: "${profile.locale}"

With this knowledge and the samples it is easy to see how you can implement support for only a choice of languages. You would detect the user language from profile.locale, profile.translateTag or a custom user profile database table to then compare it with a list languages supported by your bot. If the user preferred language does

Page 15: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 15

not match you can show a list of supported languages for the user to choose from. Once the user selected a language, you set the profile.languageTag to the user selected language.

Translation Overview and Comparison Now that you know about all the tools you can use in Oracle Intelligent Bots to build multi language and multi language bots, let’s show them in a single view for simple comparison.

Component / Feature Description Reads language settings from

Requires translation service

System.DetectLanguage Detects the user language from user input and sets the language code to the bot internal profile.languageTag variable.

NA Yes

System.TranslateInput Translates all user input to English. You use this component to build multi language bot that use English for the intent resolution.

NA Yes

System.TranslateOutput Translates strings to the user preferred language

profile.languageTag Yes

Resource Bundle Substitutes resource bundle references in component properties with pre-defined translations strings.

Falls back to default language is translation for a language doesn't exist

profile.languageTag profile.locale (second)

No

UI component "translate" property

Overrides global translation settings. If set to true, leads to labels, titles, descriptions, text and prompts to be translated to user language.

Note: Does not translate user input strings

profile.languageTag

profile.locale (second)

Yes

Table 2: Translation features overview and the feature's dependency to a translation service

Translating Custom Component Responses As a custom component developer you have 3 options available to return information to a user:

Page 16: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 16

• You can store data objects in a context variable, which then is referenced by an output component or system component label or prompt.

• You can save string messages in a context variable for display by system components like System.Output, System.List, System.CommonResponse or System.TranslateOutput.

• You can send responses directly to the user, not requiring any help from system components.

Storing Data in a Context Variable Custom components that query backend services usually return data in a simple format like text or number, or in a complex format like object or array of objects. If the data queried from a remote backend service requires translation then one option is to save the data in a context variable defined in the bot dialog flow and use the translate property on an output component, or the System.TranslateOutput component, to translate it to the user preferred language. To write data to a context variable you use the following code line within a custom component.

conversation.variable('data', dataObject);

conversation.transition();

done();

For this code to work, you need to define a context variable with the name "data" of type "string" in the variables section of the dialog flow.

data: "string"

Hint: Avoid making assumptions about the existing of context variables when developing custom components. We recommend to pass the name of the context variable the component should write the data object to as an input parameter to the custom component.

Oracle Intelligent Bots system components don't translate data objects as a whole. To translate data objects stored in a context variable, you reference the context variable including the name of the data object attribute to display and translate.

For example, let’s assume the data object passed from a custom component to the "data" context variable is

{product: "an apple", type: "fruit", origin: "Spain" }

You then reference the data object as follows in a System.Output component for translation

printProduct: component: "System.Output"

Properties: text: "The product in your cart is a ${data.value.type}. It is ${data.value.product} from ${data.value.origin" translate: true

If translation service is enabled for the bot then this message gets translated to the user language, including translations of the content of the data object passed from the custom component.

Page 17: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 17

Saving Messages in a Context Variable The BotML sample below references a custom component in its "initializeReceipe" state and passes the name of the variable to hold the component response and the purchaseId as input parameters.

Note: It is good programming practice to pass the name of the context variable to hold the component response as an input parameter to the custom component. This ensures that custom components are not dependent on a specific BotML implementation code and remain reusable.

To display messages in the detected user language you either use the System.TranslateOutput component or set the translate property on an output component like System.Output or System.CommonResponse, as shown in the "printReceipe" state below

initializeReceipe: component: "sample.receipe.dataresponse" properties: dataVariable: "receipe" purchaseid: "${purchaseId.value}

printReceipe: component: "System.Output" properties: text:"${receipe.value}" translate: true … The custom component code would look as shown below

... invoke: (conversation, done) => { var responseVariable = conversation.properties().dataVariable; var purchaseId = conversation.properties().purchaseid; … var message = queryRemoteServiceForReceipe(purchaseId); … conversation.variable(responseVariable, message); conversation.transition(); done(); }

If a custom component returns a message in a non-English language that should be processed by the natural language processing engine (NLP) engine, then you use the System.TranslateInput component to translate the message into English before calling System.Intent.

translateMessage: component: " System.TranslateInput " properties: source: "variable_populated_by_custom_component" variable: "translationStringVar"

getIntent: component: "System.Intent"

Page 18: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 18

properties: variable: "iResult" sourceVariable: "translationStringVar" …

Sending Responses Directly to the User

Without going into a longer discussion of when custom components should write message responses directly to a

message channel and when they should save messages or data in a context variable, we simply accept the fact

that there are use cases for custom components to be self-contained.

Custom components use the conversation.reply(…) function in the bot custom component SDK to return responses directly to the user. Responses that are directly sent to a user cannot be translated by the translation service feature of Oracle Intelligent Bots and therefore need to be translated by the component before sending them.

To translate bot responses sent directly from a custom component, you have two options

• Call a translation service from the custom component implementation code before sending a message.

This option is suitable if the messages are not composed in the custom component but queried from a

remote backend. Here auto-translation will help making the strings available in the bot user preferred

language.

• Use message bundles to provide translated responses for the languages you want the bot to support.

Message bundles can use placeholders in the translated strings for the custom component to insert data

value at runtime. Message bundles in custom components are a viable option if the custom component

queries data from a remote service to integrate in a bot response.

To know about the user preferred language in the custom component you can either pass the locale as an input parameter to the component or access the profile.locale and profile.languageTag variables from the component as shown below.

//detect user locale. If not set, define a default var locale = conversation.variable('profile.locale')? conversation.variable('profile.locale') : 'en';

//when profile languageTag is set, use it. If not, use profile.locale var languageTag = conversation.variable('profile.langageTag')? conversation.variable('profile.langageTag') : locale;

Note: We provide a sample that shows a simple solution for using resource bundles in custom component on the Oracle TechExchange blog.

Page 19: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 19

https://blogs.oracle.com/mobile/techexchange%3a-a-simple-guide-and-solution-to-using-resource-bundles-in-custom-components

Translation Strategies and Practices Tooling is not enough for building multi language bots. Without a plan the success of your translations is determined by luck and coincidence. Below are suggestions of good practices we found when working with translation services.

Opt-in vs. Opt-out There exist two implementation strategies for adding translations to a bot

• Disabling auto-translation and selectively enable translation on individual components using the "translate" property (opt-in).

• Enabling auto-translation so that all content is translated by a translation service except those marked not to be translated (opt-out)

Both strategies lead to equally good results. We – the authors – prefer the opt-in approach to building multi language bots for the following reasons

• Not all translations are expected be handled by the translation service. Developers will use the resource bundle feature provided by Oracle Intelligent Bots or even create their own, custom component based resource bundle mechanism.

• When testing bots it is easier to spot if a component is not translated than badly translated. Opt-in allows an incremental approach to adding translations, which we feel give developers more control.

Translation opt-out is a good technique for prototyping and layout testing. As many know, German words and sentences are usually longer than the same in English. So to test the bot response layout in German you would use auto-translation and disable translation for some components, which should not have their content translated.

Use Resource Bundles for Prompts and Responses It is not only the grammar and the wording you want to be in control of for output messages but also the tone in which things are said. If you are in the banking and insurance business then your business tone most likely is more formal than if you run a radio station. Resource bundles allow you to define the tone and ensure that the language used fits the business.

Use Resource Bundles for Content that Should not be Translated What does 'Summer' mean to you? Well, in the US 'Summer' could be both, a time of the season and a female first name. The point is that some words or names need to stand as they are because they reflect names or business terminology. If you enable auto-translation then those strings will be translated with the sentence they are surrounded by.

The option for you to choose here is to use Resource bundles and to set the translate property on a component to "false". Resource bundles translate component output based on the following criteria

• The profile.locale or profile.languageTag variables are set to the user preferred language code

• A bundle exists for the detected user language

Page 20: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 20

Using placeholders (e.g. {0}) in the resource bundle text, you can pass content in at runtime. The content that is passed in at runtime will not be translated and thus remains the name or business term you want to display.

What if a resource bundle is referenced from a component that has its translate property set to true? In this case all resource bundle content, including the content passed in at runtime, is getting auto-translated.

Test Your Translation Service Using backward Translations So what about entity extraction? This indeed is where you need to pay some extra attention. Translation services may not translate a foreign word exactly to the matching English word but to one that is close. Especially business terminology may be subject to an incorrect translation.

For example, Google translates the English word "checking", as a bank account type, to "Überprüfung" in German, which is not the proper translation.

However, the phrase "checking account" is translated properly to "Girokonto" in German. The term "Giro" entered by a German-speaking user is translated to "giro" instead of checking. However, "Girokonto" gets translated to "checking account", which is correct.

As design practices you should therefore consider

• test the translation service and optionally create synonyms for entity values where the translation service deviates from the expected word.

• consider "blind testing" testing. The bot developer usually knows about the utterances that were used to train the bot and for this reason is not the best person to test multi-language bots. Good testing aims for the bot to fail, not to succeed.

• avoid use of abbreviations or slang even if understood in a region. So instead of displaying a list of value with the word "checking" in it, use the words "checking account". The more clues you give for a translation service the better is the quality

• Whenever there is a fixed set of choice, use lists or card layouts for the user to select from. This allows you to control the input strings to work with.

Be Aware of the Limitations of Language Detection The Oracle Intelligent Bots System.DetectLanguage component uses the configured translation service to detect the user language from a sample sentence. While this automatic language detection works well for many languages, you need to be aware of limitations that exist for languages that are closely related. For example, lets consider the Swedish, Danish and Norwegian translation of "Good morning my friend".

• Swedish: “God morgon min vän!”

• Danish: “God morgen min ven!”

• Norwegian: ”God morgen min venn!”

Imagine the Swedish, Norwegian or Danish sentence to be the sample phrase used for detecting the user languages. Chances are indeed good that the translation service picks the wrong language.

In such cases, beside of testing the bot, you may consider to verify the detected language with the user. You could, for example, show a message asking the user to confirm the detected language. Once the user confirms the language, you save the language code to a user scope variable for next time.

Page 21: Oracle Intelligent Bots TechExchange€¦ · Oracle Intelligent Bots - TechExchange 6 Enabling Translations for a Bot Still, with translation services enabled for a bot, nothing happens

OracleIntelligentBots-TechExchange 21

Limit the Set of Languages Supported by a Bot Using a translation service your bot probably understands more languages than you need support for. It is a good practice to limit the bot to the languages you need to support and that you frequently test. If you work with resource bundles then the list of languages should be reduced to those that you have support bundles for.

To limit the set of languages for a bot you should implement one of the following approaches

• Detect a user language and compare it to a list of supported languages. If the language doesn't match a supported language, display a list of supported languages for the user to select from. Based on the user-selected language you then set the pofile.languageTag variable.

• Don't detect the user language at all but have her selecting a preferred language the first time she uses a bot. You then save this information in a database or the user scope so that each time the user accesses the bot the profile.languageTag is set to the preferred language.

Save and Remember Consider leveraging user scope variables for language related settings. Save the user detected language in a user scope variable and provide the user an option to reset or change this setting. User variables are created with a "user." Prefix in front of the variable names. User scope variables are not pre-defined in BotML and are created at runtime upon its first use. For example, you can use the following BotML code to save the user detected language:

#save detected user language in user scope variable saveUserLanguageState: component: "System.SetVariable" properties: variable: "user.languageSetting" #access language detected by System.DetectLanguage value: "${profile.languageTag}"

Summary This article explored Oracle Intelligent Bots capabilities to leverage translation services for building multi-language bots. It explained the components and configuration, as well as best practices you can use to build a bot that works with and in foreign languages.