3
# Yahoo!JAPANデベロッパーネットワークの校正支援APIを利用して、
4
# 日本語文の校正作業を支援します。文字の入力ミスや言葉の誤用がないか、
5
# わかりにくい表記や不適切な表現が使われていないかなどをチェックします。
7
# Copyright (c) 2010, hb <http://www.smallstyle.com/>
8
# You can redistribute it and/or modify it under GPL.
12
# @options['yahoo_kousei.appid'] : アプリケーションID(必須)
13
# @options['yahoo_kousei.filter_group'] :
14
# 指摘グループの番号をコンマで区切って指定します。
15
# @options['yahoo_kousei.no_filter'] :
16
# filter_groupで指定した指摘グループから除外する指摘を指定します。
18
# 設定値は http://developer.yahoo.co.jp/webapi/jlp/kousei/v1/kousei.html を参照
22
require 'rexml/document'
26
def kousei_api( sentence )
27
appid = @conf['yahoo_kousei.appid']
29
query = "appid=#{appid}&sentence=#{URI.encode( sentence.gsub( /\n/, '' ) )}"
30
query << "&filter_group=" + @conf['yahoo_kousei.filter_group'] if @conf['yahoo_kousei.filter_group']
31
query << "&no_filter=" + @conf['yahoo_kousei.no_filter'] if @conf['yahoo_kousei.no_filter']
33
px_host, px_port = (@conf['proxy'] || '').split( /:/ )
34
px_port = 80 if px_host and !px_port
37
Net::HTTP::Proxy( px_host, px_port ).start( 'jlp.yahooapis.jp' ) do |http|
38
xml = http.post( '/KouseiService/V1/kousei', query ).body
43
def kousei_result( result_set )
47
<tr><th>対象表記</th><th>候補文字</th><th>詳細情報</th></tr>
51
doc = REXML::Document::new( result_set )
52
REXML::XPath.each( doc, "//Result" ) do |result|
53
ranges << [REXML::XPath.match( result, "StartPos/text()").to_s, REXML::XPath.match( result, "Length/text()" ).to_s ]
54
surface = REXML::XPath.match( result, "Surface/text()" ).to_s
55
shiteki = REXML::XPath.match( result, "ShitekiWord/text()" ).to_s
56
info = REXML::XPath.match( result, "ShitekiInfo/text()" ).to_s
57
html << %Q|<tr class="plugin_yahoo_search_result_raw"><td>#{surface}</td><td>#{shiteki}</td><td>#{info}</td></tr>|
62
ranges.map!{|r| "[" + r.join( "," ) + "]" }
65
<script type="text/javascript">
68
#{ranges.join( ", " )}
70
$( ".plugin_yahoo_search_result_raw" ).each( function( index ) {
71
$(this).click( function() {
72
var o = $( "textarea[name='body']" ).get( 0 );
74
if ( jQuery.browser.msie ) {
75
var range = document.selection.createRange();
77
range.moveStart( "character", ranges[index][0] );
78
range.moveEnd( "character", ranges[index][1] );
81
o.setSelectionRange( ranges[index][0] , ranges[index][0] + ranges[index][1] );
92
if @mode == 'preview' && @conf['yahoo_kousei.appid'] then
93
xml = kousei_api( @cgi.params['body'][0] )
95
<div id="plugin_yahoo_kousei" class="section">
96
#{kousei_result( xml )}
97
<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet -->
98
<span style="margin:15px 15px 15px 15px"><a href="http://developer.yahoo.co.jp/about">Webサービス by Yahoo! JAPAN</a></span>
99
<!-- End Yahoo! JAPAN Web Services Attribution Snippet -->