superslau-pinyinify 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/README.txt +55 -0
- data/Rakefile +30 -0
- data/bin/pinyinify +8 -0
- data/data/hanzi.dat +94038 -0
- data/data/hanzi.yml +25478 -0
- data/lib/pinyinify.rb +49 -0
- data/lib/pinyinify/transliterate/transliterate.rb +36 -0
- data/pinyinify.gemspec +37 -0
- data/spec/pinyinify/transliterate/transliterate_spec.rb +58 -0
- data/spec/pinyinify_spec.rb +7 -0
- data/spec/spec_helper.rb +16 -0
- data/test/test_pinyinify.rb +0 -0
- metadata +77 -0
data/lib/pinyinify.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
|
2
|
+
module Pinyinify
|
3
|
+
|
4
|
+
# :stopdoc:
|
5
|
+
VERSION = '1.0.0'
|
6
|
+
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
7
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
8
|
+
# :startdoc:
|
9
|
+
|
10
|
+
# Returns the version string for the library.
|
11
|
+
#
|
12
|
+
def self.version
|
13
|
+
VERSION
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the library path for the module. If any arguments are given,
|
17
|
+
# they will be joined to the end of the libray path using
|
18
|
+
# <tt>File.join</tt>.
|
19
|
+
#
|
20
|
+
def self.libpath( *args )
|
21
|
+
args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the lpath for the module. If any arguments are given,
|
25
|
+
# they will be joined to the end of the path using
|
26
|
+
# <tt>File.join</tt>.
|
27
|
+
#
|
28
|
+
def self.path( *args )
|
29
|
+
args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Utility method used to require all files ending in .rb that lie in the
|
33
|
+
# directory below this file that has the same name as the filename passed
|
34
|
+
# in. Optionally, a specific _directory_ name can be passed in such that
|
35
|
+
# the _filename_ does not have to be equivalent to the directory.
|
36
|
+
#
|
37
|
+
def self.require_all_libs_relative_to( fname, dir = nil )
|
38
|
+
dir ||= ::File.basename(fname, '.*')
|
39
|
+
search_me = ::File.expand_path(
|
40
|
+
::File.join(::File.dirname(fname), dir, '**', '*.rb'))
|
41
|
+
|
42
|
+
Dir.glob(search_me).sort.each {|rb| require rb}
|
43
|
+
end
|
44
|
+
|
45
|
+
end # module Pinyinify
|
46
|
+
|
47
|
+
Pinyinify.require_all_libs_relative_to(__FILE__)
|
48
|
+
|
49
|
+
# EOF
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Pinyinify
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'active_support'
|
6
|
+
require 'active_support/multibyte'
|
7
|
+
|
8
|
+
class Transliterate
|
9
|
+
|
10
|
+
attr_reader :characters
|
11
|
+
|
12
|
+
def transliterate(string)
|
13
|
+
result = ""
|
14
|
+
ActiveSupport::Multibyte::Chars.u_unpack(string).each_with_index {|c,i|
|
15
|
+
lookup_result=lookup(c)
|
16
|
+
lookup_result==nil ? result=result+string.mb_chars[i] : result=result+lookup_result.strip+" "
|
17
|
+
}
|
18
|
+
result.strip
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
|
23
|
+
hanzi_file_yml = File.join(File.dirname(__FILE__), %w[ .. .. .. data hanzi.yml ])
|
24
|
+
@characters = YAML.load( IO.read(hanzi_file_yml) )
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def lookup(character_code)
|
31
|
+
ucode="U"+character_code.to_s(base=16).upcase #convert into form U12345
|
32
|
+
res = @characters[ucode]
|
33
|
+
res==nil ? nil : res.split(" ").first.gsub(/[^-A-Z\s]/, "").downcase
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/pinyinify.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{pinyinify}
|
5
|
+
s.version = "1.0.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["FIXME (who is writing this software)"]
|
9
|
+
s.date = %q{2009-03-15}
|
10
|
+
s.default_executable = %q{pinyinify}
|
11
|
+
s.description = %q{Transliterate from Mandarin to Pinyin}
|
12
|
+
s.email = %q{superslau@gmail.com}
|
13
|
+
s.executables = ["pinyinify"]
|
14
|
+
s.extra_rdoc_files = ["History.txt", "README.txt", "bin/pinyinify"]
|
15
|
+
s.files = ["History.txt", "README.txt", "Rakefile", "bin/pinyinify", "data/hanzi.dat", "data/hanzi.yml", "lib/pinyinify.rb", "lib/pinyinify/transliterate/transliterate.rb", "pinyinify.gemspec", "spec/pinyinify/transliterate/transliterate_spec.rb", "spec/pinyinify_spec.rb", "spec/spec_helper.rb", "test/test_pinyinify.rb"]
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.homepage = %q{http://github.com/superslau/pinyinify/}
|
18
|
+
s.rdoc_options = ["--main", "README.txt"]
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rubyforge_project = %q{pinyinify}
|
21
|
+
s.rubygems_version = %q{1.3.1}
|
22
|
+
s.summary = %q{Transliterate from Mandarin to Pinyin}
|
23
|
+
s.test_files = ["test/test_pinyinify.rb"]
|
24
|
+
|
25
|
+
if s.respond_to? :specification_version then
|
26
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
27
|
+
s.specification_version = 2
|
28
|
+
|
29
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
30
|
+
s.add_development_dependency(%q<bones>, [">= 2.4.2"])
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<bones>, [">= 2.4.2"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<bones>, [">= 2.4.2"])
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# spec/pinyinify/transliterate/transliterate_spec.rb
|
2
|
+
# $Id$
|
3
|
+
|
4
|
+
# Require the spec helper relative to this file
|
5
|
+
require File.join(File.dirname(__FILE__), %w[ .. .. spec_helper])
|
6
|
+
|
7
|
+
# No need to type Pinyinify:: before each call
|
8
|
+
include Pinyinify
|
9
|
+
|
10
|
+
describe Transliterate do
|
11
|
+
setup do
|
12
|
+
@t = Transliterate.new
|
13
|
+
end
|
14
|
+
|
15
|
+
# All of our specs for Transliterate will go in here
|
16
|
+
it "should be instantiated without any arguments" do
|
17
|
+
lambda { Transliterate.new() }.should_not raise_error
|
18
|
+
lambda { Transliterate.new( "moo" )}.should raise_error(ArgumentError)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should accept a string on a transliterate method, and return a string" do
|
22
|
+
@t.transliterate("text").should be_instance_of(String)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should accept a non chinese string and return the same value back " do
|
26
|
+
@t.transliterate("text test english").should == "text test english"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should accept a chinese character and return the correct transliterated string" do
|
30
|
+
@t.transliterate("周").should be_instance_of(String)
|
31
|
+
@t.transliterate("周").should == "zhou"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should accept a chinese phrase and return the correct transliterated string" do
|
35
|
+
@t.transliterate("最長的電影").should be_instance_of(String)
|
36
|
+
@t.transliterate("最長的電影").should == "zui chang de dian ying" #this transliteration came from itunes :)
|
37
|
+
@t.transliterate("东风破").should == "dong feng po" #this transliteration also came from itunes
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should accept a mixed chinese and english phrase and return the correct transliterated string" do
|
41
|
+
|
42
|
+
@t.transliterate("最長的電影is a good song").should == "zui chang de dian ying is a good song"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should load the hanzi data in from the data file on instantiation" do
|
46
|
+
IO.should_receive(:read).and_return(
|
47
|
+
"--- \nU742B: BENG3\nU3E61: XUAN2 XUAN4"
|
48
|
+
)
|
49
|
+
|
50
|
+
t = Transliterate.new
|
51
|
+
t.characters.should be_a_kind_of(Hash)
|
52
|
+
t.characters.keys.size.should == 2
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
# EOF
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
require File.expand_path(
|
3
|
+
File.join(File.dirname(__FILE__), %w[.. lib pinyinify]))
|
4
|
+
|
5
|
+
Spec::Runner.configure do |config|
|
6
|
+
# == Mock Framework
|
7
|
+
#
|
8
|
+
# RSpec uses it's own mocking framework by default. If you prefer to
|
9
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
10
|
+
#
|
11
|
+
# config.mock_with :mocha
|
12
|
+
# config.mock_with :flexmock
|
13
|
+
# config.mock_with :rr
|
14
|
+
end
|
15
|
+
|
16
|
+
# EOF
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: superslau-pinyinify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- FIXME (who is writing this software)
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-15 00:00:00 -07:00
|
13
|
+
default_executable: pinyinify
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bones
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.4.2
|
24
|
+
version:
|
25
|
+
description: Transliterate from Mandarin to Pinyin
|
26
|
+
email: superslau@gmail.com
|
27
|
+
executables:
|
28
|
+
- pinyinify
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- History.txt
|
33
|
+
- README.txt
|
34
|
+
- bin/pinyinify
|
35
|
+
files:
|
36
|
+
- History.txt
|
37
|
+
- README.txt
|
38
|
+
- Rakefile
|
39
|
+
- bin/pinyinify
|
40
|
+
- data/hanzi.dat
|
41
|
+
- data/hanzi.yml
|
42
|
+
- lib/pinyinify.rb
|
43
|
+
- lib/pinyinify/transliterate/transliterate.rb
|
44
|
+
- pinyinify.gemspec
|
45
|
+
- spec/pinyinify/transliterate/transliterate_spec.rb
|
46
|
+
- spec/pinyinify_spec.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
- test/test_pinyinify.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/superslau/pinyinify/
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --main
|
54
|
+
- README.txt
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: pinyinify
|
72
|
+
rubygems_version: 1.2.0
|
73
|
+
signing_key:
|
74
|
+
specification_version: 2
|
75
|
+
summary: Transliterate from Mandarin to Pinyin
|
76
|
+
test_files:
|
77
|
+
- test/test_pinyinify.rb
|