Natural Language API를 사용한 항목 및 감정 분석 | Google Cloud Skills Boost
Cloud Natural Language API를 사용하면 텍스트 블록에서 항목을 추출하고 감정 및 구문 분석을 수행할 수 있습니다. 이 실습에서는 Cloud Natural Language API를 사용하여 텍스트에서 항목과 감정을 추출하는
www.cloudskillsboost.google
이번 실습에서는 Natural Language API를 사용하여 entity, sentiment, syntax를 분석하는 방법을 배운다.
Navigation menu -> APIs & Services -> Credentials

CREATE CREDENTIALS -> API key
key 복사

Navigation menu -> Compute Engine
vm instance에 SSH로 접속

복사해뒀던 키를 환경변수로 저장
export API_KEY=<YOUR_API_KEY>
request.json을 열어 다음 코드를 저장
nano 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" }
Cloud Storage에 저장된 파일을 보내고 싶다면 content 대신 gcsContentUri와 text 파일의 url을 key와 value로 설정해주면 된다.
request를 보내고 결과를 result.json에 저장하자.
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
result.json
{ "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" }
이번에는 sentiment analysis를 해보자.
request.json을 조금 더 sentiment가 강한 문장으로 바꿔보자.
{ "document":{ "type":"PLAIN_TEXT", "content":"Harry Potter is the best book. I think everyone should read it." }, "encodingType": "UTF8" }
request
curl "https://language.googleapis.com/v1/documents:analyzeSentiment?key=${API_KEY}" \ -s -X POST -H "Content-Type: application/json" --data-binary @request.json
result
{ "documentSentiment": { "magnitude": 1.9, "score": 0.9 }, "language": "en", "sentences": [ { "text": { "content": "Harry Potter is the best book.", "beginOffset": 0 }, "sentiment": { "magnitude": 0.9, "score": 0.9 } }, { "text": { "content": "I think everyone should read it.", "beginOffset": 31 }, "sentiment": { "magnitude": 0.9, "score": 0.9 } } ] }
score는 -1.0 부터 1.0 까지의 수로 positive와 negative를 의미한다.
manitude는 0부터 무한대까지의 수로 weight를 표현한다.
(얼마나 강하게 sentiment를 나타내는지)
"I liked the sushi but the service was terrible."
이라는 문장을 살펴봤을 때 위와 같이 문장 전체의 sentiment score르 얻는 것은 그리 유용하지 않을 것이다.
이런 경우 사용할 수 있는 방법이 analyzeEntitySentiment 이다.
request.json 파일을 다음과 같이 바꿔주자.
{ "document":{ "type":"PLAIN_TEXT", "content":"I liked the sushi but the service was terrible." }, "encodingType": "UTF8" }
request
curl "https://language.googleapis.com/v1/documents:analyzeEntitySentiment?key=${API_KEY}" \ -s -X POST -H "Content-Type: application/json" --data-binary @request.json
result
{ "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와 service 두 가지 entity로 구분하여 sentiment score를 매기는 것을 확인할 수 있다.
sushi는 0점, service는 -0.7점으로 잘 들어맞는다.
이번에는 syntactic analysis를 해보자.
request.json을 바꿔준다.
{ "document":{ "type":"PLAIN_TEXT", "content": "Joanne Rowling is a British novelist, screenwriter and film producer." }, "encodingType": "UTF8" }
request
curl "https://language.googleapis.com/v1/documents:analyzeSyntax?key=${API_KEY}" \ -s -X POST -H "Content-Type: application/json" --data-binary @request.json
result
"person": "PERSON_UNKNOWN", "proper": "PROPER_UNKNOWN", "reciprocity": "RECIPROCITY_UNKNOWN", "tense": "TENSE_UNKNOWN", "voice": "VOICE_UNKNOWN" }, "dependencyEdge": { "headTokenIndex": 10, "label": "NN" }, "lemma": "film" }, { "text": { "content": "producer", "beginOffset": 60 }, "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_UNKNOWN", "reciprocity": "RECIPROCITY_UNKNOWN", "tense": "TENSE_UNKNOWN", "voice": "VOICE_UNKNOWN" }, "dependencyEdge": { "headTokenIndex": 5, "label": "CONJ" }, "lemma": "producer" }, { "text": { "content": ".", "beginOffset": 68 }, "partOfSpeech": { "tag": "PUNCT", "aspect": "ASPECT_UNKNOWN", "case": "CASE_UNKNOWN", "form": "FORM_UNKNOWN", "gender": "GENDER_UNKNOWN", "mood": "MOOD_UNKNOWN", "number": "NUMBER_UNKNOWN", "person": "PERSON_UNKNOWN", "proper": "PROPER_UNKNOWN", "reciprocity": "RECIPROCITY_UNKNOWN", "tense": "TENSE_UNKNOWN", "voice": "VOICE_UNKNOWN" }, "dependencyEdge": { "headTokenIndex": 2, "label": "P" }, "lemma": "." } ], "language": "en" }
partOfSpeech (POS) 는 "Joanne"이 명사임을 알려준다.
dependencyEdge는 dependency parse tree를 만드는 데 사용할 수 있는 data이다.
(token 간의 관계를 나타냄)
일본어에 관한 요청도 API에 보내보자
request.json
{ "document":{ "type":"PLAIN_TEXT", "content":"日本のグーグルのオフィスは、東京の六本木ヒルズにあります" } }
언어를 지정해주지 않아도 자동으로 찾는다.
request
curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" \ -s -X POST -H "Content-Type: application/json" --data-binary @request.json
result
{ "entities": [ { "name": "日本", "type": "LOCATION", "metadata": { "mid": "/m/03_3d", "wikipedia_url": "https://en.wikipedia.org/wiki/Japan" }, "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": { "wikipedia_url": "https://en.wikipedia.org/wiki/Roppongi_Hills", "mid": "/m/01r2_k" }, "salience": 0.19418614, "mentions": [ { "text": { "content": "六本木ヒルズ", "beginOffset": -1 }, "type": "PROPER" } ] }, { "name": "東京", "type": "LOCATION", "metadata": { "mid": "/g/11b6gnvfr8", "wikipedia_url": "https://en.wikipedia.org/wiki/Special_wards_of_Tokyo" }, "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" }
'Cloud > Google Cloud Study Jam' 카테고리의 다른 글
Bayes Classifier on Dataproc (0) | 2022.05.08 |
---|---|
Speech to Text Transcription with the Cloud Speech API (0) | 2022.03.28 |
Cloud Natural Language API: Qwik Start (0) | 2022.03.28 |
Google Cloud Speech API: Qwik Start (0) | 2022.03.28 |
AI Platform: Qwik Start (0) | 2022.03.28 |
댓글