DEV Community

Cover image for How you can learn Language Analysis using AI services in the Cloud and JavaScript
Chris Noring for Microsoft Azure

Posted on • Updated on

How you can learn Language Analysis using AI services in the Cloud and JavaScript

This article is part of #25DaysOfServerless. New challenges will be published every day from Microsoft Cloud Advocates throughout the month of December. Find out more about how Microsoft Azure enables your Serverless functions.

Have an idea or a solution?

It's freezing cold up here on the North Pole, which normally makes it the ideal place to host a server farm. But today Santa's elves are freaking out!
Children all over the world write Santa letters to say what they want for Christmas. The elves had scripts running locally on the server farm to process the letters but without the missing servers this is no longer possible. Santa could translate manually, but he won't be able to get through all the letters in time!

Resources

To get the children's wishes, find them at this API:

https://aka.ms/holiday-wishes

  • Free account Azure account You will need to sign up on Azure to use the Text Analytics Service

  • Try Text Analytics This will let you test the service from a web page so you see what you are supposed to input but also what kind of output to expect.

The Challenge

Write a serverless application that helps Santa figure out if a given child is being naughty or nice based on what they've said.
You'll likely need to detect the language of the correspondence, and then perform sentiment analysis to determine whether it's naughty or nice.

Want to submit your solution to this challenge?

Build a solution locally and then PR this repo. If your solution doesn't involve code you can record a short video and submit it as a PR to the same repo. Make sure to tell us which challenge the solution is for. We're excited to see what you build! Do you have comments or questions? Add them to the comments area below.

Watch for surprises all during December as we celebrate 25 Days of Serverless. Stay tuned here on dev.to as we feature challenges and solutions! Sign up for a free account on Azure to get ready for the challenges!

 Solution

We've got two problems to solve:

  • Language, we need to detect what language the letters are written in
  • Sentiment analysis, Once knowing the language we need to run a sentiment analysis on them

Provision Azure Resources

For it to be possible to carry out everything we need to provision some Azure resources. Each resource will give you an endpoint and an API key:

  • Text Analytics, this will help to detect language and translate

Text Analytics

  1. portal.azure.com
  2. + Create a resource
  3. Text Analytics
  4. Go to resource and click Quick start. Note the values of Key1 and Endpoint

-1- Detect language

Here we want to be able to detect a language given an input. HINT, you need to do this per child. All messages from one child are likely written in one language. So pick out one message from each child

Create a directory for your app, for example:

mkdir smartapps
cd smartapps

Then initialize a Node.js project:

npm init -y

Next, install the node-fetch library, we will use it to do HTTP requests:

npm install node-fetch

Lastly create a file app.js.

Perform a request

Remember the ENDPOINT value you noted down from Text Analytics.

const BASE_URL = '<replace with `Endpoint` from Text Analytics resource>'
const url = 'text/analytics/v2.1/languages';
const API_KEY = '<replace with `Key1` from Text Analytics resource>';

/*
this needs to have a certain format

let documents = {
    'documents': [{
      'id': '1',
      'text': 'my little brother is so annoying and stupid'
     },
     {
      'id': '2',
      'text': 'Eu odeio limpar meu quarto'
     },
     {
      'id': '3',
      'text': '爸爸和圣诞老人​​很相似'
     } 
  ]
}

*/
const body = '<see above comment for example body>';

fetch(`${BASE_URL}/${url}`, { 
  method: 'post',
  body: JSON.stringify(body),
  headers: {
    'Ocp-Apim-Subscription-Key': API_KEY,
    'Content-Type': 'application/json'
  },
})
.then(res => res.json())
.then(json => console.log)

What comes back from the response will have the following format:

{
  "documents": [
    {
      "id": "1",
      "detectedLanguages": [
        {
          "name": "English",
          "iso6391Name": "en",
          "score": 1
        }
      ]
    },
    {
      "id": "2",
      "detectedLanguages": [
        {
          "name": "Portuguese",
          "iso6391Name": "pt",
          "score": 1
        }
      ]
    },
    {
      "id": "3",
      "detectedLanguages": [
        {
          "name": "Chinese_Simplified",
          "iso6391Name": "zh_chs",
          "score": 1
        }
      ]
    }
  ],
  "errors": []
}

