pinny 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +85 -0
- data/lib/pinny.rb +60 -0
- data/pinny.gemspec +25 -0
- data/test/pinny_test.rb +14 -0
- data/test/test_helper.rb +5 -0
- metadata +71 -0
data/Rakefile
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
require "rubygems/package_task"
|
6
|
+
require "rdoc/task"
|
7
|
+
|
8
|
+
require "rake/testtask"
|
9
|
+
Rake::TestTask.new do |t|
|
10
|
+
t.libs << "test"
|
11
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
task :default => ["test"]
|
17
|
+
|
18
|
+
# This builds the actual gem. For details of what all these options
|
19
|
+
# mean, and other ones you can add, check the documentation here:
|
20
|
+
#
|
21
|
+
# http://rubygems.org/read/chapter/20
|
22
|
+
#
|
23
|
+
spec = Gem::Specification.new do |s|
|
24
|
+
|
25
|
+
# Change these as appropriate
|
26
|
+
s.name = "pinny"
|
27
|
+
s.version = "0.1.0"
|
28
|
+
s.summary = "Pinyin conversion tool"
|
29
|
+
s.author = "Matthew Rudy Jacobs"
|
30
|
+
s.email = "MatthewRudyJacobs@gmail.com"
|
31
|
+
s.homepage = "http://github.com/matthewrudy/pinny"
|
32
|
+
|
33
|
+
s.has_rdoc = true
|
34
|
+
# You should probably have a README of some kind. Change the filename
|
35
|
+
# as appropriate
|
36
|
+
# s.extra_rdoc_files = %w(README)
|
37
|
+
# s.rdoc_options = %w(--main README)
|
38
|
+
|
39
|
+
# Add any extra files to include in the gem (like your README)
|
40
|
+
s.files = %w(pinny.gemspec Rakefile) + Dir.glob("{test,lib}/**/*")
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
|
43
|
+
# If you want to depend on other gems, add them here, along with any
|
44
|
+
# relevant versions
|
45
|
+
# s.add_dependency("some_other_gem", "~> 0.1.0")
|
46
|
+
|
47
|
+
# If your tests use any gems, include them here
|
48
|
+
# s.add_development_dependency("mocha") # for example
|
49
|
+
end
|
50
|
+
|
51
|
+
# This task actually builds the gem. We also regenerate a static
|
52
|
+
# .gemspec file, which is useful if something (i.e. GitHub) will
|
53
|
+
# be automatically building a gem for this project. If you're not
|
54
|
+
# using GitHub, edit as appropriate.
|
55
|
+
#
|
56
|
+
# To publish your gem online, install the 'gemcutter' gem; Read more
|
57
|
+
# about that here: http://gemcutter.org/pages/gem_docs
|
58
|
+
Gem::PackageTask.new(spec) do |pkg|
|
59
|
+
pkg.gem_spec = spec
|
60
|
+
end
|
61
|
+
|
62
|
+
desc "Build the gemspec file #{spec.name}.gemspec"
|
63
|
+
task :gemspec do
|
64
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
65
|
+
File.open(file, "w") {|f| f << spec.to_ruby }
|
66
|
+
end
|
67
|
+
|
68
|
+
# If you don't want to generate the .gemspec file, just remove this line. Reasons
|
69
|
+
# why you might want to generate a gemspec:
|
70
|
+
# - using bundler with a git source
|
71
|
+
# - building the gem without rake (i.e. gem build blah.gemspec)
|
72
|
+
# - maybe others?
|
73
|
+
task :package => :gemspec
|
74
|
+
|
75
|
+
# Generate documentation
|
76
|
+
RDoc::Task.new do |rd|
|
77
|
+
|
78
|
+
rd.rdoc_files.include("lib/**/*.rb")
|
79
|
+
rd.rdoc_dir = "rdoc"
|
80
|
+
end
|
81
|
+
|
82
|
+
desc 'Clear out RDoc and generated packages'
|
83
|
+
task :clean => [:clobber_rdoc, :clobber_package] do
|
84
|
+
rm "#{spec.name}.gemspec"
|
85
|
+
end
|
data/lib/pinny.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Pinny
|
2
|
+
|
3
|
+
TONES = {
|
4
|
+
"a" => [nil, "ā", "á", "ǎ", "à"],
|
5
|
+
"e" => [nil, "ē", "é", "ě", "è"],
|
6
|
+
"i" => [nil, "ī", "í", "ǐ", "ì"],
|
7
|
+
"o" => [nil, "ō", "ó", "ǒ", "ò"],
|
8
|
+
"u" => [nil, "ū", "ú", "ǔ", "ù"],
|
9
|
+
"v" => [nil, "ǖ", "ǘ", "ǚ", "ǜ"]
|
10
|
+
}
|
11
|
+
|
12
|
+
module ModuleMethods
|
13
|
+
def to_pinyin(string)
|
14
|
+
string.split(/\b/).map do |word|
|
15
|
+
if is_pinyin?(word)
|
16
|
+
add_tone_mark(word)
|
17
|
+
else
|
18
|
+
word
|
19
|
+
end
|
20
|
+
end.join("")
|
21
|
+
end
|
22
|
+
|
23
|
+
def is_pinyin?(word)
|
24
|
+
word =~ /^\w+[1-5]$/
|
25
|
+
end
|
26
|
+
|
27
|
+
def extract_tone(word)
|
28
|
+
tone = word.scan(/[1-4]$/).first
|
29
|
+
|
30
|
+
word.sub!(/\d$/, "")
|
31
|
+
|
32
|
+
tone && tone.to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
def lowest_vowel(word)
|
36
|
+
case word
|
37
|
+
when /a/i then "a"
|
38
|
+
when /e/i then "e"
|
39
|
+
when /i/i then "i"
|
40
|
+
when /o/i then "o"
|
41
|
+
when /u/i then "u"
|
42
|
+
when /v/i then "v"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_tone_mark(word)
|
47
|
+
if t = extract_tone(word)
|
48
|
+
v = lowest_vowel(word)
|
49
|
+
r = TONES[v] && TONES[v][t]
|
50
|
+
word.sub(v, r)
|
51
|
+
else
|
52
|
+
word
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
extend ModuleMethods
|
58
|
+
|
59
|
+
end
|
60
|
+
|
data/pinny.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{pinny}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Matthew Rudy Jacobs"]
|
9
|
+
s.date = %q{2011-10-13}
|
10
|
+
s.email = %q{youremail@example.com}
|
11
|
+
s.files = ["lib/pinny.rb"]
|
12
|
+
s.homepage = %q{http://yoursite.example.com}
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
s.rubygems_version = %q{1.4.2}
|
15
|
+
s.summary = %q{What this thing does}
|
16
|
+
|
17
|
+
if s.respond_to? :specification_version then
|
18
|
+
s.specification_version = 3
|
19
|
+
|
20
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
21
|
+
else
|
22
|
+
end
|
23
|
+
else
|
24
|
+
end
|
25
|
+
end
|
data/test/pinny_test.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'pinny'
|
3
|
+
|
4
|
+
class PinnyTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
test "to_pinyin - simple" do
|
7
|
+
assert_equal "Ní hǎo", Pinny.to_pinyin("Ni2 hao3")
|
8
|
+
end
|
9
|
+
|
10
|
+
test "to_pinyin - capital A" do
|
11
|
+
assert_equal "Ai", Pinny.to_pinyin("Ai4")
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pinny
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matthew Rudy Jacobs
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-10-13 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description:
|
23
|
+
email: MatthewRudyJacobs@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- pinny.gemspec
|
32
|
+
- Rakefile
|
33
|
+
- test/pinny_test.rb
|
34
|
+
- test/test_helper.rb
|
35
|
+
- lib/pinny.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/matthewrudy/pinny
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.4.2
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Pinyin conversion tool
|
70
|
+
test_files: []
|
71
|
+
|