niku-yma4r 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  = yma4r
3
2
 
4
3
 
@@ -21,22 +20,25 @@
21
20
 
22
21
  == Synopsis
23
22
 
24
- require 'pp'
25
- require 'yma4r'
26
-
27
- ma = Yma4r.new(:appid => 'Your Yahoo Japan Application ID',
28
- :sentence => 'すもももももももものうち')
29
-
30
- result = ma.analyse
31
- pp result.word_list
32
- #[{:pos=>"名詞", :surface=>"すもも", :reading=>"すもも"},
33
- # {:pos=>"助詞", :surface=>"も", :reading=>"も"},
34
- # {:pos=>"名詞", :surface=>"もも", :reading=>"もも"},
35
- # {:pos=>"助詞", :surface=>"も", :reading=>"も"},
36
- # {:pos=>"名詞", :surface=>"もも", :reading=>"もも"},
37
- # {:pos=>"助詞", :surface=>"の", :reading=>"の"},
38
- # {:pos=>"名詞", :surface=>"うち", :reading=>"うち"}]
39
- #=> nil
23
+ require 'yma4r'
24
+ yma4r = Yma4r.new(
25
+ :appid => 'Your Yahoo Japan Application ID',
26
+ :sentence => 'すもももももももものうち',
27
+ :results => :ma,
28
+ :response => [:surface, :reading, :pos, :baseform]
29
+ )
30
+
31
+ yma4r.parse.ma_result.word_list.each do |e|
32
+ puts "surface:#{e.surface}, reading:#{e.reading}, pos:#{e.pos}, baseform:#{e.baseform}"
33
+ end
34
+
35
+ # >> surface:すもも, reading:すもも, pos:名詞, baseform:すもも
36
+ # >> surface:も, reading:も, pos:助詞, baseform:も
37
+ # >> surface:もも, reading:もも, pos:名詞, baseform:もも
38
+ # >> surface:も, reading:も, pos:助詞, baseform:も
39
+ # >> surface:もも, reading:もも, pos:名詞, baseform:もも
40
+ # >> surface:の, reading:の, pos:助詞, baseform:の
41
+ # >> surface:うち, reading:うち, pos:名詞, baseform:うち
40
42
 
41
43
  == SEE ALSO
42
44
 
data/lib/yma4r.rb CHANGED
@@ -6,78 +6,153 @@ class Yma4r
6
6
  include ClassX
7
7
  require 'net/http'
8
8
  require 'yma_parser'
9
-
9
+
10
10
  has :appid,
11
- :desc => 'アプリケーションID。'
12
-
11
+ :desc => 'アプリケーションID。',
12
+ :kind_of => String,
13
+ :writable => false
14
+
13
15
  has :sentence,
14
16
  :desc => '解析対象のテキストです。',
15
- :writable => true,
17
+ :kind_of => String,
16
18
  :optional => true
17
-
19
+
18
20
  has :results,
19
21
  :desc => '解析結果の種類をコンマで区切って指定します。',
20
- :default => proc { 'ma' },
21
- :validate => proc { |val| val.is_a? 'ma' || 'uniq' },
22
- :writable => true,
22
+ :kind_of => Array,
23
+ :coerce => {
24
+ String => proc {|val| [val.to_sym] },
25
+ Symbol => proc {|val| [val]}
26
+ },
27
+ :validate_each => proc { |val| (val == :uniq) || (val == :ma) },
23
28
  :optional => true
24
29
 
25
30
  has :response,
26
31
  :desc => 'ma_response, uniq_response のデフォルト設定です。word に返される形態素情報をコンマで区切って指定します。無指定の場合は "surface,reading,pos" になります。',
27
- :default => proc { 'surface,reading,pos' },
28
- :validate => proc { |val| val.is_a? 'surface' || 'reading' || 'pos' || 'baseform' || 'feature' },
32
+ :kind_of => Array,
33
+ :coerce => { Symbol => proc {|val| [val]} },
34
+ :validate_each => proc { |item|
35
+ (item == :surface) ||
36
+ (item == :reading) ||
37
+ (item == :pos) ||
38
+ (item == :baseform) ||
39
+ (item == :feature)
40
+ },
29
41
  :optional => true
30
42
 
31
43
  has :filter,
32
44
  :desc => 'ma_filter, uniq_filter のデフォルト設定です。解析結果として出力する品詞番号を "|" で区切って指定します。',
33
- :default => proc { '1|2|3|4|5|6|7|8|9|10|11|12|13' },
45
+ :kind_of => Array,
46
+ :coerce => {
47
+ Fixnum => proc {|val| [val]},
48
+ String => proc {|val| val.split('|').uniq.map{|v| v.to_i}}
49
+ },
50
+ :validate => proc {|item| (item.min >= 1) && (item.max <= 13) },
34
51
  :optional => true
