RubyでCloudNaturalLangageを使ってみた。

CloudNaturalLangageをRubyで動かしてみる!

その後Railsでこれを使ったWebアプリを作りたい。

 

Cloud Natural Language  |  Cloud Natural Language API  |  Google Cloud

こちらを使ってテキストの中から単語の抽出をしてみたり、

発言のネガポジ度測定をしたりできます。

 

1.APIkeyの取得

Google Natural Language API を活用した誰でも出来る!自然言語処理! - WonderPlanet Tech Blog

こちらを参考にどうぞ!!

APIkeyは後ほどソースコードで使用しますのでしっかりと記録しておきましょう。

 

2.公式からcurlコマンドを取得

Natural Language API でエンティティと感情を分析する

上記で欲しいものを選択する。

今回の私の場合はentitiesの単語を抽出したいので「10.多言語の自然言語処理」を選択します。

ページ内に上記のcurlコマンドがあるのでそちらを参考にしましょう。

f:id:Railsprogram:20181217211002p:plain

 

3.curlコマンドからrubyコードへ変換処理

下記のサイトでrubyのコードに変換します。

curl-to-ruby: Convert curl commands to ruby's net/http

f:id:Railsprogram:20181217211138p:plain

こちらを参考にサンプルコードを生成していきましょう。

 

4.コード作成

require 'net/http'
require 'uri'
require 'json'

uri = URI.parse("https://language.googleapis.com/v1beta1/documents:analyzeEntities?key=取得したAPIkeyをこちらへ")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request.body = ""
request.body = '{
"document":{
"type":"PLAIN_TEXT",
"content": "Google, headquartered in Mountain View, unveiled the new Android phone at the Consumer Electronic Show. Sundar Pichai said in his keynote that users love their new Android phones." #こちらに検査したい文字列を挿入
}
}'

req_options = {
use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
json = JSON.parse(response.body)
p json

p json['entities'][0]['name']#=> "Google"
p json['entities'][1]['name']#=> "users"
p json['entities'][2]['name']#=> "phone"
p json['entities'][3]['name']#=> "Android"

 

5.出力結果

% ruby test.rb [20:55:53]
{"entities"=>[{"name"=>"Google", "type"=>"ORGANIZATION", "metadata"=>{"mid"=>"/m/045c7b", "wikipedia_url"=>"https://en.wikipedia.org/wiki/Google"}, "salience"=>0.25572062, "mentions"=>[{"text"=>{"content"=>"Google", "beginOffset"=>-1}, "type"=>"PROPER"}]}, {"name"=>"users", "type"=>"PERSON", "metadata"=>{}, "salience"=>0.1527633, "mentions"=>[{"text"=>{"content"=>"users", "beginOffset"=>-1}, "type"=>"COMMON"}]}, {"name"=>"phone", "type"=>"CONSUMER_GOOD", "metadata"=>{}, "salience"=>0.13119894, "mentions"=>[{"text"=>{"content"=>"phone", "beginOffset"=>-1}, "type"=>"COMMON"}]}, {"name"=>"Android", "type"=>"CONSUMER_GOOD", "metadata"=>{"mid"=>"/m/02wxtgw", "wikipedia_url"=>"https://en.wikipedia.org/wiki/Android_(operating_system)"}, "salience"=>0.122452624, "mentions"=>[{"text"=>{"content"=>"Android", "beginOffset"=>-1}, "type"=>"PROPER"}, {"text"=>{"content"=>"Android", "beginOffset"=>-1}, "type"=>"PROPER"}]}, {"name"=>"Sundar Pichai", "type"=>"PERSON", "metadata"=>{"mid"=>"/m/09gds74", "wikipedia_url"=>"https://en.wikipedia.org/wiki/Sundar_Pichai"}, "salience"=>0.11414111, "mentions"=>[{"text"=>{"content"=>"Sundar Pichai", "beginOffset"=>-1}, "type"=>"PROPER"}]}, {"name"=>"Mountain View", "type"=>"LOCATION", "metadata"=>{"wikipedia_url"=>"https://en.wikipedia.org/wiki/Mountain_View,_California", "mid"=>"/m/0r6c4"}, "salience"=>0.101959646, "mentions"=>[{"text"=>{"content"=>"Mountain View", "beginOffset"=>-1}, "type"=>"PROPER"}]}, {"name"=>"Consumer Electronic Show", "type"=>"EVENT", "metadata"=>{"mid"=>"/m/01p15w", "wikipedia_url"=>"https://en.wikipedia.org/wiki/Consumer_Electronics_Show"}, "salience"=>0.070343755, "mentions"=>[{"text"=>{"content"=>"Consumer Electronic Show", "beginOffset"=>-1}, "type"=>"PROPER"}]}, {"name"=>"phones", "type"=>"CONSUMER_GOOD", "metadata"=>{}, "salience"=>0.03383166, "mentions"=>[{"text"=>{"content"=>"phones", "beginOffset"=>-1}, "type"=>"COMMON"}]}, {"name"=>"keynote", "type"=>"OTHER", "metadata"=>{}, "salience"=>0.01758835, "mentions"=>[{"text"=>{"content"=>"keynote", "beginOffset"=>-1}, "type"=>"COMMON"}]}], "language"=>"en"}
"Google"
"users"
"phone"
"Android"