🈸Creating Translate Feature

translate_featur

This time we will create the translate feature in the chat by translating the intended word (intent) by the user using NLP Wit.ai.

Creating user intent, entity and train utterance with Wit.ai#

  1. Go to Apps Wit.ai page, make sure you have login in using a facebook account.

  2. Create a wit.ai application project

    Show Details

    create wit.ai app

  3. add utterance Translate <example word / sentence> for example 'Translate community'> create 'translate' intent.

    Show Details

    create utterance and intent

  4. Create word entities to be translated by block word Community> select wit/ phrase_to_translate.

    Show Details

    make an entity

  5. Click the 'Train and Validate' button to train and validate the utterances, intents, and entities that we add.

    Show Details

    train_and_validate

  6. Continue to train our wit.ai application by entering the various possible utterances that the user uses to translate.

    Show Details

    train_again utterances

What is NLP? Why wit.ai?

Natural Language Processing (NLP) allows you to understand the meaning of a user input. You may need it as a stand-alone layer to parse text or speech into structure data. You will also need NLP to build a conversational app to understand the user query and extract meaningful information out of it. source

We use wit.ai because we don't need code to connect it with our app. we can connect it through Fb app developer page that I explain in the next section and it's easy to define intent, entities, and train utterance in wit.ai.

What are intent, entity, and utterance?
  • intent is what your end-user wants to perform, for example, is translate a word.
  • entity is data that we get from end-user utterance, for example, translate developer. the entity is developer.
  • utterance is what the user says, for example, are translate developer, what is developer, translate community.
tip

The more utterances you validate for each intent, the better it will work. source

Connecting Wit.ai with FB Messenger#

after we train the wit.ai application, then we connect it with the application on our fb messenger.

  1. Open the facebook developer dashboard page that we have created> go to 'Messenger Settings' > in the Built-in NLP section select the application page.

    Show Details

    connect_to_wit.ai

  2. In the 'Default Language Model' select 'Custom Model`.

    Show Details

    custom_model

  3. Click 'Link to existing Wit App' button.

    Show Details

    Link_to_existing_wit_app

  4. Enter the wit.ai app server access token obtained on the wit.ai application settings page.

    Show Details

    get_server_access_token_wit add_server_access_token_wit

  5. After successfully connecting, every user message that enters our application will be processed through wit.ai and will send the nlp object into the webhook request as follows :

    //console.log(received_message.nlp.entitites)
    {"intent":[{"confidence":0.99694817127169,"value":"translate"}],"phrase_to_translate":[{"suggested":true,"confidence":0.91449405248263,"value":"home","type":"value"}]}

Using the Translate API#

now we will use the API 'Just Translated' in RapidAPI to translate the words that users enter in the chat.

  1. Go to Just Translated RapidAPI page, pastikan sudah login.

  2. Click 'Subscribe to Test' button.

    Show Details

    subscribe_translate_api

  3. Select 'Basic' Plan.

    Show Details

    select_just_translated_api_plan

  4. Create a variable RAPID_API_KEY in the .env file, and fill it with the api key in Just Translated RapidAPI.

    Show Details

    get_translate_api_key

  5. Copy the sample code provided by RapidAPI, on the right, select the programming language Node.js > Request.

    Show Details

    select_program_lang

  6. Create a translate (text, lang) function from the previously copied code to translate the word in the app.js file and edit it like this.

    async function translate(text, lang) {
    return new Promise((resolve, reject) => {
    var options = {
    method: "GET",
    url: "https://just-translated.p.rapidapi.com/",
    qs: { text: text, lang_from: "en", lang_to: lang },
    headers: {
    "x-rapidapi-host": "just-translated.p.rapidapi.com",
    "x-rapidapi-key": process.env.RAPID_API_KEY,
    useQueryString: true
    }
    };
    request(options, function(error, response, body) {
    if (error) reject(error);
    let res = JSON.parse(body);
    resolve(res.text[0]);
    });
    });
    }
    Show Details

    translate_function

  7. To call the translate function, text and language parameters are needed, so that there are not many requests to get user data in firebase, then create a global language variable var language, then fill in these variables when the user adds with language = docSnapshot.data () .language and updating its data with language = req.body.lang.

    Show Details

    var_lang

    assign_language_variable

    update_language_variable

  8. Updated the handleMessage function to call the translate function when nlp (wit.ai) confidence intent translate and entity phrase_to_translate> 0.8.

    async function handleMessage(sender_psid, received_message) {
    let response;
    // check when message contains text
    if (received_message.text) {
    //create variables from the object passed nlp wit.ai
    const objNlp = received_message.nlp.entities;
    //Checks whether there is an intent translate with a confidence greater than 0.8, as well as a confidence entity phrase_to_translate
    if (
    objNlp.intent &&
    objNlp.intent[0].value == "translate" &&
    objNlp.intent[0].confidence > 0.8 &&
    objNlp.phrase_to_translate[0].confidence > 0.8
    ) {
    //check whether the language variable is filled, otherwise it will retrieve data from Firebase
    language = language ? language : await db.doc(`users/${sender_psid}`).get().then(docSnapshot => {
    if (docSnapshot.exists) {
    return docSnapshot.data().language
    }});
    //call translate function
    let tr = await translate(objNlp.phrase_to_translate[0].value, language);
    //update the response variable with the result from the translate function
    response = {
    text: tr
    };
    }
    }
    // send response to user
    callSendAPI(sender_psid, response);
    }
What is API?

Application Programming Interface (API) is like a way to access a service or method between apps. For example, Hayword app doesn't create a translate service because it's complicated but uses a third-party service to translate (Just Translate API) through API.

tip

Find more APIs on RapidAPI or ProgrammableWeb