Page 27 - MSDN Magazine, November 2017
P. 27

understanding of linguistic concepts and actions in free-form text. It performs part-of-speech (POS) tagging, tokenization and sentence separation, among other things. You can use this API to mine information from customer feedback and to understand the intent of users and the structure of the text.
In this article, I’ll show how these APIs work and the scenarios where they can be used to generate insights from the various forms of text encountered every day.
What Does Text Analytics Do?
The Text Analytics API is a suite of services built with Azure Machine Learning. At the time of this writing, you can use the following API operations to perform the actions mentioned earlier:
• Detect language determines the language of the text sent to the API from among the 120 supported languages.
• Key phrases finds key phrases in the text input.
• Sentiment detects the sentiment of the text, returning a numeric score between 0 and 1, where a higher score denotes
the degree of positive sentiment.
• Detect topics discovers topics or phrases from a corpus of
text in a batch operation. This operation is marked for dep-
recation and won’t be covered in the article.
The Text Analytics API is pre-trained to perform these oper- ations and doesn’t need any training data. It can handle most of the reviews on most shopping portals. To help you better under- stand the capabilities of the Text Analytics API, take a look at the
following example review:
“This phone has a great battery. The display is sharp and bright. But the store does not have the apps I need.”
Figure 1 The Languages Result
This excerpt reflects a shopper’s experience with a phone. Let’s run this example through the different API operations and see what turns up. Along the way, you can use any utility you choose (such as Fiddler, Postman or Curl) for these tests to see the results for yourself. You can also use the API Testing Console, available in the API reference documentation. But there are a few things you
need to know before you can test run these APIs:
1. You need a subscription key. If you don’t have an Azure
subscription, you can get a trial key from bit.ly/2eLG8OT. 2. The API expects to receive the text record in the
following format:
{
"documents": [
{
"language": "string", "id": "string", "text": "string"
} ]
}
This format applies to the key phrases and the sentiment oper- ations. However, you’ll need to remove the “language” field for the calls to the detect language operation.
I’ll start with a simple test to determine the language of the example text, and the request body will be formatted like so:
{
}
I’m using the Curl utility and here’s what my request looks like:
curl -v -X POST "https://westus.api.cognitive.microsoft.com/text/ analytics/v2.0/languages"
-H "Content-Type: application/json"
-H "Ocp-Apim-Subscription-Key: my-trial-key"
--data-ascii "{ "documents": [
{
"id": "1",
"text": "This phone has a great battery. The display is sharp and bright.
But the store does not have the apps I need." }
] }"
And the result I get back after I place this request is shown in Figure 1.
As you can see, the result is a scored response and English was detected as the language of the text record submitted. You are wel- come to try out a different language to see how it works.
Next, I’ll run the same record through the key phrase endpoint.
I need to change my Curl command with the updated endpoint
and the request body as follows:
curl -v --silent -X POST "https://westus.api.cognitive.microsoft.com/text/ analytics/v2.0/keyPhrases"
-H "Content-Type: application/json"
-H "Ocp-Apim-Subscription-Key: my-trial-key"
--data-ascii '{ "documents": [
{ "language": "en",
"id": "1",
"text": "This phone has a great battery. The display is sharp and bright.
But the store does not have the apps I need." }
] }'
The response is shown in Figure 2.
] }
"documents": [ {
"id": "1",
"text": "This phone has a great battery. The display is sharp and bright.
But the store does not have the apps I need."
{
"documents": [
{
"id": "1", "detectedLanguages": [
{
"name": "English", "iso6391Name": "en", "score": 1
} ]
} ],
"errors": [] }
Figure 2 The Key Phrases Result
{
"documents": [
{
"keyPhrases": [
"phone",
"great battery", "display", "store",
"apps"
],
"id": "1" }
],
"errors": [] }
msdnmagazine.com
November 2017 23
















   25   26   27   28   29