35
-
52
+
36
53
  has :ma_response,
37
54
  :desc => 'ma_result 内の word に返される形態素情報をコンマで区切って指定します。無指定の場合 response の指定が用いられます。',
38
- :default => nil,
55
+ :kind_of => Array,
56
+ :coerce => { Symbol => proc {|val| [val]} },
57
+ :validate_each => proc { |item|
58
+ (item == :surface) ||
59
+ (item == :reading) ||
60
+ (item == :pos) ||
61
+ (item == :baseform) ||
62
+ (item == :feature)
63
+ },
39
64
  :optional => true
40
-
65
+
41
66
  has :ma_filter,
42
67
  :desc => 'ma_result 内に解析結果として出力する品詞番号を "|" で区切って指定します。無指定の場合 filter の指定が用いられます。',
43
- :default => nil,
68
+ :kind_of => Array,
69
+ :coerce => {
70
+ Fixnum => proc {|val| [val]},
71
+ String => proc {|val| val.split('|').uniq.map{|v| v.to_i}}
72
+ },
73
+ :validate => proc {|item| (item.min >= 1) && (item.max <= 13) },
44
74
  :optional => true
45
-
75
+
46
76
  has :uniq_response,
47
77
  :desc => 'uniq_result 内の word に返される形態素情報をコンマで区切って指定します。無指定の場合 response の指定が用いられます。',
48
- :default => nil,
78
+ :kind_of => Array,
79
+ :coerce => { Symbol => proc {|val| [val]} },
80
+ :validate_each => proc { |item|
81
+ (item == :surface) ||
82
+ (item == :reading) ||
83
+ (item == :pos) ||
84
+ (item == :baseform) ||
85
+ (item == :feature)
86
+ },
49
87
  :optional => true
50
-
88
+
51
89
  has :uniq_filter,
52
90
  :desc => 'uniq_result 内に解析結果として出力する品詞番号を "|" で区切って指定します。無指定の場合 filter の指定が用いられます。',
53
- :default => nil,
91
+ :kind_of => Array,
92
+ :coerce => {
93
+ Fixnum => proc {|val| [val]},
94
+ String => proc {|val| val.split('|').uniq.map{|v| v.to_i}}
95
+ },
96
+ :validate => proc {|item| (item.min >= 1) && (item.max <= 13) },
54
97
  :optional => true
55
-
98
+
56
99
  has :uniq_by_baseform,
57
100
  :desc => 'このパラメータが true ならば、基本形の同一性により、uniq_result の結果を求めます。',
58
- :default => proc { false },
101
+ :validate => proc{ |val| (val.is_a? TrueClass) || (val.is_a? FalseClass) },
59
102
  :optional => true
60
103
 
61
- def analyse
62
- YmaParser.new(request)
104
+ def query
105
+ hash.map { |key,val|
106
+ unless (val == nil || val == '')
107
+ "#{URI.encode(key)}=#{URI.encode(val)}"
108
+ end
109
+ }.compact.join('&')
63
110
  end
64
-
65
- def request
111
+
112
+ def parse
113
+ YmaParser::ResultSet.new(post)
114
+ end
115
+
116
+ private
117
+ def post
66
118
  host = 'jlp.yahooapis.jp'
67
119
  path = '/MAService/V1/parse'
68
- Net::HTTP.start(host){ |http| http.post(path, query_string) }.body
120
+ Net::HTTP.start(host){|http| http.post(path, query)}.body
69
121
  end
70
122
 
71
- def query_hash
123
+ def hash
124
+ if sentence == nil
125
+ raise ClassX::AttrRequiredError
126
+ end
127
+
72
128
  keys = ['appid', 'sentence', 'results', 'response', 'filter', 'ma_response', 'ma_filter', 'uniq_response', 'uniq_filter', 'uniq_by_baseform']
73
- vals = [appid, sentence, results , response, filter, ma_response, ma_filter, uniq_response, uniq_filter, uniq_by_baseform]
129
+ vals = [appid,
130
+ sentence,
131
+ (response_for_query results),
132
+ (response_for_query response),
133
+ (filter_for_query filter),
134
+ (response_for_query ma_response),
135
+ (filter_for_query ma_filter),
136
+ (response_for_query uniq_response),
137
+ (filter_for_query uniq_filter),
138
+ (uniq_by_baseform_for_query)]
74
139
  alist = keys.zip(vals)
75
140
  Hash[*alist.flatten]
76
141
  end
77
142
 
78
- def query_string
79
- query_hash.map{ |key,val| "#{URI.encode(key)}=#{URI.encode(val)}" if val }.compact.join('&')
143
+ def results_for_query val
144
+ val == nil ? nil : val.join(',')
145
+ end
146
+
147
+ def response_for_query val
148
+ val == nil ? nil : val.join(',')
149
+ end
150
+
151
+ def filter_for_query val
152
+ val == nil ? nil : val.join('|')
80
153
  end