Ok we have three different languages.

  • Adam is English speaking
  • Eva is Portugese
  • Tracy is Chinese

-2- Detect sentiment

Now that we know the language for three children - we can call a sentiment service to detect if the messages are naughty or nice. The value coming back will be between 0 and 1. The closer to 1 the more positive and vice versa.

Perform a request

Note the example input and how we now use the field language. This was something we didn't know before carrying out language detection and something we needed for sentiment analysis. Now that we know the language we can simply update the language property with the correct language according to the response.

HINT, use the field iso6391Name from the response.

It's a good idea to keep using the same project that you started with. As you can see that call for sentiment analysis is very similar to language detection.

const BASE_URL = '<replace with `Endpoint` from Text Analytics resource>'
const url = 'text/analytics/v2.0/sentiment';
const API_KEY = '<replace with `Key1` from Text Analytics resource>';

/*
{
  'documents': [{
      'id': '1',
      'language': 'en',
      'text': 'my little brother is so annoying and stupid'
    },
    {
      'id': '2',
      'language': 'en',
      'text': 'I really like the bike I got for a present'
    },
    {
      'id': '3',
      'language': 'en',
      'text': 'The food is really bad at home, I'd rather go to McDonalds'
    }
  ]
}
*/

const body = '<see above comment for example body>';

fetch(`${BASE_URL}/${url}`, { 
  method: 'post',
  body: JSON.stringify(body),
  headers: {
    'Ocp-Apim-Subscription-Key': API_KEY,
    'Content-Type': 'application/json'
  },
})
.then(res => res.json())
.then(json => console.log)

What comes back from the response will have the following format:

{
  "documents": [
    {
      "id": "1",
      "score": 0.0031304657459259033
    },
    {
      "id": "2",
      "score": 0.8319455981254578
    },
    {
      "id": "3",
      "score": 0.030931025743484497
    }
  ],
  "errors": []
}

Adam

So what do we have here 0.003, 0.83 and 0.03. Two really bad ones and a good one. Your call here parents but my vote is on COAL.

Eva

So for Eva we will send in the following input to the Sentiment analysis:

let documents = {
    'documents': [{
    'id': '1',
    'language': 'pt',
    'text': 'Eu odeio limpar meu quarto '
  },
  {
    'id': '2',
    'language': 'pt',
    'text': 'meu professor é legal'
  }, {
    'id': '3',
    'language': 'pt',
    'text': 'o cachorro vizinho é estúpido e fedorento'
  }]
}

So what do we have here 0.004, 0.53, 0.06. Worse than Adam pretty much. Wow, what's with kids these days. Another COAL

Tracy

Tracy, our hope is with you. Don't fail us.

Given the input:

let documents = {
  'documents': [{
      'id': '1',
      'language': 'zh_chs',
      'text': '爸爸和圣诞老人​​很相似'
    },
    {
      'id': '2',
      'language': 'zh_chs',
      'text': '我讨厌饭'
    }, {
      'id': '3',
      'language': 'zh_chs',
      'text': '我喜欢我的小妹妹,她好可爱'
    }
  ]
}

We get the following results:
0.32, 0.38, 0.74. Not too bad. Way better than the other two. Is that we settle for today, above horrible? Alright, Tracy is getting what she wished for. Let's see what she wanted:

World peace, A puppy, A Bicycle.

Bicycle it is :)

Summary

We were able to perform the two needed steps of language detection followed by sentiment analysis and could thereby carry out the needed task.

Want to submit your solution to this challenge?


Want to submit your solution to this challenge? Build a solution locally and then submit an issue. If your solution doesn't involve code you can record a short video and submit it as a link in the issue description. Make sure to tell us which challenge the solution is for. We're excited to see what you build! Do you have comments or questions? Add them to the comments area below.


Watch for surprises all during December as we celebrate 25 Days of Serverless. Stay tuned here on dev.to as we feature challenges and solutions! Sign up for a free account on Azure to get ready for the challenges!

Latest comments (0)