[Cloud Natural Language API] Entity and Sentiment Analysis with the Natural Language API

yejinยท2026๋…„ 4์›” 15์ผ

Google Skills

๋ชฉ๋ก ๋ณด๊ธฐ
6/46

Course

Analyze Speech and Language with Google APIs

Lab

๋ชฉ๋ก


๐ŸŒ Entity and Sentiment Analysis with the Natural Language API

๊ฐœ์š”

Natural Language API๋ฅผ ์‚ฌ์šฉํ•ด ํ•ญ๋ชฉ, ๊ฐ์ •, ๋ฌธ๋ฒ•์„ ๋ถ„์„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž!

์‹ค์Šต๊ณผ์ •

(1) API ํ‚ค ์ƒ์„ฑ


โžก๏ธ Cloud Natural Language API ๋ฅผ ์„ ํƒํ•œ๋‹ค.


(2) SSH ์ ‘์† ์—ด๊ณ  API_KEY๋ฅผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ๋“ฑ๋กํ•˜๊ธฐ


โžก๏ธ Compute Engine > VM instance > SSH Shell ์—ด๊ธฐ


(3) ํ•ญ๋ชฉ ๋ถ„์„ ์š”์ฒญ ๋ฐ ํ˜ธ์ถœ

1) request.json ํŒŒ์ผ ์ƒ์„ฑ

{
  "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"
}

2) API ํ˜ธ์ถœ

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

3) ๊ฒฐ๊ณผ ๋ถ„์„

