google-ime-skk 1.0.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.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ rspec (~> 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 hitode909
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = google-ime-skk
2
+
3
+ Google IME SKK Server
4
+
5
+ == Run:
6
+
7
+ % google-ime-skk
8
+
9
+ == Options:
10
+ -p, --port 55100 Listen port number
11
+ -h, --host "0.0.0.0" Listen hostname
12
+ -x "http://proxy.example.com:8080",
13
+ --proxy HTTP Proxy server
14
+ -c, --cache-time 1h Cache keep time
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2011 hitode909. See LICENSE.txt for
19
+ further details.
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "google-ime-skk"
16
+ gem.homepage = "http://github.com/hitode909/google-ime-skk"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Google IME SKK Server}
19
+ gem.description = %Q{SKK Server which uses Google CGI API for Japanese Input}
20
+ gem.email = "hitode909@gmail.com"
21
+ gem.authors = ["hitode909"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ gem.executables = ["google-ime-skk"]
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
37
+ spec.pattern = 'spec/**/*_spec.rb'
38
+ spec.rcov = true
39
+ end
40
+
41
+ task :default => :spec
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "google-ime-skk #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby -Ku
2
+
3
+ self_file =
4
+ if File.symlink?(__FILE__)
5
+ require 'pathname'
6
+ Pathname.new(__FILE__).realpath
7
+ else
8
+ __FILE__
9
+ end
10
+ $:.unshift(File.dirname(self_file) + "/../lib")
11
+
12
+ require 'google-ime-skk/cli'
13
+
14
+ GoogleImeSkk::CLI.execute
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'socialskk'
3
+ require 'cgi'
4
+ require 'json'
5
+
6
+ class GoogleImeSkk < SocialSKK
7
+ VERSION_STRING = "GoogleImeSKK0.1 "
8
+
9
+ def encode_to_utf8(text)
10
+ if String.new.respond_to?(:encode)
11
+ text.encode('utf-8', 'euc-jp')
12
+ else
13
+ require 'kconv'
14
+ Kconv.toutf8(text)
15
+ end
16
+ end
17
+
18
+ def encode_to_eucjp(text)
19
+ if String.new.respond_to?(:encode)
20
+ text.encode('euc-jp', 'utf-8')
21
+ else
22
+ require 'kconv'
23
+ Kconv.toeuc(text)
24
+ end
25
+ end
26
+
27
+ def social_ime_search(text)
28
+ text = encode_to_utf8(text)
29
+ text = text.sub(/[a-z]?$/) { |m| ',' + m }
30
+ uri = URI.parse 'http://www.google.com/transliterate'
31
+ http = Net::HTTP.new(uri.host, uri.port)
32
+ http = Net::HTTP.new(uri.host, uri.port, @proxy.host, @proxy.port) if @proxy
33
+ http.start do |h|
34
+ res = h.get("/transliterate?langpair=ja-Hira%7Cja&text=" + URI.escape(text))
35
+ obj = JSON.parse(res.body.to_s)
36
+ encode_to_eucjp(obj[0][1].join('/'))
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,55 @@
1
+ require 'google-ime-skk'
2
+ require 'optparse'
3
+ require 'ostruct'
4
+
5
+ module GoogleImeSkk::CLI
6
+
7
+ def self.time_parse(str)
8
+ n, u = str.scan(/(\d+)([dhms]?)/)[0]
9
+ n = n.to_i
10
+ case (u)
11
+ when "d"
12
+ n *= 24 * 60 * 60
13
+ when "h"
14
+ n *= 60 * 60
15
+ when "m"
16
+ n *= 60
17
+ end
18
+ n
19
+ end
20
+
21
+ def self.execute
22
+ opts = OpenStruct.new({
23
+ :port => 55100,
24
+ :host => '0.0.0.0',
25
+ :proxy => nil,
26
+ :cache_time => 3600,
27
+ })
28
+
29
+ OptionParser.new do |parser|
30
+ parser.instance_eval do
31
+ self.banner = "Usage: #{$0} [opts]"
32
+
33
+ separator ''
34
+ separator 'Options:'
35
+ on('-p', '--port 55100', 'Listen port number') do |port|
36
+ opts.port = port
37
+ end
38
+ on('-h', '--host "0.0.0.0"', 'Listen hostname') do |host|
39
+ opts.host = host
40
+ end
41
+ on('-x', '--proxy "http://proxy.example.com:8080"', 'HTTP Proxy server') do |proxy|
42
+ opts.proxy = URI.parse(proxy)
43
+ end
44
+ on('-c', '--cache-time 1h', 'Cache keep time') do |ct|
45
+ opts.cache_time = time_parse(ct)
46
+ end
47
+
48
+ parse!(ARGV)
49
+ end
50
+ end
51
+
52
+ ime = GoogleImeSkk.new(opts.host, opts.port, opts.proxy, opts.cache_time)
53
+ ime.mainloop
54
+ end
55
+ end
@@ -0,0 +1,117 @@
1
+ require "thread"
2
+ require 'socket'
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'timeout'
6
+ require 'yaml'
7
+
8
+ # http://coderepos.org/share/browser/lang/ruby/misc/socialskk/socialskk.rb
9
+
10
+ Net::HTTP.version_1_2
11
+
12
+ class SocialSKK
13
+ VERSION_STRING = "SocialSKK0.2 "
14
+
15
+ CLIENT_END = ?0
16
+ CLIENT_REQUEST = ?1
17
+ CLIENT_VERSION = ?2
18
+ CLIENT_HOST = ?3
19
+ SERVER_ERROR = ?0
20
+ SERVER_FOUND = ?1
21
+ SERVER_NOT_FOUND = ?4
22
+
23
+ BUFSIZE = 512
24
+ TIMEOUT = 10
25
+
26
+ def initialize(host, port, proxy, cache_time, cache=nil)
27
+ @host = host
28
+ @port = port
29
+ @proxy = proxy
30
+ @cache = cache || {}
31
+ @cache_time = cache_time
32
+ @ts = TCPServer.open(@host, @port)
33
+ puts "server is on #{@ts.addr[1..-1].join(":")}"
34
+ puts "proxy is #{@proxy.to_s}" if @proxy
35
+ puts "cache keep time #{@cache_time}sec"
36
+ end
37
+
38
+ def mainloop
39
+ loop do
40
+ Thread.start(@ts.accept) do |s|
41
+ while cmdbuf = s.sysread(BUFSIZE)
42
+ case cmdbuf[0]
43
+ when CLIENT_END
44
+ break
45
+ when CLIENT_REQUEST
46
+ cmdend = cmdbuf.index(?\ ) || cmdbuf.index(?\n)
47
+ kana = cmdbuf[1 .. (cmdend - 1)]
48
+ ret = ''
49
+ begin
50
+ if kanji = search(kana)
51
+ ret.concat(SERVER_FOUND)
52
+ ret.concat(kanji)
53
+ else
54
+ ret.concat(SERVER_NOT_FOUND)
55
+ ret.concat(cmdbuf[1 .. -1])
56
+ end
57
+ ret.concat("\n")
58
+ rescue Exception
59
+ ret.concat(SERVER_ERROR)
60
+ ret.concat($!)
61
+ end
62
+ s.write(ret)
63
+ when CLIENT_VERSION
64
+ s.write(VERSION_STRING)
65
+ when CLIENT_HOST
66
+ ret = host(s)
67
+ s.write(ret)
68
+ end
69
+ end
70
+ s.close
71
+ end
72
+ end
73
+ end
74
+
75
+ private
76
+ def host(sock = nil)
77
+ if sock.nil?
78
+ hostname = Socket.gethostname
79
+ ipaddr = TCPSocket.getaddress(hostname)
80
+ else
81
+ hostname, ipaddr = sock.addr[2], sock.addr[3]
82
+ end
83
+
84
+ hostname + ':' + ipaddr + ': '
85
+ end
86
+
87
+ def search(kana)
88
+ if @cache[kana] and Time.now < (@cache[kana][:ctime] + @cache_time)
89
+ @cache[kana][:kanji]
90
+ else
91
+ kanji = social_ime_search(kana)
92
+ @cache[kana] = {
93
+ :kanji => kanji,
94
+ :ctime => Time.now
95
+ }
96
+ kanji
97
+ end
98
+ end
99
+
100
+ def social_ime_search(kana)
101
+ size = kana.size / 2
102
+ kanji = '/'
103
+ begin
104
+ timeout(TIMEOUT) do
105
+ http = Net::HTTP.new('www.social-ime.com', 80)
106
+ http = Net::HTTP.new('www.social-ime.com', 80, @proxy.host, @proxy.port) if @proxy
107
+ http.start do |h|
108
+ res = h.get("/api/?string=#{URI.escape(kana)}&resize[0]=+#{size}")
109
+ kanji += res.body.to_s.split("\n").join('/').gsub(/\t/, '/')
110
+ end
111
+ end
112
+ rescue
113
+ kanji = nil
114
+ end
115
+ kanji
116
+ end
117
+ end
@@ -0,0 +1,10 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+ require 'kconv'
4
+
5
+ describe "GoogleImeSkk" do
6
+ it "can search" do
7
+ ime = GoogleImeSkk.new("localhost", 12343, nil, 3600)
8
+ Kconv.toutf8(ime.social_ime_search(Kconv.toeuc("ねこ"))).should match "猫"
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'google-ime-skk'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-ime-skk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - hitode909
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-04-20 00:00:00.000000000 +09:00
13
+ default_executable: google-ime-skk
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &2156553020 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.3.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2156553020
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &2156551900 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2156551900
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &2156550640 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.5.2
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2156550640
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &2156549300 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *2156549300
59
+ description: SKK Server which uses Google CGI API for Japanese Input
60
+ email: hitode909@gmail.com
61
+ executables:
62
+ - google-ime-skk
63
+ extensions: []
64
+ extra_rdoc_files:
65
+ - LICENSE.txt
66
+ - README.rdoc
67
+ files:
68
+ - .document
69
+ - .rspec
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.rdoc
74
+ - Rakefile
75
+ - VERSION
76
+ - bin/google-ime-skk
77
+ - lib/google-ime-skk.rb
78
+ - lib/google-ime-skk/cli.rb
79
+ - lib/socialskk.rb
80
+ - spec/google-ime-skk_spec.rb
81
+ - spec/spec_helper.rb
82
+ has_rdoc: true
83
+ homepage: http://github.com/hitode909/google-ime-skk
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ segments:
97
+ - 0
98
+ hash: 2417247502560721624
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.5.2
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Google IME SKK Server
111
+ test_files:
112
+ - spec/google-ime-skk_spec.rb
113
+ - spec/spec_helper.rb