COTOHA で固有名詞の抽出 (Ruby)


COTOHA API Portal の使用例です。

proper_noun.rb
#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#   proper_noun.rb
#
#                   Feb/27/2020
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
#
load 'get_config.rb'
load 'get_token.rb'
# ---------------------------------------------------------------------
STDERR.puts "*** 開始 ***"
#
config = get_config_proc()
#
access_token = get_token_proc(config)
#   
sentence = "特急はくたかで富山に向かいます。それから、金沢に行って、兼六園に行きます。"
#
headers={
    "Content-Type": "application/json",
    "Authorization": "Bearer " + access_token
    }
#
data = {
    "sentence": sentence,
    "type": "default"
    }

str_json = JSON.generate(data)
#
url = config['url_base'] + "v1/ne"
#
con = Faraday.new 
res = con.post do |req|
    req.url url
    req.headers = headers
    req.body = str_json
    end
#
    puts res.status
    dict_aa=JSON.parse(res.body)
#
    dict_aa['result'].each {|unit|
        puts unit['form']
    }
#
STDERR.puts "*** 終了 ***"
# ---------------------------------------------------------------------

get_config.rb get_token.rb はこちら
COTOHA API で構文解析 (Ruby)

実行結果

$ ./proper_noun.rb
*** 開始 ***
200
富山
金沢
兼六園
*** 終了 ***