zenra 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2010-11-24
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/zenra.rb
6
+ lib/yahoo_api.rb
7
+ spec/account.rb.sample
8
+ spec/spec_helper.rb
9
+ spec/yahoo_api_spec.rb
10
+ spec/zenra_spec.rb
@@ -0,0 +1,72 @@
1
+ = ruby_zenra
2
+
3
+ * https://github.com/kwappa/ruby-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 '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.
@@ -0,0 +1,8 @@
1
+ # -*- ruby -*-
2
+ require 'rubygems'
3
+ require 'hoe'
4
+
5
+ Hoe.spec 'zenra' do
6
+ developer('SHIOYA, Hiromu', 'kwappa.856@gmail.com')
7
+ self.rubyforge_name = 'zenra'
8
+ 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,32 @@
1
+ # -*- coding: utf-8 -*-
2
+ $: << File.join(File.dirname(File.expand_path(__FILE__)))
3
+ require 'yahoo_api'
4
+
5
+ def z sentence
6
+ Zenra.new.zenrize sentence
7
+ end
8
+
9
+ class Zenra
10
+ VERSION = '0.1.0'
11
+ @@app_id = ENV['YAHOO_APPID']
12
+
13
+ def self.app_id
14
+ @@app_id
15
+ end
16
+
17
+ def self.app_id= app_id
18
+ @@app_id = app_id
19
+ end
20
+
21
+ def initialize
22
+ raise RuntimeError, 'Yahoo API was not given.' unless Zenra.app_id
23
+ end
24
+
25
+ def zenrize sentence, opts = {}
26
+ opts = { :pos => '動詞', :add => '全裸で' }.merge(opts)
27
+ YahooApiDAService.new(Zenra.app_id, sentence).parsed_array.inject('') do |r, v|
28
+ r += opts[:add] if v['POS'].eql? opts[:pos]
29
+ r += v['Surface']
30
+ end
31
+ end
32
+ 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 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,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 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-11-28 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyforge
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
+ - 0
31
+ - 4
32
+ version: 2.0.4
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 7
46
+ - 0
47
+ version: 2.7.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: zenra-ize some text.
51
+ email:
52
+ - kwappa.856@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - README.txt
61
+ files:
62
+ - History.txt
63
+ - Manifest.txt
64
+ - README.txt
65
+ - Rakefile
66
+ - lib/zenra.rb
67
+ - lib/yahoo_api.rb
68
+ - spec/account.rb.sample
69
+ - spec/spec_helper.rb
70
+ - spec/yahoo_api_spec.rb
71
+ - spec/zenra_spec.rb
72
+ has_rdoc: true
73
+ homepage: https://github.com/kwappa/ruby-zenra
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --main
79
+ - README.txt
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project: zenra
101
+ rubygems_version: 1.3.7
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: zenra-ize some text.
105
+ test_files: []
106
+