Analyze Speech and Language with Google APIs

๋ชฉ๋ก
- Cloud Natural Language API: Qwik Start
- Speech-to-Text API: Qwik Start
- Entity and Sentiment Analysis with the Natural Language API โฌ ๏ธ ์ค๋์ Lab!
- Analyze Speech and Language with Google APIs: Challenge Lab
Natural Language API๋ฅผ ์ฌ์ฉํด ํญ๋ชฉ, ๊ฐ์ , ๋ฌธ๋ฒ์ ๋ถ์ํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์!

โก๏ธ Cloud Natural Language API ๋ฅผ ์ ํํ๋ค.
API_KEY๋ฅผ ํ๊ฒฝ ๋ณ์์ ๋ฑ๋กํ๊ธฐ
โก๏ธ Compute Engine > VM instance > SSH Shell ์ด๊ธฐ
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"
}

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
{
"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'๋ฅผ ๋ชจ๋ ๋์ผ์ธ์ผ๋ก ํ๋จ!
request.json ๋ด์ฉ ์์ (์์ฒญ ๋ด์ฉ ๋ณ๊ฒฝ)
{
"document":{
"type":"PLAIN_TEXT",
"content":"Harry Potter is the best book. I think everyone should read it."
},
"encodingType": "UTF8"
}

curl "https://language.googleapis.com/v1/documents:analyzeSentiment?key=${API_KEY}" \
-s -X POST -H "Content-Type: application/json" --data-binary @request.json
๋ถ์ ๊ฒฐ๊ณผ
score: ํํ์ด ๊ธ์ ์ ์ด๊ฑฐ๋, ๋ถ์ ์ ์ธ ์ ๋๋ฅผ ๋ํ๋ธ๋ค.
- -1.0 (๋งค์ฐ ๋ถ์ ์ ) <----> 0.0 (์ค๋ฆฝ) <----> 1.0 (๋งค์ฐ ๊ธ์ ์ )
magnitude: 0~๋ฌดํ๋์ ์ด๋ฅด๋ ์์น์ด๋ฉฐ ๊ธ์ /๋ถ์ ์ ์ธ ์ ๋์ ์๊ด์์ด ์ง์ ์ ํํ๋ ๊ฐ์ ์ ๋น์ค์ ๋ํ๋ธ๋ค. (๊ธด ํ ์คํธ ๋ธ๋ก๊ณผ ์ค์๋๊ฐ ๋์ ๋ฌธ์ฅ์ผ์๋ก ํฌ๊ธฐ ๊ฐ์ด ๋ ๋๋ค.)
request.json ๋ด์ฉ ์์ (์์ฒญ ๋ด์ฉ ๋ณ๊ฒฝ)
{
"document":{
"type":"PLAIN_TEXT",
"content":"I liked the sushi but the service was terrible."
},
"encodingType": "UTF8"
}

curl "https://language.googleapis.com/v1/documents:analyzeEntitySentiment?key=${API_KEY}" \
-s -X POST -H "Content-Type: application/json" --data-binary @request.json
{
"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์ โก๏ธ ๊ธ์ /๋ถ์ ๋ํ๋ธ๋ค.
request.json ๋ด์ฉ ์์ (์์ฒญ ๋ด์ฉ ๋ณ๊ฒฝ)
{
"document":{
"type":"PLAIN_TEXT",
"content": "Joanne Rowling is a British novelist, screenwriter and film producer."
},
"encodingType": "UTF8"
}

curl "https://language.googleapis.com/v1/documents:analyzeSyntax?key=${API_KEY}" \
-s -X POST -H "Content-Type: application/json" --data-binary @request.json
{
"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: ๋จ์ด์ ๊ธฐ๋ณธํ
request.json ๋ด์ฉ ์์ (์์ฒญ ๋ด์ฉ ๋ณ๊ฒฝ)
{
"document":{
"type":"PLAIN_TEXT",
"content":"ๆฅๆฌใฎใฐใผใฐใซใฎใชใใฃในใฏใๆฑไบฌใฎๅ
ญๆฌๆจใใซใบใซใใใพใ"
}
}

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