COTOHA でキーワードの抽出 (Ruby)
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
*** 終了 ***
Author And Source
この問題について(COTOHA でキーワードの抽出 (Ruby)), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/f081ffd94b434b30fcd5著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .