pebbles-zenra 0.1.0

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/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2010-11-24
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/pebbles/zenra.rb
6
+ lib/pebbles/yahoo_api_da_service.rb
7
+ spec/account.rb.sample
8
+ spec/spec_helper.rb
9
+ spec/yahoo_api_spec.rb
10
+ spec/zenra_spec.rb
data/README.txt ADDED
@@ -0,0 +1,72 @@
1
+ = ruby_zenra
2
+
3
+ * https://github.com/kwappa/pebbles-zenra
4
+
5
+ == DESCRIPTION:
6
+
7
+ zenra-ize some text.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * zenra-ize some text.
12
+
13
+ == SYNOPSIS:
14
+
15
+ require 'pebbles/zenra'
16
+ Zenra.app_id = "#{your_yahoo_api_application_id}"
17
+ # (or set environment valiable `YAHOO_APPID`)
18
+
19
+ Zenra.new.zenrize '合コンに参加する。'
20
+ # => '合コンに全裸で参加する。'
21
+
22
+ Zenra.new.zenrize 'お腹が空いたのでスパゲッティが食べたい', :pos => '名詞', :add => '夜の'
23
+ # => '夜のお腹が空いたので夜のスパゲッティが食べたい'
24
+
25
+ z '焼肉を食べて酒を飲んだ。'
26
+ # => '焼肉を全裸で食べて酒を全裸で飲んだ。'
27
+
28
+ == REQUIREMENTS:
29
+
30
+ * Yahoo! API key
31
+
32
+ == INSTALL:
33
+
34
+ * gem install zenra
35
+
36
+ == DEVELOPPERS:
37
+
38
+ * If you want to run specs
39
+
40
+ cp spec/account.rb.sample spec/account.rb
41
+
42
+ and write your Yahoo! API application id into spec/account.rb
43
+
44
+ == THANKS AND RESPECT TO:
45
+
46
+ * Earier Developer `yohfee`
47
+ https://gist.github.com/713759
48
+
49
+ == LICENSE:
50
+
51
+ (The MIT License)
52
+
53
+ Copyright (c) 2010 kwappa (SHIOYA, Hiromu)
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining
56
+ a copy of this software and associated documentation files (the
57
+ 'Software'), to deal in the Software without restriction, including
58
+ without limitation the rights to use, copy, modify, merge, publish,
59
+ distribute, sublicense, and/or sell copies of the Software, and to
60
+ permit persons to whom the Software is furnished to do so, subject to
61
+ the following conditions:
62
+
63
+ The above copyright notice and this permission notice shall be
64
+ included in all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
67
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
68
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
69
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
70
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
71
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
72
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # -*- ruby -*-
2
+ require 'rubygems'
3
+ require 'hoe'
4
+
5
+ Hoe.spec 'pebbles-zenra' do
6
+ developer('SHIOYA, Hiromu', 'kwappa.856@gmail.com')
7
+ end
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'net/http'
3
+ require 'cgi'
4
+ require 'rexml/document'
5
+
6
+ class YahooApiDAService
7
+ REQUEST_HOST = 'jlp.yahooapis.jp'
8
+ API_PATH = '/DAService/V1/parse'
9
+
10
+ def initialize app_id, sentence
11
+ @app_id = app_id
12
+ @sentence = sentence
13
+ request
14
+ self
15
+ end
16
+
17
+ def request
18
+ http = Net::HTTP.new(REQUEST_HOST,80)
19
+ query = "?appid=#{@app_id}&sentence=#{CGI.escape(@sentence)}"
20
+ req = Net::HTTP::Get.new(API_PATH + query)
21
+ @response = http.request(req)
22
+ end
23
+
24
+ def xml_document
25
+ @xml_doc ||= REXML::Document.new @response.body
26
+ @xml_doc
27
+ end
28
+
29
+ def parsed_array
30
+ result = Array.new
31
+ xml_document.elements.each('ResultSet/Result/ChunkList/Chunk') do |chunk|
32
+ chunk.elements.each('MorphemList/Morphem') do |morphem|
33
+ result << {
34
+ 'POS' => morphem.elements['POS'].text,
35
+ 'Surface' => morphem.elements['Surface'].text,
36
+ }
37
+ end
38
+ end
39
+ result
40
+ end
41
+
42
+ end
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+ $: << File.join(File.dirname(File.expand_path(__FILE__)))
3
+ require 'pebbles'
4
+ require 'yahoo_api_da_service'
5
+
6
+ def z sentence
7
+ Pebbles::Zenra.new.zenrize sentence
8
+ end
9
+
10
+ class Pebbles::Zenra
11
+ VERSION = '0.1.0'
12
+ @@app_id = ENV['YAHOO_APPID']
13
+
14
+ def self.app_id
15
+ @@app_id
16
+ end
17
+
18
+ def self.app_id= app_id
19
+ @@app_id = app_id
20
+ end
21
+
22
+ def initialize
23
+ raise RuntimeError, 'Yahoo API was not given.' unless Zenra.app_id
24
+ end
25
+
26
+ def zenrize sentence, opts = {}
27
+ opts = { :pos => '動詞', :add => '全裸で' }.merge(opts)
28
+ YahooApiDAService.new(Zenra.app_id, sentence).parsed_array.inject('') do |r, v|
29
+ r += opts[:add] if v['POS'].eql? opts[:pos]
30
+ r += v['Surface']
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,2 @@
1
+ # -*- coding: utf-8 -*-
2
+ APPLICATION_ID = '[dummy] ENTER YOUR YAHOO API APPLICATION ID --'
@@ -0,0 +1,3 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(File.expand_path(__FILE__)), %w(.. lib pebbles zenra))
3
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'account')
@@ -0,0 +1,63 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper')
3
+
4
+ describe YahooApiDAService do
5
+ context 'connect with application id' do
6
+ before :all do
7
+ @y = YahooApiDAService.new APPLICATION_ID, 'うちの庭には二羽鶏がいます。'
8
+ end
9
+
10
+ it 'should build REXML::Document from sample sentence' do
11
+ xml_doc = @y.xml_document
12
+ results = [
13
+ %w{ 名詞 地名町名 * うち うち うち },
14
+ %w{ 助詞 助詞連体化 * の の の },
15
+ %w{ 名詞 名詞場所 * 庭 にわ 庭 },
16
+ %w{ 助詞 格助詞 * に に に },
17
+ %w{ 助詞 係助詞 * は は は },
18
+ %w{ 接尾辞 助数 * 二羽 2わ 2羽 },
19
+ %w{ 名詞 名詞 * 鶏 にわとり 鶏 },
20
+ %w{ 助詞 格助詞 * が が が },
21
+ %w{ 動詞 一段 未然ウ接続 い い い },
22
+ %w{ 助動詞 助動詞ます 基本形 ます ま ま },
23
+ %w{ 特殊 句点 * 。 。 。 },
24
+ ]
25
+ keys = {
26
+ 0 => 'POS',
27
+ 3 => 'Surface',
28
+ 4 => 'Reading',
29
+ 5 => 'Baseform',
30
+ }
31
+
32
+ idx = 0
33
+ xml_doc.elements.each('ResultSet/Result/ChunkList/Chunk') do |chunk|
34
+ chunk.elements.each('MorphemList/Morphem') do |morphem|
35
+ r = results[idx]
36
+ keys.each do |k, v|
37
+ morphem.elements[v].text.should be_eql r[k]
38
+ end
39
+ idx += 1
40
+ end
41
+ end
42
+ end
43
+
44
+ it 'should create parsed array' do
45
+ parsed_array = @y.parsed_array
46
+
47
+ result = [
48
+ { 'POS' => '名詞', 'Surface' => 'うち', },
49
+ { 'POS' => '助詞', 'Surface' => 'の', },
50
+ { 'POS' => '名詞', 'Surface' => '庭', },
51
+ { 'POS' => '助詞', 'Surface' => 'に', },
52
+ { 'POS' => '助詞', 'Surface' => 'は', },
53
+ { 'POS' => '接尾辞', 'Surface' => '二羽', },
54
+ { 'POS' => '名詞', 'Surface' => '鶏', },
55
+ { 'POS' => '助詞', 'Surface' => 'が', },
56
+ { 'POS' => '動詞', 'Surface' => 'い', },
57
+ { 'POS' => '助動詞', 'Surface' => 'ます', },
58
+ { 'POS' => '特殊', 'Surface' => '。', },
59
+ ]
60
+ parsed_array.should be_eql result
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,48 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper')
3
+
4
+ describe Zenra do
5
+
6
+ context 'zenrize text' do
7
+ context 'without application id' do
8
+ it { lambda{ Zenra.new }.should raise_error RuntimeError }
9
+ it { Object.private_methods.should be_include :z}
10
+ end
11
+
12
+ context 'with application id' do
13
+ before :all do
14
+ Zenra.app_id = APPLICATION_ID
15
+ @zenra = Zenra.new
16
+ end
17
+
18
+ context 'and no extra option' do
19
+ it { @zenra.zenrize('お腹が空きました'). should be_eql 'お腹が全裸で空きました' }
20
+ end
21
+ context 'and extra :pos and :add option' do
22
+ it {
23
+ @zenra.zenrize('お腹が空いたのでスパゲッティが食べたい', :pos => '名詞', :add => '夜の').
24
+ should be_eql '夜のお腹が空いたので夜のスパゲッティが食べたい'
25
+ }
26
+ end
27
+ context 'and extra :add option' do
28
+ it {
29
+ @zenra.zenrize('お腹が空いたのでスパゲッティが食べたい', :add => '半裸で').
30
+ should be_eql 'お腹が半裸で空いたのでスパゲッティが半裸で食べたい'
31
+ }
32
+ end
33
+ context 'and extra :pos option' do
34
+ it {
35
+ @zenra.zenrize('お腹が空いたのでスパゲッティが食べたい', :pos => '名詞').
36
+ should be_eql '全裸でお腹が空いたので全裸でスパゲッティが食べたい'
37
+ }
38
+ end
39
+ context 'called from top level' do
40
+ it {
41
+ z('焼肉を食べて酒を飲んだ。').
42
+ should be_eql '焼肉を全裸で食べて酒を全裸で飲んだ。'
43
+ }
44
+ end
45
+ end
46
+
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pebbles-zenra
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - SHIOYA, Hiromu
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-19 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hoe
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 8
31
+ - 0
32
+ version: 2.8.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: zenra-ize some text.
36
+ email:
37
+ - kwappa.856@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - README.txt
46
+ files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.txt
50
+ - Rakefile
51
+ - lib/pebbles/zenra.rb
52
+ - lib/pebbles/yahoo_api_da_service.rb
53
+ - spec/account.rb.sample
54
+ - spec/spec_helper.rb
55
+ - spec/yahoo_api_spec.rb
56
+ - spec/zenra_spec.rb
57
+ has_rdoc: true
58
+ homepage: https://github.com/kwappa/pebbles-zenra
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --main
64
+ - README.txt
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project: pebbles-zenra
86
+ rubygems_version: 1.3.7
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: zenra-ize some text.
90
+ test_files: []
91
+