81
154
 
82
- private :request, :query_hash, :query_string
155
+ def uniq_by_baseform_for_query
156
+ uniq_by_baseform == nil ? nil : uniq_by_baseform.to_s
157
+ end
83
158
  end
data/lib/yma_parser.rb CHANGED
@@ -1,15 +1,95 @@
1
- class YmaParser
2
- require 'rexml/document'
3
- attr_reader :xml, :total_count, :filtered_count, :word_list
4
- def initialize(xml)
5
- @xml = REXML::Document.new(xml)
6
- @total_count = @xml.elements['//total_count'].text.to_i
7
- @filtered_count = @xml.elements['//filtered_count'].text.to_i
8
- @word_list = @xml.elements['//word_list'].map do |word|
9
- { :surface=>word.elements['surface'].text,
10
- :reading=>word.elements['reading'].text,
11
- :pos=>word.elements['pos'].text
12
- }
1
+ module YmaParser
2
+ # See http://jlp.yahooapis.jp/MAService/V1/parseResponse.xsd
3
+ class ResultSet
4
+ require 'rexml/document'
5
+ attr_reader :xml
6
+
7
+ def initialize(xml)
8
+ @xml = REXML::Document.new(xml)
9
+ end
10
+
11
+ def ma_result
12
+ init_result(:ma_result)
13
+ end
14
+
15
+ def uniq_result
16
+ init_result(:uniq_result)
17
+ end
18
+
19
+ private
20
+ def init_result(type)
21
+ ResultType.new(type, @xml.elements["ResultSet/#{type}"])
22
+ end
23
+ end
24
+
25
+ class ResultType
26
+ require 'rexml/document'
27
+ attr_reader :type, :xml
28
+
29
+ def initialize(type, xml)
30
+ @type = type
31
+ @xml = xml
32
+ end
33
+
34
+ def total_count
35
+ count(:total_count)
36
+ end
37
+
38
+ def filtered_count
39
+ count(:filtered_count)
40
+ end
41
+
42
+ def word_list
43
+ @xml.elements["word_list"].map do |wordtype|
44
+ word_list = WordType.new(@type, wordtype)
45
+ end
46
+ end
47
+
48
+ private
49
+ def count(type)
50
+ @xml.elements["#{type}"].text.to_i
51
+ end
52
+ end
53
+
54
+ class WordType
55
+ require 'rexml/document'
56
+ attr_reader :xml
57
+
58
+ def initialize(type, xml)
59
+ @xml = xml
60
+
61
+ if type==:uniq_result
62
+ self.instance_eval do
63
+ def count
64
+ element(:count)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ def surface
71
+ element(:surface)
72
+ end
73
+
74
+ def reading
75
+ element(:reading)
76
+ end
77
+
78
+ def pos
79
+ element(:pos)
80
+ end
81
+
82
+ def baseform
83
+ element(:baseform)
84
+ end
85
+
86
+ def feature
87
+ element(:feature)
88
+ end
89
+
90
+ private
91
+ def element(type)
92
+ @xml.elements["#{type}"].text.to_s
13
93
  end
14
94
  end
15
95
  end
data/spec/yma4r_spec.rb CHANGED
@@ -3,13 +3,357 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
3
3
  require 'yma4r'
4
4
 
5
5
  describe Yma4r, "を new する場合:" do
6
+ it ":appid を String で渡すと new できること" do
7
+ lambda{ Yma4r.new(:appid => 'appid') }.should_not raise_error
8
+ end
6
9
 
7
- it "引数なしでは例外になること" do
10
+ it "引数なしでは ClassX::AttrRequiredError になること" do
8
11
  lambda { Yma4r.new }.should raise_error(ClassX::AttrRequiredError)
9
12
  end
10
13
 
