yahoo-japanese-analysis 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ credential.yaml
@@ -0,0 +1,32 @@
1
+ #Yahoo Japanese Analysis Library
2
+
3
+ This is Yahoo! Japan Text Analysis Wrapper for Ruby.
4
+ See below to get more info.
5
+ http://developer.yahoo.co.jp/webapi/jlp/
6
+
7
+ #Installation
8
+
9
+ ##Yahoo! Japan Developers' Network Registration
10
+
11
+ You must register at http://developer.yahoo.co.jp/ .
12
+ And you can get appid to access APIs
13
+
14
+ ##Installtion gem
15
+
16
+ #Example
17
+
18
+ see https://gist.github.com/2605666
19
+
20
+ #LICENSE
21
+
22
+ ## The MIT License
23
+
24
+ Copyright (c) 2012 rhysd
25
+
26
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
27
+
28
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
29
+
30
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+
32
+
@@ -0,0 +1,33 @@
1
+ require 'yahoo-japanese-analysis/client'
2
+ require 'yahoo-japanese-analysis/version'
3
+
4
+
5
+ #
6
+ # YahooJA module. You can use YahooJA class methods to use
7
+ # YahooJA::Client instance methods without instance.
8
+ # If you want to use multi-account states, you should use
9
+ # YahooJA::Client instance to use multi accounts.
10
+ #
11
+ module YahooJA
12
+ class << self
13
+
14
+ # override to delegate
15
+ def method_missing name,*args,&block
16
+ @client ||= YahooJA::Client.new
17
+ return super unless @client.respond_to? name
18
+ @client.send name,*args,&block
19
+ end
20
+
21
+ # override to delegate
22
+ def respond_to? name,include_private=false
23
+ @client ||= YahooJA::Client.new
24
+ @client.respond_to?(name,include_private) || super(name,include_private)
25
+ end
26
+
27
+ # reset client state
28
+ def reset
29
+ @client = nil
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ require 'yahoo-japanese-analysis/config'
2
+
3
+ module YahooJA
4
+
5
+ #
6
+ # Client to use Yahoo! Japan Text Analysis API.
7
+ #
8
+ class Client
9
+
10
+ include YahooJA::Config
11
+ attr_accessor *Config::CONFIG_KEYS
12
+
13
+ require 'yahoo-japanese-analysis/client/keyphrase'
14
+ include YahooJA::Client::Keyphrase
15
+
16
+ require 'yahoo-japanese-analysis/client/morpheme_analysis'
17
+ include YahooJA::Client::MorphemeAnalysis
18
+
19
+ require 'yahoo-japanese-analysis/client/kanji_conv'
20
+ include YahooJA::Client::KanjiConv
21
+
22
+ require 'yahoo-japanese-analysis/client/furigana'
23
+ include YahooJA::Client::Furigana
24
+
25
+ require 'yahoo-japanese-analysis/client/kousei_support'
26
+ include YahooJA::Client::KouseiSupport
27
+
28
+ require 'yahoo-japanese-analysis/client/kakari_uke'
29
+ include YahooJA::Client::KakariUke
30
+
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ module YahooJA
2
+ class Client
3
+ module Furigana
4
+
5
+ require 'yahoo-japanese-analysis/service_base'
6
+ include YahooJA::ServiceBase
7
+
8
+ #
9
+ # "ルビ振り" API
10
+ #
11
+ # @args
12
+ # @text text to analyze
13
+ # @opts other options.
14
+ # @return Hash converted from XML format. more info is below.
15
+ # @see http://developer.yahoo.co.jp/webapi/jlp/furigana/v1/furigana.html
16
+ #
17
+ def furigana text,opts={}
18
+ use_service 'http://jlp.yahooapis.jp/FuriganaService/V1/furigana',self.app_key,text,opts
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module YahooJA
2
+ class Client
3
+ module KakariUke
4
+
5
+ require 'yahoo-japanese-analysis/service_base'
6
+ include YahooJA::ServiceBase
7
+
8
+ #
9
+ # "日本語係り受け解析" API
10
+ #
11
+ # @args
12
+ # @text text to analyze
13
+ # @return Hash converted from XML format. more info is below.
14
+ # @see http://developer.yahoo.co.jp/webapi/jlp/da/v1/parse.html
15
+ # @supplementation kakari-uke service doesn't have any option
16
+ #
17
+ def kakari_uke text
18
+ use_service 'http://jlp.yahooapis.jp/DAService/V1/parse',self.app_key,text
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module YahooJA
2
+ class Client
3
+ module KanjiConv
4
+
5
+ require 'yahoo-japanese-analysis/service_base'
6
+ include YahooJA::ServiceBase
7
+
8
+ #
9
+ # "かな漢字変換" API
10
+ #
11
+ # @args
12
+ # @text text to analyze
13
+ # @opts other options.
14
+ # @return Hash converted from XML format. more info is below.
15
+ # @see http://developer.yahoo.co.jp/webapi/jlp/jim/v1/conversion.html
16
+ #
17
+ def kanji_conv text,opts={}
18
+ use_service 'http://jlp.yahooapis.jp/JIMService/V1/conversion',self.app_key,text,opts
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ require 'yahoo-japanese-analysis/request'
2
+ require 'rexml/document'
3
+
4
+ module YahooJA
5
+ class Client
6
+ module Keyphrase
7
+ include YahooJA::Request
8
+
9
+ #
10
+ # "特徴語抽出" API
11
+ #
12
+ # @args
13
+ # @text text to analyze
14
+ # @opts other options.
15
+ # @return Hash( key: word, value: score )
16
+ # @see http://developer.yahoo.co.jp/webapi/jlp/keyphrase/v1/extract.html
17
+ #
18
+ def keyphrase text,opts={}
19
+ raise "appid is needed" unless self.app_key
20
+ params = {:sentence => text}.merge(opts) do |k,v1,v2|
21
+ raise "you shouldn't set #{k} as opts. it is already set."
22
+ end
23
+ res = request 'http://jlp.yahooapis.jp/KeyphraseService/V1/extract',self.app_key,params
24
+ keyphrases = {}
25
+ doc = REXML::Document.new(res).elements['ResultSet']
26
+ doc.elements.each('Result') do |result|
27
+ phrase = result.elements['Keyphrase'].get_text.to_s
28
+ score = result.elements['Score'].get_text.to_s.to_i
29
+ keyphrases[phrase] = score
30
+ end
31
+ keyphrases
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,23 @@
1
+ module YahooJA
2
+ class Client
3
+ module KouseiSupport
4
+
5
+ require 'yahoo-japanese-analysis/service_base'
6
+ include YahooJA::ServiceBase
7
+
8
+ #
9
+ # "校正支援" API
10
+ #
11
+ # @args
12
+ # @text text to analyze
13
+ # @opts other options.
14
+ # @return Hash converted from XML format. more info is below.
15
+ # @see http://developer.yahoo.co.jp/webapi/jlp/kousei/v1/kousei.html
16
+ #
17
+ def kousei_support text,opts={}
18
+ use_service 'http://jlp.yahooapis.jp/KouseiService/V1/kousei',self.app_key,text,opts
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module YahooJA
2
+ class Client
3
+ module MorphemeAnalysis
4
+
5
+ require 'yahoo-japanese-analysis/service_base'
6
+ include YahooJA::ServiceBase
7
+
8
+ #
9
+ # "日本語形態素解析" API (Japanese Morpheme Analysis)
10
+ #
11
+ # @args
12
+ # @text text to analyze
13
+ # @opts other options.
14
+ # @return Hash converted from XML format. more info is below.
15
+ # @see http://developer.yahoo.co.jp/webapi/jlp/ma/v1/parse.html
16
+ #
17
+ def morpheme_analysis text,opts={}
18
+ use_service 'http://jlp.yahooapis.jp/MAService/V1/parse',self.app_key,text,opts
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ module YahooJA
2
+
3
+ #
4
+ # Config module.
5
+ # You should set appid before using APIs.
6
+ #
7
+ module Config
8
+ DEFAULT_APP_KEY = nil
9
+
10
+ CONFIG_KEYS = [ :app_key ]
11
+
12
+ attr_accessor *CONFIG_KEYS
13
+
14
+ #
15
+ # set appid
16
+ #
17
+ # YahooJA.configure do |config|
18
+ # config.app_key = 'hogehoge'
19
+ # end
20
+ #
21
+ # c = YahooJA::Client.new
22
+ # c.configure do |config|
23
+ # config.app_key = 'hogehoge'
24
+ # end
25
+ #
26
+ def configure
27
+ yield self
28
+ self
29
+ end
30
+
31
+ def options
32
+ opts = {}
33
+ CONFIG_KEYS.each do |k|
34
+ opts[k] = send k
35
+ end
36
+ opts
37
+ end
38
+
39
+ def self.extended base
40
+ base.reset
41
+ end
42
+
43
+ # reset config setting
44
+ def reset
45
+ self.app_key = DEFAULT_APP_KEY
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,18 @@
1
+ module YahooJA
2
+ module Request
3
+
4
+ require 'uri'
5
+ require 'net/http'
6
+
7
+ def request path,app_id,params={}
8
+ uri = URI.parse path
9
+ queries = 'appid='+app_id+'&'+params.map{|k,v| k.to_s+'='+v.to_s}.join('&')
10
+ req = Net::HTTP::Post.new uri.path
11
+ res = Net::HTTP.start(uri.host,uri.port) do |http|
12
+ http.request req, queries
13
+ end
14
+ res.body
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,47 @@
1
+ module YahooJA
2
+ module ServiceBase
3
+
4
+ require 'rexml/document'
5
+
6
+ require 'yahoo-japanese-analysis/request'
7
+ include YahooJA::Request
8
+
9
+ def use_service url,appid,text,opts={}
10
+ raise "appid is needed" unless appid
11
+ params = {:sentence => text}.merge(opts) do |k,v1,v2|
12
+ raise "you shouldn't set #{k} as opts. it is already set."
13
+ end
14
+ res = request url, appid, params
15
+
16
+ doc = REXML::Document.new(res)
17
+ xml_to_hash(REXML::Document.new(res))[:ResultSet]
18
+ end
19
+
20
+ private
21
+
22
+ def xml_to_hash rexml
23
+ xml_elem_to_hash rexml.root
24
+ end
25
+
26
+ def xml_elem_to_hash elem
27
+ value = if elem.has_elements?
28
+ children = {}
29
+ elem.each_element do |e|
30
+ # children << xml_elem_to_hash(e)
31
+ children.merge!(xml_elem_to_hash(e)) do |k,v1,v2|
32
+ if v1.class == Array
33
+ v1 << v2
34
+ else
35
+ [v1,v2]
36
+ end
37
+ end
38
+ end
39
+ children
40
+ else
41
+ elem.text
42
+ end
43
+ { elem.name.to_sym => value }
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ module YahooJA
2
+ class Version
3
+ class << self
4
+
5
+ def major
6
+ 0
7
+ end
8
+
9
+ def minor
10
+ 1
11
+ end
12
+
13
+ def patch
14
+ 1
15
+ end
16
+
17
+ def pre
18
+ nil
19
+ end
20
+
21
+ def to_s
22
+ [major, minor, patch, pre].compact!.join '.'
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,2 @@
1
+ path = Dir.pwd+'/../lib'
2
+ $LOAD_PATH.push path unless $LOAD_PATH.include? path
@@ -0,0 +1,140 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require './pre_test.rb'
5
+ require 'yahoo-japanese-analysis'
6
+ require 'test/unit'
7
+ require 'yaml'
8
+
9
+ class TestBase < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @appid = YAML.load(File.open('credential.yaml').read)['my_appid']
13
+ @client = YahooJA::Client.new
14
+ end
15
+
16
+ def test_base_client
17
+ assert_not_nil @client
18
+ assert_instance_of YahooJA::Client, @client
19
+ assert_respond_to @client,:configure
20
+ assert_respond_to @client,:options
21
+ assert_respond_to @client,:reset
22
+ end
23
+
24
+ def test_configure
25
+ @client.configure{|config| config.app_key = @appid}
26
+ assert_equal @client.app_key,@appid
27
+ end
28
+
29
+ def test_version
30
+ assert_equal YahooJA::Version.major.class,Fixnum
31
+ assert_equal YahooJA::Version.minor.class,Fixnum
32
+ assert_equal YahooJA::Version.patch.class,Fixnum
33
+ assert( !YahooJA::Version.pre || (YahooJA::Version.pre.class == String) )
34
+ assert YahooJA::Version.to_s =~ /^\d+?\.\d+?\.\d+?.*?$/
35
+ end
36
+
37
+ # test YahooJA class methods for lightweight-use
38
+ def test_gateway
39
+ [ :configure, :options, :reset,
40
+ :keyphrase, :morpheme_analysis, :furigana,
41
+ :kanji_conv, :kousei_support, :kakari_uke
42
+ ].each do |m|
43
+ assert YahooJA.respond_to?(m),"#{m} is missing."
44
+ end
45
+ end
46
+
47
+ require 'yahoo-japanese-analysis/service_base'
48
+ include YahooJA::ServiceBase
49
+
50
+ # test xml_to_hash
51
+ def test_service_base
52
+
53
+ xml_pairs = {}
54
+ xml_pairs['<result><info><title>Title1</title><data>1234</data></info><info><title>Title2</title><data>5678</data></info></result>'] = {:result=>{:info=>[{:title=>"Title1", :data=>"1234"}, {:title=>"Title2", :data=>"5678"}]}}
55
+ xml_pairs['<hoge></hoge>'] = {:hoge=>nil}
56
+ xml_pairs['<hoge><foo><bar>piyo</bar></foo></hoge>'] = {:hoge=>{:foo=>{:bar=>"piyo"}}}
57
+
58
+ # work around to test private method in block.
59
+ xml_to_hash_method = method(:xml_to_hash).to_proc
60
+ xml_pairs.each do |xml,expected|
61
+ rexml = REXML::Document.new xml
62
+ result = xml_to_hash_method.call rexml
63
+ assert_equal result,expected
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ class TestFeatures < Test::Unit::TestCase
70
+
71
+ def setup
72
+ @appid = YAML.load(File.open('credential.yaml').read)['my_appid']
73
+ @client = YahooJA::Client.new
74
+ end
75
+
76
+ def test_keyphrase
77
+ @client.configure{|config| config.app_key = @appid}
78
+ assert_respond_to @client,:keyphrase
79
+ text = "RubyやC++も良いけれど,そろそろHaskellを習得したいなぁ."
80
+ result = nil
81
+ assert_nothing_raised{ result = @client.keyphrase text }
82
+ assert_not_nil result
83
+ assert_not_nil result['Ruby']
84
+ assert_not_nil result['Haskell']
85
+ assert_not_nil result['C']
86
+ end
87
+
88
+ def test_morpheme_analysis
89
+ @client.configure{|config| config.app_key = @appid}
90
+ assert_respond_to @client, :morpheme_analysis
91
+ text = "庭には二羽ニワトリがいる。"
92
+ result = nil
93
+ assert_nothing_raised{ result = @client.morpheme_analysis text,{:results => "ma,uniq"} }
94
+ assert_not_nil result
95
+ assert_not_nil result[:ma_result][:total_count]
96
+ assert_not_nil result[:ma_result][:word_list][:word].first[:surface]
97
+ assert_not_nil result[:uniq_result][:total_count]
98
+ assert_not_nil result[:uniq_result][:word_list][:word].first[:surface]
99
+ end
100
+
101
+ def test_furigana
102
+ @client.configure{|config| config.app_key = @appid}
103
+ assert_respond_to @client, :morpheme_analysis
104
+ text = "漢字かな交じり文にふりがなを振ること。"
105
+ result = nil
106
+ assert_nothing_raised{ result = @client.furigana text }
107
+ assert_not_nil result
108
+ assert_not_nil result[:Result][:WordList][:Word].first[:Surface]
109
+ end
110
+
111
+ def test_kanji_conv
112
+ @client.configure{|config| config.app_key = @appid}
113
+ assert_respond_to @client, :kanji_conv
114
+ text = "きょうはよいてんきです。"
115
+ result = nil
116
+ assert_nothing_raised{ result = @client.kanji_conv text }
117
+ assert_not_nil result
118
+ assert_not_nil result[:Result][:SegmentList][:Segment].first[:CandidateList]
119
+ end
120
+
121
+ def test_kousei_support
122
+ @client.configure{|config| config.app_key = @appid}
123
+ assert_respond_to @client, :kousei_support
124
+ text = "遙か彼方に小形飛行機が見える。"
125
+ result = nil
126
+ assert_nothing_raised{ result = @client.kousei_support text }
127
+ assert_not_nil result
128
+ assert_not_nil result[:Result].first[:ShitekiInfo]
129
+ end
130
+
131
+ def test_kakari_uke
132
+ @client.configure{|config| config.app_key = @appid}
133
+ assert_respond_to @client, :kakari_uke
134
+ text = "うちの庭には二羽鶏がいます。"
135
+ result = nil
136
+ assert_nothing_raised{ result = @client.kakari_uke text }
137
+ assert_not_nil result
138
+ assert_not_nil result[:Result][:ChunkList][:Chunk].first[:MorphemList][:Morphem].first[:Feature]
139
+ end
140
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path '../lib/yahoo-japanese-analysis/version',__FILE__
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'yahoo-japanese-analysis'
5
+ s.version = YahooJA::Version.to_s
6
+ s.date = '2012-05-06'
7
+ s.summary = "Japanese Analysis API Wrapper."
8
+ s.description = "Yahoo! Japan Text Analysis API Wrapper."
9
+ s.authors = ["rhysd"]
10
+ s.email = 'lin90162@yahoo.co.jp'
11
+ s.require_paths = ['lib']
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = ['test/test_all.rb']
14
+ s.homepage = 'https://github.com/rhysd/yahoo-japanese-analysis'
15
+ s.license = 'MIT'
16
+ s.required_ruby_version = '>= 1.9.2'
17
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yahoo-japanese-analysis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - rhysd
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Yahoo! Japan Text Analysis API Wrapper.
15
+ email: lin90162@yahoo.co.jp
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - README.md
22
+ - lib/yahoo-japanese-analysis.rb
23
+ - lib/yahoo-japanese-analysis/client.rb
24
+ - lib/yahoo-japanese-analysis/client/furigana.rb
25
+ - lib/yahoo-japanese-analysis/client/kakari_uke.rb
26
+ - lib/yahoo-japanese-analysis/client/kanji_conv.rb
27
+ - lib/yahoo-japanese-analysis/client/keyphrase.rb
28
+ - lib/yahoo-japanese-analysis/client/kousei_support.rb
29
+ - lib/yahoo-japanese-analysis/client/morpheme_analysis.rb
30
+ - lib/yahoo-japanese-analysis/config.rb
31
+ - lib/yahoo-japanese-analysis/request.rb
32
+ - lib/yahoo-japanese-analysis/service_base.rb
33
+ - lib/yahoo-japanese-analysis/version.rb
34
+ - test/pre_test.rb
35
+ - test/test_all.rb
36
+ - yahoo-japanese-analysis.gemspec
37
+ homepage: https://github.com/rhysd/yahoo-japanese-analysis
38
+ licenses:
39
+ - MIT
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: 1.9.2
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.23
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Japanese Analysis API Wrapper.
62
+ test_files:
63
+ - test/test_all.rb