{
  "entities": [
    {
      "name": "Joanne Rowling",
      "type": "PERSON",
      "metadata": {
        "wikipedia_url": "https://en.wikipedia.org/wiki/J._K._Rowling",
        "mid": "/m/042xh"
      },
      "salience": 0.79828626,
      "mentions": [
        {
          "text": {
            "content": "Joanne Rowling",
            "beginOffset": 0
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "Rowling",
            "beginOffset": 53
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "novelist",
            "beginOffset": 96
          },
          "type": "COMMON"
        },
        {
          "text": {
            "content": "Robert Galbraith",
            "beginOffset": 65
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "pen names",
      "type": "OTHER",
      "metadata": {},
      "salience": 0.07300248,
      "mentions": [
        {
          "text": {
            "content": "pen names",
            "beginOffset": 37
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "J.K.",
      "type": "PERSON",
      "metadata": {},
      "salience": 0.043804582,
      "mentions": [
        {
          "text": {
            "content": "J. K.",
            "beginOffset": 47
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "British",
      "type": "LOCATION",
      "metadata": {},
      "salience": 0.019752095,
      "mentions": [
        {
          "text": {
            "content": "British",
            "beginOffset": 88
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "fantasy series",
      "type": "WORK_OF_ART",
      "metadata": {},
      "salience": 0.01764168,
      "mentions": [
        {
          "text": {
            "content": "fantasy series",
            "beginOffset": 149
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "Harry Potter",
      "type": "WORK_OF_ART",
      "metadata": {
        "wikipedia_url": "https://en.wikipedia.org/wiki/Harry_Potter",
        "mid": "/m/078ffw"
      },
      "salience": 0.014916742,
      "mentions": [
        {
          "text": {
            "content": "Harry Potter",
            "beginOffset": 136
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "screenwriter",
      "type": "PERSON",
      "metadata": {},
      "salience": 0.011085264,
      "mentions": [
        {
          "text": {
            "content": "screenwriter",
            "beginOffset": 109
          },
          "type": "COMMON"
        }
      ]
    }
  ],
  "language": "en"
}

๊ฒฐ๊ณผ ๋ถ„์„

๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐœ์ฒด์ธ "Joanne Rowling"์„ ๋ณด๋ฉด mentions๊ฐ€ 4๊ฐœ
โžก๏ธ ๋ฌธ์žฅ ์•ˆ์—์„œ ๊ทธ๋…€๋ฅผ ์ง€์นญํ•˜๋Š” ํ‘œํ˜„์ด 4๊ฐœ ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ
โžก๏ธ ์ฆ‰, API๋Š” 'Joanne Rowling', 'Rowling', 'novelist' ๋ฐ 'Robert Galbriath'๋ฅผ ๋ชจ๋‘ ๋™์ผ์ธ์œผ๋กœ ํŒ๋‹จ!


(4) ๊ฐ์ • ๋ถ„์„ ์š”์ฒญ ๋ฐ ํ˜ธ์ถœ

1) request.json ๋‚ด์šฉ ์ˆ˜์ • (์š”์ฒญ ๋‚ด์šฉ ๋ณ€๊ฒฝ)

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

2) API ํ˜ธ์ถœ

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

3) ๊ฒฐ๊ณผ ๋ถ„์„

๋ถ„์„ ๊ฒฐ๊ณผ

  • score: ํ‘œํ˜„์ด ๊ธ์ •์ ์ด๊ฑฐ๋‚˜, ๋ถ€์ •์ ์ธ ์ •๋„๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    • -1.0 (๋งค์šฐ ๋ถ€์ •์ ) <----> 0.0 (์ค‘๋ฆฝ) <----> 1.0 (๋งค์šฐ ๊ธ์ •์ )
  • magnitude: 0~๋ฌดํ•œ๋Œ€์— ์ด๋ฅด๋Š” ์ˆ˜์น˜์ด๋ฉฐ ๊ธ์ •/๋ถ€์ •์ ์ธ ์ •๋„์™€ ์ƒ๊ด€์—†์ด ์ง„์ˆ ์— ํ‘œํ˜„๋œ ๊ฐ์ •์˜ ๋น„์ค‘์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. (๊ธด ํ…์ŠคํŠธ ๋ธ”๋ก๊ณผ ์ค‘์š”๋„๊ฐ€ ๋†’์€ ๋ฌธ์žฅ์ผ์ˆ˜๋ก ํฌ๊ธฐ ๊ฐ’์ด ๋” ๋†’๋‹ค.)

(5) ํ•ญ๋ชฉ ๊ฐ์ • ๋ถ„์„ ์š”์ฒญ ๋ฐ ํ˜ธ์ถœ

1) request.json ๋‚ด์šฉ ์ˆ˜์ • (์š”์ฒญ ๋‚ด์šฉ ๋ณ€๊ฒฝ)

 {
  "document":{
    "type":"PLAIN_TEXT",
    "content":"I liked the sushi but the service was terrible."
  },
  "encodingType": "UTF8"
}

2) API ํ˜ธ์ถœ

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

3) ๊ฒฐ๊ณผ ๋ถ„์„

{
  "entities": [
    {
      "name": "sushi",
      "type": "CONSUMER_GOOD",
      "metadata": {},
      "salience": 0.51064336,
      "mentions": [
        {
          "text": {
            "content": "sushi",
            "beginOffset": 12
          },
          "type": "COMMON",
          "sentiment": {
            "magnitude": 0,
            "score": 0
          }
        }
      ],
      "sentiment": {
        "magnitude": 0,
        "score": 0
      }
    },
    {
      "name": "service",
      "type": "OTHER",
      "metadata": {},
      "salience": 0.48935664,
      "mentions": [
        {
          "text": {
            "content": "service",
            "beginOffset": 26
          },
          "type": "COMMON",
          "sentiment": {
            "magnitude": 0.7,
            "score": -0.7
          }
        }
      ],
      "sentiment": {
        "magnitude": 0.7,
        "score": -0.7
      }
    }
  ],
  "language": "en"
}

๋ถ„์„ ๊ฒฐ๊ณผ

  • ์Šค์‹œ(sushi): 0์ , ์„œ๋น„์Šค(service): 0.7์  โžก๏ธ ๊ธ์ •/๋ถ€์ • ๋‚˜ํƒ€๋‚ธ๋‹ค.

(6) ๊ตฌ๋ฌธ ๋ฐ ํ’ˆ์‚ฌ ๋ถ„์„ ์š”์ฒญ ๋ฐ ํ˜ธ์ถœ

1) request.json ๋‚ด์šฉ ์ˆ˜์ • (์š”์ฒญ ๋‚ด์šฉ ๋ณ€๊ฒฝ)

 {
  "document":{
    "type":"PLAIN_TEXT",
    "content": "Joanne Rowling is a British novelist, screenwriter and film producer."
  },
  "encodingType": "UTF8"
}

2) API ํ˜ธ์ถœ

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

3) ๊ฒฐ๊ณผ ๋ถ„์„

{
  "sentences": [
    {
      "text": {
        "content": "Joanne Rowling is a British novelist, screenwriter and film producer.",
        "beginOffset": 0
      }
    }
  ],
  "tokens": [
    {
      "text": {
        "content": "Joanne",
        "beginOffset": 0
      },
      "partOfSpeech": {
        "tag": "NOUN",
        "aspect": "ASPECT_UNKNOWN",
        "case": "CASE_UNKNOWN",
        "form": "FORM_UNKNOWN",
        "gender": "GENDER_UNKNOWN",
        "mood": "MOOD_UNKNOWN",
        "number": "SINGULAR",
        "person": "PERSON_UNKNOWN",
        "proper": "PROPER",
        "reciprocity": "RECIPROCITY_UNKNOWN",
        "tense": "TENSE_UNKNOWN",
        "voice": "VOICE_UNKNOWN"
      },
      "dependencyEdge": {
        "headTokenIndex": 1,
        "label": "NN"
      },
      "lemma": "Joanne"
    },
    ...
  ],
  "language": "en"
}

๋ถ„์„ ๊ฒฐ๊ณผ

  • sentences: ๋ฌธ์žฅ ๋‹จ์œ„ ๋ถ„์„
  • tokens: ๋‹จ์–ด ๋‹จ์œ„ ๋ถ„์„
    • partOfSpeech: ํ’ˆ์‚ฌ
    • dependencyEdge: ๋‹จ์–ด๊ฐ€ ๋ฌธ์žฅ ๋‚ด์—์„œ ๋‹ค๋ฅธ ๋‹จ์–ด์™€ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐ๋˜๋Š”์ง€ ๋ณด์—ฌ์ค€๋‹ค. (ํ…์ŠคํŠธ์˜ ์ข…์† ํ•ญ๋ชฉ ํŒŒ์‹ฑ ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ)
    • headTokenIndex: ํ† ํฐ์˜ ์ƒ‰์ธ
    • lemma: ๋‹จ์–ด์˜ ๊ธฐ๋ณธํ˜•

(7) ๋‹ค๊ตญ์–ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์š”์ฒญ ๋ฐ ํ˜ธ์ถœ

1) request.json ๋‚ด์šฉ ์ˆ˜์ • (์š”์ฒญ ๋‚ด์šฉ ๋ณ€๊ฒฝ)

{
  "document":{
    "type":"PLAIN_TEXT",
    "content":"ๆ—ฅๆœฌใฎใ‚ฐใƒผใ‚ฐใƒซใฎใ‚ชใƒ•ใ‚ฃใ‚นใฏใ€ๆฑไบฌใฎๅ…ญๆœฌๆœจใƒ’ใƒซใ‚บใซใ‚ใ‚Šใพใ™"
  }
}

2) API ํ˜ธ์ถœ

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

3) ๊ฒฐ๊ณผ ํ™•์ธ

{
  "entities": [
    {
      "name": "ๆ—ฅๆœฌ",
      "type": "LOCATION",
      "metadata": {
        "wikipedia_url": "https://en.wikipedia.org/wiki/Japan",
        "mid": "/m/03_3d"
      },
      "salience": 0.23804513,
      "mentions": [
        {
          "text": {
            "content": "ๆ—ฅๆœฌ",
            "beginOffset": -1
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "ใ‚ฐใƒผใ‚ฐใƒซ",
      "type": "ORGANIZATION",
      "metadata": {
        "mid": "/m/045c7b",
        "wikipedia_url": "https://en.wikipedia.org/wiki/Google"
      },
      "salience": 0.21214141,
      "mentions": [
        {
          "text": {
            "content": "ใ‚ฐใƒผใ‚ฐใƒซ",
            "beginOffset": -1
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "ๅ…ญๆœฌๆœจใƒ’ใƒซใ‚บ",
      "type": "PERSON",
      "metadata": {
        "mid": "/m/01r2_k",
        "wikipedia_url": "https://en.wikipedia.org/wiki/Roppongi_Hills"
      },
      "salience": 0.19418614,
      "mentions": [
        {
          "text": {
            "content": "ๅ…ญๆœฌๆœจใƒ’ใƒซใ‚บ",
            "beginOffset": -1
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "ๆฑไบฌ",
      "type": "LOCATION",
      "metadata": {
        "wikipedia_url": "https://en.wikipedia.org/wiki/Tokyo",
        "mid": "/m/07dfk"
      },
      "salience": 0.18159479,
      "mentions": [
        {
          "text": {
            "content": "ๆฑไบฌ",
            "beginOffset": -1
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "ใ‚ชใƒ•ใ‚ฃใ‚น",
      "type": "OTHER",
      "metadata": {},
      "salience": 0.17403255,
      "mentions": [
        {
          "text": {
            "content": "ใ‚ชใƒ•ใ‚ฃใ‚น",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    }
  ],
  "language": "ja"
}
profile
์ƒˆ์‹น ๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€