11
- it ":appid をハッシュで渡すと new できること" do
12
- lambda{ Yma4r.new(:appid => 'hoge') }.should_not raise_error(ClassX::AttrRequiredError)
14
+ it ":appid String で渡さないと ClassX::InvalidAttrArgument になること" do
15
+ lambda{ Yma4r.new(:appid => 1111) }.should raise_error(ClassX::InvalidAttrArgument)
16
+ end
17
+ end
18
+
19
+ describe Yma4r, "の sentence を設定する場合:" do
20
+ before do
21
+ @yma4r = Yma4r.new(:appid => 'appid')
22
+ end
23
+
24
+ it "String で 渡すと sentence に設定されること" do
25
+ @yma4r.sentence = target = 'すもももももももものうち'
26
+ @yma4r.sentence.should == target
27
+ end
28
+
29
+ it "String で 渡さないと ClassX::InvalidAttrArgument になること" do
30
+ lambda{ @yma4r.sentence = 1111 }.should raise_error(ClassX::InvalidAttrArgument)
31
+ end
32
+ end
33
+
34
+ describe Yma4r, "の results を設定する場合:" do
35
+ before do
36
+ @yma4r = Yma4r.new(:appid => 'appid')
37
+ end
38
+
39
+ it ":ma で 渡すと results に設定されること" do
40
+ @yma4r.results = target = :ma
41
+ @yma4r.results.should == [target]
42
+ end
43
+
44
+ it ":uniq で渡すと results に設定されること" do
45
+ @yma4r.results = target = :uniq
46
+ @yma4r.results.should == [target]
47
+ end
48
+
49
+ it "[:ma] で 渡すと results に設定されること" do
50
+ @yma4r.results = target = [:ma]
51
+ @yma4r.results.should == target
52
+ end
53
+
54
+ it "[:ma, :uniq] で 渡すと results に設定されること" do
55
+ @yma4r.results = target = [:ma, :uniq]
56
+ @yma4r.results.should == target
57
+ end
58
+
59
+ it "String で渡すと Symbol になること" do
60
+ @yma4r.results = target = 'ma'
61
+ @yma4r.results.should == [target.to_sym]
62
+ end
63
+
64
+ it ":ma, :uniq 以外なら ClassX::InvalidAttrArgument になること" do
65
+ lambda{ @yma4r.results = :hoge }.should raise_error(ClassX::InvalidAttrArgument)
66
+ end
67
+ end
68
+
69
+ describe Yma4r, "の response を設定する場合:" do
70
+ before do
71
+ @yma4r = Yma4r.new(:appid => 'appid')
72
+ end
73
+
74
+ it "[:surface,:reading,:pos] で渡すと response に設定されること" do
75
+ @yma4r.response = target = [:surface,:reading,:pos]
76
+ @yma4r.response.should == target
77
+ end
78
+
79
+ it "Symbol で渡すと Array になること" do
80
+ @yma4r.response = target = :surface
81
+ @yma4r.response.should == [target]
82
+ end
83
+
84
+ it "Array の内訳が :surface, :reading, :pos, :baseform, :feature なら例外にならないこと" do
85
+ lambda{ @yma4r.response = [:surface] }.should_not raise_error
86
+ lambda{ @yma4r.response = [:reading] }.should_not raise_error
87
+ lambda{ @yma4r.response = [:pos] }.should_not raise_error
88
+ lambda{ @yma4r.response = [:baseform] }.should_not raise_error
89
+ lambda{ @yma4r.response = [:feature] }.should_not raise_error
90
+ end
91
+
92
+ it "Array の内訳が :surface, :reading, :pos, :baseform, :feature 以外なら ClassX::InvalidAttrArgument になること" do
93
+ lambda{ @yma4r.response = [:hoge] }.should raise_error(ClassX::InvalidAttrArgument)
94
+ end
95
+ end
96
+
97
+ describe Yma4r, "の filter を設定する場合:" do
98
+ before do
99
+ @yma4r = Yma4r.new(:appid => 'appid')
100
+ end
101
+
102
+ it "[1,2,3] で渡すと filter に設定されること" do
103
+ @yma4r.filter = target = [1,2,3]
104
+ @yma4r.filter.should == target
105
+ end
106
+
107
+ it "Fixnum で渡すと Array になること" do
108
+ @yma4r.filter = target = 13
109
+ @yma4r.filter.should == [target]
110
+ end
111
+
112
+ it "'2|3|4' の文字列で渡すと Array になること" do
113
+ @yma4r.filter = target = '2|3|4'
114
+ @yma4r.filter.should == target.split('|').map { |v| v.to_i }
115
+ end
116
+
117
+ it "Array の内訳が 1 以上 13 以下 でないと ClassX::InvalidAttrArgument になること" do
118
+ lambda{ @yma4r.filter = [0] }.should raise_error(ClassX::InvalidAttrArgument)
119
+ lambda{ @yma4r.filter = [14] }.should raise_error(ClassX::InvalidAttrArgument)
120
+ end
121
+ end
122
+
123
+ describe Yma4r, "の ma_response を設定する場合:" do
124
+ before do
125
+ @yma4r = Yma4r.new(:appid => 'appid')
126
+ end
127
+
128
+ it "[:surface,:reading,:pos] で渡すと ma_response に設定されること" do
129
+ @yma4r.ma_response = target = [:surface,:reading,:pos]
130
+ @yma4r.ma_response.should == target
131
+ end
132
+
133
+ it "Symbol で渡すと Array になること" do
134
+ @yma4r.ma_response = target = :surface
135
+ @yma4r.ma_response.should == [target]
136
+ end
137
+
138
+ it "Array の内訳が :surface, :reading, :pos, :baseform, :feature なら例外にならないこと" do
139
+ lambda{ @yma4r.ma_response = [:surface] }.should_not raise_error
140
+ lambda{ @yma4r.ma_response = [:reading] }.should_not raise_error
141
+ lambda{ @yma4r.ma_response = [:pos] }.should_not raise_error
142
+ lambda{ @yma4r.ma_response = [:baseform] }.should_not raise_error
143
+ lambda{ @yma4r.ma_response = [:feature] }.should_not raise_error
144
+ end
145
+
146
+ it "Array の内訳が :surface, :reading, :pos, :baseform, :feature 以外なら ClassX::InvalidAttrArgument になること" do
147
+ lambda{ @yma4r.ma_response = [:hoge] }.should raise_error(ClassX::InvalidAttrArgument)
148
+ end
149
+ end
150
+
151
+ describe Yma4r, "の ma_filter を設定する場合:" do
152
+ before do
153
+ @yma4r = Yma4r.new(:appid => 'appid')
154
+ end
155
+
156
+ it "[1,2,3] で渡すと ma_filter に設定されること" do
157
+ @yma4r.ma_filter = target = [1,2,3]
158
+ @yma4r.ma_filter.should == target
159
+ end
160
+
161
+ it "Fixnum で渡すと Array になること" do
162
+ @yma4r.ma_filter = target = 13
163
+ @yma4r.ma_filter.should == [target]
164
+ end
165
+
166
+ it "'2|3|4' の文字列で渡すと Array になること" do
167
+ @yma4r.ma_filter = target = '2|3|4'
168
+ @yma4r.ma_filter.should == target.split('|').map { |v| v.to_i }
169
+ end
170
+
171
+ it "Array の内訳が 1 以上 13 以下 でないと ClassX::InvalidAttrArgument になること" do
172
+ lambda{ @yma4r.ma_filter = [0] }.should raise_error(ClassX::InvalidAttrArgument)
173
+ lambda{ @yma4r.ma_filter = [14] }.should raise_error(ClassX::InvalidAttrArgument)
174
+ end
175
+ end
176
+
177
+ describe Yma4r, "の uniq_response を設定する場合:" do
178
+ before do
179
+ @yma4r = Yma4r.new(:appid => 'appid')
180
+ end
181
+
182
+ it "[:surface,:reading,:pos] で渡すと uniq_response に設定されること" do
183
+ @yma4r.uniq_response = target = [:surface,:reading,:pos]
184
+ @yma4r.uniq_response.should == target
185
+ end
186
+
187
+ it "Symbol で渡すと Array になること" do
188
+ @yma4r.uniq_response = target = :surface
189
+ @yma4r.uniq_response.should == [target]
190
+ end
191
+
192
+ it "Array の内訳が :surface, :reading, :pos, :baseform, :feature なら例外にならないこと" do
193
+ lambda{ @yma4r.uniq_response = [:surface] }.should_not raise_error
194
+ lambda{ @yma4r.uniq_response = [:reading] }.should_not raise_error
195
+ lambda{ @yma4r.uniq_response = [:pos] }.should_not raise_error
196
+ lambda{ @yma4r.uniq_response = [:baseform] }.should_not raise_error
197
+ lambda{ @yma4r.uniq_response = [:feature] }.should_not raise_error
198
+ end
199
+
200
+ it "Array の内訳が :surface, :reading, :pos, :baseform, :feature 以外なら ClassX::InvalidAttrArgument になること" do
201
+ lambda{ @yma4r.uniq_response = [:hoge] }.should raise_error(ClassX::InvalidAttrArgument)
202
+ end
203
+ end
204
+
205
+ describe Yma4r, "の uniq_filter を設定する場合:" do
206
+ before do
207
+ @yma4r = Yma4r.new(:appid => 'appid')
208
+ end
209
+
210
+ it "[1,2,3] で渡すと uniq_filter に設定されること" do
211
+ @yma4r.uniq_filter = target = [1,2,3]
212
+ @yma4r.uniq_filter.should == target
213
+ end
214
+
215
+ it "Fixnum で渡すと Array になること" do
216
+ @yma4r.uniq_filter = target = 13
217
+ @yma4r.uniq_filter.should == [target]
218
+ end
219
+
220
+ it "'2|3|4' の文字列で渡すと Array になること" do
221
+ @yma4r.uniq_filter = target = '2|3|4'
222
+ @yma4r.uniq_filter.should == target.split('|').map{ |v| v.to_i }
223
+ end
224
+
225
+ it "Array の内訳が 1 以上 13 以下 でないと ClassX::InvalidAttrArgument になること" do
226
+ lambda{ @yma4r.uniq_filter = [0] }.should raise_error(ClassX::InvalidAttrArgument)
227
+ lambda{ @yma4r.uniq_filter = [14] }.should raise_error(ClassX::InvalidAttrArgument)
228
+ end
229
+ end
230
+
231
+ describe Yma4r, "の uniq_by_baseform を設定する場合:" do
232
+ before do
233
+ @yma4r = Yma4r.new(:appid => 'appid')
234
+ end
235
+
236
+ it "false を渡すと uniq_by_baseform に設定されること" do
237
+ pending 'falseを登録したいが、nilが帰ってくるなぁ…'
238
+ @yma4r.uniq_by_baseform = target = false
239
+ @yma4r.uniq_by_baseform.should == target.to_s
240
+ end
241
+
242
+ it "TrueClass か FalseClass を設定できること" do
243
+ lambda{ @yma4r.uniq_by_baseform = true }.should_not raise_error
244
+ lambda{ @yma4r.uniq_by_baseform = false }.should_not raise_error
245
+ end
246
+
247
+ it "TrueClass か FalseClass でないとき ClassX::InvalidAttrArgument になること" do
248
+ lambda{ @yma4r.uniq_by_baseform = 'hoge' }.should raise_error(ClassX::InvalidAttrArgument)
249
+ end
250
+ end
251
+
252
+ describe Yma4r, "の query を実行したとき" do
253
+ before do
254
+ @yma4r = Yma4r.new(:appid => 'appid')
255
+ end
256
+
257
+ it "sentence が設定されていない場合 ClassX::AttrRequiredError になること" do
258
+ lambda{ @yma4r.query }.should raise_error(ClassX::AttrRequiredError)
259
+ end
260
+
261
+ it "sentence に設定された内容がURI.encodeされて出力されること" do
262
+ @yma4r.sentence = target = 'すもももももももものうち'
263
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
264
+ Hash[*alist.flatten]['sentence'].should == URI.encode(target)
265
+ end
266
+
267
+ it "results に設定された内容が出力されること" do
268
+ @yma4r.results = target = :ma
269
+ @yma4r.sentence = 'ほげふが'
270
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
271
+ Hash[*alist.flatten]['results'].should == URI.encode(target.to_s)
272
+ end
273
+
274
+ it "response に設定された内容が出力されること" do
275
+ @yma4r.response = target = [:reading, :pos]
276
+ @yma4r.sentence = 'ほげふが'
277
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
278
+ Hash[*alist.flatten]['response'].should == URI.encode(target.join(','))
279
+ end
280
+
281
+ it "filter に設定された内容が出力されること" do
282
+ @yma4r.filter = target = [1,2,3]
283
+ @yma4r.sentence = 'ほげふが'
284
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
285
+ Hash[*alist.flatten]['filter'].should == URI.encode(target.join('|'))
286
+ end
287
+
288
+ it "ma_response に設定された内容が出力されること" do
289
+ @yma4r.ma_response = target = [:reading, :pos]
290
+ @yma4r.sentence = 'ほげふが'
291
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
292
+ Hash[*alist.flatten]['ma_response'].should == URI.encode(target.join(','))
293
+ end
294
+
295
+ it "ma_filter に設定された内容が出力されること" do
296
+ @yma4r.ma_filter = target = [1,2,3]
297
+ @yma4r.sentence = 'ほげふが'
298
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
299
+ Hash[*alist.flatten]['ma_filter'].should == URI.encode(target.join('|'))
300
+ end
301
+
302
+ it "uniq_response に設定された内容が出力されること" do
303
+ @yma4r.uniq_response = target = [:reading, :pos]
304
+ @yma4r.sentence = 'ほげふが'
305
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
306
+ Hash[*alist.flatten]['uniq_response'].should == URI.encode(target.join(','))
307
+ end
308
+
309
+ it "uniq_filter に設定された内容が出力されること" do
310
+ @yma4r.uniq_filter = target = [1,2,3]
311
+ @yma4r.sentence = 'ほげふが'
312
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
313
+ Hash[*alist.flatten]['uniq_filter'].should == URI.encode(target.join('|'))
314
+ end
315
+
316
+ it "uniq_by_baseform に設定された内容が出力されること" do
317
+ @yma4r.uniq_by_baseform = target = true
318
+ @yma4r.sentence = 'ほげふが'
319
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
320
+ Hash[*alist.flatten]['uniq_by_baseform'].should == URI.encode(target.to_s)
321
+ end
322
+
323
+ it "全部設定しても大丈夫なこと" do
324
+ @yma4r.sentence = sentence = 'すもももももももものうち'
325
+ @yma4r.results = results = :ma
326
+ @yma4r.response = response = [:reading, :pos]
327
+ @yma4r.filter = filter = [1,2,3]
328
+ @yma4r.ma_response = ma_response = :surface
329
+ @yma4r.ma_filter = ma_filter = 1
330
+ @yma4r.uniq_response = uniq_response = :feature
331
+ @yma4r.uniq_filter = uniq_filter = '5|6|7'
332
+ @yma4r.uniq_by_baseform = uniq_by_baseform = true
333
+
334
+ alist = @yma4r.query.split('&').map{ |val| val.split('=') }
335
+ hash = Hash[*alist.flatten]
336
+
337
+ hash['sentence'].should == URI.encode(sentence)
338
+ hash['results'].should == URI.encode(results.to_s)
339
+ hash['response'].should == URI.encode(response.join(','))
340
+ hash['filter'].should == URI.encode(filter.join('|'))
341
+ hash['ma_response'].should == URI.encode(ma_response.to_s)
342
+ hash['ma_filter'].should == URI.encode(ma_filter.to_s)
343
+ hash['uniq_response'].should == URI.encode(uniq_response.to_s)
344
+ hash['uniq_filter'].should == URI.encode(uniq_filter)
345
+ hash['uniq_by_baseform'].should == URI.encode(uniq_by_baseform.to_s)
346
+ end
347
+
348
+ describe Yma4r, "の post を実行したとき" do
349
+ before do
350
+ pending 'rspecのモックとスタブの書き方を調べる'
351
+ @yma4r = Yma4r.new(:appid => 'appid',
352
+ :sentence => 'すもももももももものうち')
353
+ end
354
+
355
+ it "YmaParser が返ること"
356
+ it "アクセスできなかった時例外が返ること"
357
+ it "パースできなかった時例外が返ること"
13
358
  end
