1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

COTOHA でキーワードの抽出 (Ruby)

Posted at

COTOHA API Portal の使用例です。

key_word.rb
# ! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#	key_word.rb
#
#					Feb/27/2020
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
#
load 'get_config.rb'
load 'get_token.rb'
# ---------------------------------------------------------------------
def read_string_proc (file_in)
#
	str_out=""
	File.open(file_in,"r:UTF-8") do |ff|
		while line=ff.gets
			str_out += line
		end
	end
#
	return	str_out
end
# ---------------------------------------------------------------------
STDERR.puts	"*** 開始 ***"
#
file_in = ARGV[0]
puts file_in
doc = read_string_proc (file_in)
#
config = get_config_proc()
#
access_token = get_token_proc(config)
#	
#
headers={
    "Content-Type": "application/json",
    "Authorization": "Bearer " + access_token
    }
#
data = {
    "document": doc,
    "type": "default"
    }

str_json = JSON.generate(data)
#
url = config['url_base'] + "v1/keyword"
#
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|
		str_out = unit['form'] + "\t" + unit['score'].to_s
		puts str_out
	}
#
STDERR.puts	"*** 終了 ***"
# ---------------------------------------------------------------------

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

実行結果

$ ./key_word.rb akai_rousoku.txt
*** 開始 ***
akai_rousoku.txt
200
猿	135.52966
蝋燭	83.9601
花火	78.08584
亀	43.078
火	42.81965
*** 終了 ***
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?