The translation is that which transforms everything so that nothing changes. -Günter Grass - Novelist
In the following exercise, you will be adding translation capabilities to your chatbot by using IBM Watson and Node-RED editor. Watch the video below and go through the steps and by the end, you should have a chatbot that can translate text into languages of your choosing.
VIDEO
IBM's Language Translator translates to and from +40 languages using deep learning that is improving its speed and accuracy.
Create New Resource
To begin you need to add the language translator as a resource from IBM's catalog. Navigate to your resource list in IBM Watson and click on the 'Create resource' button in the top right.
Search and Install the Language Translator
Do as search for 'Language Translator' from IBM's cloud products to find the translator. Once you find what you are looking for, install it by clicking the 'Create' button in the bottom right.
Connect the Translator to Node-RED
Once the translator is installed as a resource go to its Connections page and connect the same Node-RED app that was used in the previous exercise.
Navigate to IBM Watson Assistant's Skills
Navigate to the Watson Assistant's Skill menu where you previously edited the ChatBot skill during the exercise. Click to open it as you will be editing the same skill.
Create an New Intent
In the ChatBot skill's Intents menu click on the 'Create intent' button.
Name the intent as '#Translate' and give it a fitting description then some user examples that can be used to begin translating text in a chat conversation. Phrases like 'translate', 'please translate', and 'translate for me please' are fine examples.
Create a Language Entity
In the ChatBot skill's Entities menu click on the 'Create entity' button.
Name the entity '@language' and input real languages as values. English, French, Italian, Spanish, and Finnish is used as examples in the exercise. This is done so that the bot can store the languages used in the translation in its memory.
Add Translation Dialog Node
In the ChatBot skill's Dialog menu click on the Farewells node's dropdown option 'Add node below' to create a new response for the translator.
Name the newly created node 'Translate' and choose '#Translate' intent for the 'If assistant recognizes' option.
Write a response for the assistant to ask the user what will be the source language for the translation request. Write something like 'What language do you want to translate from?'.
Add Source Language Node
Click on the Translate node's dropdown option 'Add child node' to create a new node for the source language. Make sure you are creating a child node and not adding a new one just like all the previous nodes you have made.
Name the new child node as the 'Source Language' and choose the '@language' for the 'If assistant recognizes' option.
For the assistant to store the user's given language you'll need to open the JSON editor for the assistant's response. Write in the following code.
"context": { "source": "@language" },
"output": {
"text": {
"values": [ "What language do you want to translate to?" ]
}
}
}
Add Destination Language Node
Do as you did in the previous step and but this time create a child node for the Source Language node.
Name the child node as 'Destination Language' and choose the '@language' for the 'If assistant recognizes' option.
This time write the following code to the JSON editor.
"context": { "destination": "@language" },
"output": {
"text": {
"values": [ "What is the text that you want to translate?" ]}}}
Add Do Translate Node
Add the final node you will be making for the dialog. Create a new child to the Destination Language node and name it 'Do Translate'.
Simply write 'true' into the field in the 'If assistant recognizes' option. Then input the following code into the JSON editor.
"context": { "action" : "translate" },
"output": {}
}
Create a Switch Node
Next move over to the Node-RED application and to the flow chart of the chatbot.
Place a 'switch' node from the left side menu just after the first assistant node. Edit its property field and input 'msg.payload.context.action'.
Click on the add symbol at the bottom of the list so the node has two outputs one containing symbol '==' and 'translate' and another called 'otherwise'. Please refer to the image for clarity's sake. The order of outputs matters so have them ordered as pictured.
The switch creates a fork in the flow of the bot's program which chooses which connected node it runs according to the intent.
Create Prepare for Translation Node
Add another node after the 'switch' this time a function node. Edit its properties and name it 'Prepare for Translation'. Add the following code to its function field.
msg.srclang = getLanguage(msg.payload.context.source);
msg.destlang = getLanguage(msg.payload.context.destination);
msg.payload = msg.payload.input.text;
return msg;
function getLanguage(lang) {
switch (lang.toLowerCase()) {
case "french":
return "fr";
case "italian":
return "it";
case "spanish":
return "es";
case "finnish":
return "fi";
}
return "en";
}
Add the Language Translator Node
Add the IBM's translator node from the left side menu and place it after the Prepare for Translation node. Then connect the switch node to the function nodes when also connecting the just created function node to the translator node.
You can leave the translator nodes properties as they are.
Add Prepare for Telegram 2 Node
Add a new function node next to the language translator and name it as 'Prepare for Telegram 2'. Input the following code for it.
msg.payload = {
chatId : msg.chatId,
type : "message",
content : msg.payload
};
return msg;
Connect the translator node to the new function node after which connect the function to the Telegram sender node which should have both the Prepare for Telegram nodes connected to it.
Add Clear Context Node
Finally add one more function node naming it 'Clear Conversation Context' and inputting the following code.
msg.payload = "hi";
msg.params = {
context : {}
}
return msg;
Copy the assistant node from the flow by right-clicking it and choosing 'copy' and then right-clicking on the board and choosing 'paste' or alternatively by selecting the node and hitting 'ctrl + c' and then 'ctrl + v' on your keyboard.
Place the copied node after the newly created function node and connect the translator node to the clear context node while connecting it to the copy of the assistant node. This function node is added so that the chatbot can continue its operation normally after translating text.
Deploy and Test the Solution
Hit deploy on the top right in Node-Red. If the deployment fails because of errors in your code the program displays red warning signs on the nodes that contain faulty code.
If the deployment was a success you can test the chatbot's translation skill by using the commands you've set for it. If you followed the exercise's instructions it should work by typing '/Watson translate' then followed by the languages you want the translator to translate from and to finally followed by the text you want to be translated.
That is Finnish for 'Good work!', you are ready to move on.
TEHTÄVIÄ