14
-
15
359
  end
@@ -0,0 +1,182 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+ require 'yma_parser'
4
+ require 'rexml/document'
5
+
6
+ describe YmaParser, ":について" do
7
+ before do
8
+ @xml = <<-'__EOL__'
9
+ <?xml version="1.0" encoding="UTF-8" ?>
10
+ <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn: yahoo:jp:jlp" xsi:schemaLocation="urn:yahoo:jp:jlp
11
+ http://jlp.yahooapis.jp/MAService/V1/parseResponse.xsd">
12
+ <ma_result>
13
+ <total_count>9</total_count>
14
+ <filtered_count>9</filtered_count>
15
+ <word_list>
16
+ <word>
17
+ <surface>庭</surface>
18
+ <reading>にわ</reading>
19
+ <pos>名詞</pos>
20
+ <baseform>庭</baseform>
21
+ </word>
22
+ <word>
23
+ <surface>に</surface>
24
+ <reading>に</reading>
25
+ <pos>助詞</pos>
26
+ <baseform>に</baseform>
27
+ </word>
28
+ <word>
29
+ <surface>は</surface>
30
+ <reading>は</reading>
31
+ <pos>助詞</pos>
32
+ <baseform>は</baseform>
33
+ </word>
34
+ <word>
35
+ <surface>二</surface>
36
+ <reading>2</reading>
37
+ <pos>名詞</pos>
38
+ <baseform>2</baseform>
39
+ </word>
40
+ <word>
41
+ <surface>羽</surface>
42
+ <reading>わ</reading>
43
+ <pos>名詞</pos>
44
+ <baseform>羽</baseform>
45
+ </word>
46
+ <word>
47
+ <surface>ニワトリ</surface>
48
+ <reading>にわとり</reading>
49
+ <pos>名詞</pos>
50
+ <baseform>ニワトリ</baseform>
51
+ </word>
52
+ <word>
53
+ <surface>が</surface>
54
+ <reading>が</reading>
55
+ <pos>助詞</pos>
56
+ <baseform>が</baseform>
57
+ </word>
58
+ <word>
59
+ <surface>いる</surface>
60
+ <reading>いる</reading>
61
+ <pos>動詞</pos>
62
+ <baseform>いる</baseform>
63
+ </word>
64
+ <word>
65
+ <surface>。</surface>
66
+ <reading>。</reading>
67
+ <pos>特殊</pos>
68
+ <baseform>。</baseform>
69
+ </word>
70
+ </word_list>
71
+ </ma_result>
72
+ <uniq_result>
73
+ <total_count>9</total_count>
74
+ <filtered_count>5</filtered_count>
75
+ <word_list>
76
+ <word>
77
+ <surface>庭</surface>
78
+ <reading>にわ</reading>
79
+ <pos>名詞</pos>
80
+ <baseform>庭</baseform>
81
+ <count>1</count>
82
+ </word>
83
+ <word>
84
+ <surface>二</surface>
85
+ <reading>2</reading>
86
+ <pos>名詞</pos>
87
+ <baseform>2</baseform>
88
+ <count>1</count>
89
+ </word>
90
+ <word>
91
+ <surface>羽</surface>
92
+ <reading>わ</reading>
93
+ <pos>名詞</pos>
94
+ <baseform>羽</baseform>
95
+ <count>1</count>
96
+ </word>
97
+ <word>
98
+ <surface>ニワトリ</surface>
99
+ <reading>にわとり</reading>
100
+ <pos>名詞</pos>
101
+ <baseform>ニワトリ</baseform>
102
+ <count>1</count>
103
+ </word>
104
+ <word>
105
+ <surface>いる</surface>
106
+ <reading>いる</reading>
107
+ <pos>動詞</pos>
108
+ <baseform>いる</baseform>
109
+ <count>1</count>
110
+ </word>
111
+ </word_list>
112
+ </uniq_result>
113
+ </ResultSet>
114
+ __EOL__
115
+ @result = YmaParser::ResultSet.new(@xml)
116
+ end
117
+
118
+ it "xml を実行したとき"
119
+
120
+ describe "ma_result を実行したとき" do
121
+ it "xml の返り値は ma_result 以下の xml に関するものであること" do
122
+ # これ微妙…
123
+ @result.ma_result.xml.to_s.should == REXML::Document.new(@xml).elements["//ma_result"].to_s
124
+ end
125
+
126
+ it "total_count, filterd_count, word_list を返せること" do
127
+ @result.ma_result.should respond_to :total_count, :filtered_count, :word_list
128
+ end
129
+
130
+ it "total_count, filterd_count の帰り値は整数であること" do
131
+ @result.ma_result.total_count.should be_kind_of Integer
132
+ @result.ma_result.filtered_count.should be_kind_of Integer
133
+ end
134
+
135
+ it "word_list の要素は surface,reading,pos,baseform,feature メソッドを返すこと" do
136
+ @result.ma_result.word_list.each do |word_type|
137
+ word_type.should respond_to :surface, :reading, :pos, :baseform, :feature
138
+ end
139
+ end
140
+
141
+ it "word の要素は xmlに準じていること" do
142
+ @result.ma_result.word_list.first do |word_type|
143
+ word_type.surface.should eql '庭'
144
+ word_type.reading.should eql 'にわ'
145
+ word_type.pos.should eql '名詞'
146
+ word_type.baseform.should eql '庭'
147
+ end
148
+ end
149
+ end
150
+
151
+ describe "uniq_result を実行したとき" do
152
+ it "xml の返り値は ma_result 以下の xml に関するものであること" do
153
+ # これ微妙…
154
+ @result.uniq_result.xml.to_s.should == REXML::Document.new(@xml).elements["//uniq_result"].to_s
155
+ end
156
+
157
+ it "total_count, filterd_count, word_list を返せること" do
158
+ @result.uniq_result.should respond_to :total_count, :filtered_count, :word_list
159
+ end
160
+
161
+ it "total_count, filterd_count の帰り値は整数であること" do
162
+ @result.uniq_result.total_count.should be_kind_of Integer
163
+ @result.uniq_result.filtered_count.should be_kind_of Integer
164
+ end
165
+
166
+ it "word_list の要素は surface,reading,pos,baseform,feature,pass メソッドを返すこと" do
167
+ @result.uniq_result.word_list.each do |word_type|
168
+ word_type.should respond_to :surface, :reading, :pos, :baseform, :feature, :count
169
+ end
170
+ end
171
+
172
+ it "word の要素は xmlに準じていること" do
173
+ @result.ma_result.word_list.first do |word_type|
174
+ word_type.surface.should eql '庭'
175
+ word_type.reading.should eql 'にわ'
176
+ word_type.pos.should eql '名詞'
177
+ word_type.baseform.should eql '庭'
178
+ word_type.count.should eql 1
179
+ end
180
+ end
181
+ end
182
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: niku-yma4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - niku
@@ -9,11 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-20 00:00:00 -08:00
12
+ date: 2009-02-22 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: classx
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
@@ -23,6 +24,7 @@ dependencies:
23
24
  version:
24
25
  - !ruby/object:Gem::Dependency
25
26
  name: rspec
27
+ type: :runtime
26
28
  version_requirement:
27
29
  version_requirements: !ruby/object:Gem::Requirement
28
30
  requirements:
@@ -46,9 +48,10 @@ files:
46
48
  - spec/spec.opts
47
49
  - spec/spec_helper.rb
48
50
  - spec/yma4r_spec.rb
51
+ - spec/yma_parser_spec.rb
49
52
  - lib/yma4r.rb
50
53
  - lib/yma_parser.rb
51
- has_rdoc: true
54
+ has_rdoc: false
52
55
  homepage: http://github.com/niku/yma4r
53
56
  post_install_message:
54
57
  rdoc_options:
@@ -87,3 +90,4 @@ specification_version: 2
87
90
  summary: yma4r is Yahoo Morphological Analyzer wrapper For Ruby
88
91
  test_files:
89
92
  - spec/yma4r_spec.rb
93
+ - spec/yma_parser_spec.rb