iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 7
0

Based on the title, looks like we will be having a boring lesson...

But don't be sad too fast.
Probably when we learn along the way, you will find some interests~ /images/emoticon/emoticon07.gif


The Cloud Natural Language API lets us extract entities from text, perform sentiment and syntactic analysis, and classify text into categories.

  1. Open Google Cloud Platform ( follow the step in A Tour of Qwiklabs and Google Cloud )

  2. Activate Cloud Shell
    Like what we did in the previous lesson.

  3. Create an API Key
    Like what we did in the previous lesson.

  4. Make an Entity Analysis Request
    This analyzeEntities method can extract entities (like people, places, and events) from text.

4-1. Create a request.json file and add the following code:

{
  "document":{
    "type":"PLAIN_TEXT",
    "content":"Joanne Rowling, who writes under the pen names J. K. Rowling and Robert Galbraith, is a British novelist and screenwriter who wrote the Harry Potter fantasy series."
  },
  "encodingType":"UTF8"
}

This API supports type values are PLAINTEXT or HTML.

4-2. Call analyzeEntities method:

curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" \
  -s -X POST -H "Content-Type: application/json" --data-binary @request.json > result.json

The result will be something like this:
https://ithelp.ithome.com.tw/upload/images/20200920/201300542n5eDpdCGL.png

Salience -> a number in the [0,1] range that refers to the centrality of the entity to the text as a whole.

  1. Sentiment analysis with the Natural Language API

5-1. Replace the content with the following code in request.json

{
  "document":{
    "type":"PLAIN_TEXT",
    "content":"Harry Potter is the best book. I think everyone should read it."
  },
  "encodingType": "UTF8"
}

5-2. Run analyzeSentiment method:

curl "https://language.googleapis.com/v1/documents:analyzeSentiment?key=${API_KEY}" \
  -s -X POST -H "Content-Type: application/json" --data-binary @request.json

The result will be like this:
https://ithelp.ithome.com.tw/upload/images/20200920/20130054NeVjq1oDIo.png

  • partOfSpeech -> tells you that "Joanne" is a noun.
  • dependencyEdge -> includes data that you can use to create a dependency parse tree of the text.
  • headTokenIndex -> the index of the token that has an arc pointing at "Joanne". Think of each token in the sentence as a word in an array.
  • lemma -> the canonical form of the word. For example, the words run, runs, ran, and running all have a lemma of run. The lemma value is useful for tracking occurrences of a word in a large piece of text over time.

A dependency parse tree for the sentence above would look like this:
https://ithelp.ithome.com.tw/upload/images/20200920/20130054yJFEZ3Q84j.png

  1. Multilingual natural language processing
    The Natural Language API also supports languages other than English.

6-1. Modify the code in request.json with a sentence in Japanese:

{
  "document":{
    "type":"PLAIN_TEXT",
    "content":"日本のグーグルのオフィスは、東京の六本木ヒルズにあります"
  }
}

Notice that you didn't tell the API which language the text is, it can automatically detect it!

6-2. Run analyzeEntities method:

curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" \
  -s -X POST -H "Content-Type: application/json" --data-binary @request.json

The result is very cool:
https://ithelp.ithome.com.tw/upload/images/20200920/20130054ZBBvEIr9h4.png

Even cooler is the wikipedia URLs point to the Japanese Wikipedia pages!!!

What a smart text analysis!!!


Surprise! Today's lesson is over!

Didn't expect it's a short and sweet article this time right~ /images/emoticon/emoticon24.gif

The natural language is so powerful!

It "speaks" our languages and even "understands" more language than we do!

Hope you enjoy today's sharing~


上一篇
Detect Labels, Faces, and Landmarks in Images with the Cloud Vision API
下一篇
Awwvision: Cloud Vision API from a Kubernetes Cluster
系列文
Machine Learning Study Jam 202012
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言