Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚚 (plugin/yahoo_kousei.rb): Yahooの校正支援のAPI V2対応 #247

Merged
merged 2 commits into from
May 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions plugin/yahoo_kousei.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,41 @@
# わかりにくい表記や不適切な表現が使われていないかなどをチェックします。
#
# Copyright (c) 2010, hb <http://www.smallstyle.com/>
# Copyright (c) 2023, Takuya Ono <takuya-o@users.osdn.me>
# You can redistribute it and/or modify it under GPL.
#
# 設定:
#
# @options['yahoo_kousei.appid'] : アプリケーションID(必須)
# @options['yahoo_kousei.filter_group'] :
# 指摘グループの番号をコンマで区切って指定します。
# @options['yahoo_kousei.no_filter'] :
# filter_groupで指定した指摘グループから除外する指摘を指定します。
#
# 設定値は http://developer.yahoo.co.jp/webapi/jlp/kousei/v1/kousei.html を参照
# 設定値は https://developer.yahoo.co.jp/webapi/jlp/kousei/v2/kousei.html を参照
#

require 'timeout'
require 'rexml/document'
require 'json'
require 'net/http'
require 'net/https'
Net::HTTP.version_1_2

def kousei_api( sentence )
appid = @conf['yahoo_kousei.appid']
headers = {'Content-Type' => 'application/json',
'User-Agent' => "Yahoo AppID: #{appid}" }

query = "appid=#{appid}&sentence=#{CGI.escape( sentence.gsub( /\n/, '' ) )}"
query << "&filter_group=" + @conf['yahoo_kousei.filter_group'] if @conf['yahoo_kousei.filter_group']
query << "&no_filter=" + @conf['yahoo_kousei.no_filter'] if @conf['yahoo_kousei.no_filter']
query = { "id" => 1234,
"jsonrpc" => "2.0",
"method" => "jlp.kouseiservice.kousei",
"params" => {
"q" => sentence
}
}

px_host, px_port = (@conf['proxy'] || '').split( /:/ )
px_port = 80 if px_host and !px_port

xml = ''
Net::HTTP::Proxy( px_host, px_port ).start( 'jlp.yahooapis.jp' ) do |http|
xml = http.post( '/KouseiService/V1/kousei', query ).body
Net::HTTP::Proxy( px_host, px_port ).start( 'jlp.yahooapis.jp',443, use_ssl: true , verify_mode: OpenSSL::SSL::VERIFY_PEER ) do |http|
takuya-o marked this conversation as resolved.
Show resolved Hide resolved
xml = http.post( '/KouseiService/V2/kousei', query.to_json, headers ).body
end
xml
end
Expand All @@ -45,15 +49,24 @@ def kousei_result( result_set )
<h3>文章校正結果</h3>
HTML

doc = REXML::Document::new( result_set )
results = REXML::XPath.match( doc, "//Result" )
if results.empty?
html << "<p>指摘項目は見つかりませんでした。</p>"
doc = JSON.parse( result_set )
errormsg = ""
if doc["Error"] != nil
errormsg = doc["Error"]["Message"]
doc = { "result" => { "suggestions" => [] } }
end
results = doc['result']['suggestions']
if results.length == 0
if errormsg == ""
html << "<p>指摘項目は見つかりませんでした。</p>"
else
html << "<p>Error: #{errormsg}</p>"
end
else
html << '<table>'
html << '<tr><th>対象表記</th><th>候補文字</th><th>詳細情報</th><th>場所</th></tr>'
doc.elements.each( '//ResultSet/Result' ) do |r|
html << %Q|<tr class="plugin_yahoo_search_result_raw"><td>#{r.elements['Surface'].text}</td><td>#{r.elements['ShitekiWord'].text}</td><td>#{r.elements['ShitekiInfo'].text}</td><td>#{r.elements['StartPos'].text},#{r.elements['Length'].text}</td></tr>|
results.each do |r|
html << %Q|<tr class="plugin_yahoo_search_result_raw"><td>#{r['word']}</td><td>#{r['suggestion']}</td><td>#{r['rule']} #{r['note']}</td><td>#{r['offset']},#{r['length']}</td></tr>|
end
html << '</table>'
end
Expand Down
Loading