cnname 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/.gitignore +18 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +5 -0
- data/cnname.gemspec +23 -0
- data/data/compound_surnames.json +314 -0
- data/data/singleton_surnames.json +1630 -0
- data/lib/cnname.rb +7 -0
- data/lib/cnname/name.rb +36 -0
- data/lib/cnname/version.rb +3 -0
- data/rvmrc.example +1 -0
- data/spec/cnname/name_spec.rb +44 -0
- data/spec/spec_helper.rb +7 -0
- metadata +94 -0
data/lib/cnname.rb
ADDED
data/lib/cnname/name.rb
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module Cnname
|
|
2
|
+
class Name
|
|
3
|
+
attr_reader :first_name, :last_name
|
|
4
|
+
|
|
5
|
+
def initialize(name)
|
|
6
|
+
@first_name, @last_name = parse(name)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
def parse(name)
|
|
11
|
+
if hanzi_compound_surnames.include?(name[0..1])
|
|
12
|
+
[name[2..-1], name[0..1]]
|
|
13
|
+
elsif hanzi_singleton_surnames.include?(name[0])
|
|
14
|
+
[name[1..-1], name[0]]
|
|
15
|
+
else
|
|
16
|
+
raise 'Invalid Chinese name'
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def singleton_surnames
|
|
21
|
+
@singleton_surnames ||= Oj.load(File.open(File.join(File.dirname(__FILE__), '../../data/singleton_surnames.json')))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def compound_surnames
|
|
25
|
+
@compound_surnames ||= Oj.load(File.open(File.join(File.dirname(__FILE__), '../../data/compound_surnames.json')))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def hanzi_singleton_surnames
|
|
29
|
+
@hanzi_singleton_surnames ||= singleton_surnames.collect { |n| n['hanzi'] }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def hanzi_compound_surnames
|
|
33
|
+
@hanzi_compound_surnames ||= compound_surnames.collect { |n| n['hanzi'] }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
data/rvmrc.example
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rvm --create use 1.9.3@cnname
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
describe Cnname::Name do
|
|
5
|
+
subject { Cnname::Name.new(name) }
|
|
6
|
+
|
|
7
|
+
context 'when given a single surname' do
|
|
8
|
+
context 'and the first name only has one word' do
|
|
9
|
+
let(:name) { '张三' }
|
|
10
|
+
|
|
11
|
+
its(:first_name) { should == '三' }
|
|
12
|
+
its(:last_name) { should == '张' }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
context 'and the first name has more than one word' do
|
|
16
|
+
let(:name) { '张三丰' }
|
|
17
|
+
|
|
18
|
+
its(:first_name) { should == '三丰' }
|
|
19
|
+
its(:last_name) { should == '张' }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context 'when given a compound surname' do
|
|
24
|
+
context 'and the first name only has one word' do
|
|
25
|
+
let(:name) { '诸葛亮' }
|
|
26
|
+
|
|
27
|
+
its(:first_name) { should == '亮' }
|
|
28
|
+
its(:last_name) { should == '诸葛' }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
context 'and the first name has more than one word' do
|
|
32
|
+
let(:name) { '西门吹雪' }
|
|
33
|
+
|
|
34
|
+
its(:first_name) { should == '吹雪' }
|
|
35
|
+
its(:last_name) { should == '西门' }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
context 'when given an invalid surname' do
|
|
40
|
+
it 'raises an error' do
|
|
41
|
+
expect { Cnname::Name.new('不存在') }.to raise_error
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cnname
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Tower He
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-11-25 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: oj
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '0'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rspec
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
description: cnname provides support to handle the names of Chinese
|
|
47
|
+
email:
|
|
48
|
+
- towerhe@gmail.com
|
|
49
|
+
executables: []
|
|
50
|
+
extensions: []
|
|
51
|
+
extra_rdoc_files: []
|
|
52
|
+
files:
|
|
53
|
+
- .gitignore
|
|
54
|
+
- .rspec
|
|
55
|
+
- Gemfile
|
|
56
|
+
- LICENSE.txt
|
|
57
|
+
- README.md
|
|
58
|
+
- Rakefile
|
|
59
|
+
- cnname.gemspec
|
|
60
|
+
- data/compound_surnames.json
|
|
61
|
+
- data/singleton_surnames.json
|
|
62
|
+
- lib/cnname.rb
|
|
63
|
+
- lib/cnname/name.rb
|
|
64
|
+
- lib/cnname/version.rb
|
|
65
|
+
- rvmrc.example
|
|
66
|
+
- spec/cnname/name_spec.rb
|
|
67
|
+
- spec/spec_helper.rb
|
|
68
|
+
homepage: https://github.com/towerhe/cnname
|
|
69
|
+
licenses: []
|
|
70
|
+
post_install_message:
|
|
71
|
+
rdoc_options: []
|
|
72
|
+
require_paths:
|
|
73
|
+
- lib
|
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
|
+
none: false
|
|
76
|
+
requirements:
|
|
77
|
+
- - ! '>='
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: '0'
|
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
|
+
none: false
|
|
82
|
+
requirements:
|
|
83
|
+
- - ! '>='
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
requirements: []
|
|
87
|
+
rubyforge_project:
|
|
88
|
+
rubygems_version: 1.8.24
|
|
89
|
+
signing_key:
|
|
90
|
+
specification_version: 3
|
|
91
|
+
summary: cnname provides support to handle the names of Chinese
|
|
92
|
+
test_files:
|
|
93
|
+
- spec/cnname/name_spec.rb
|
|
94
|
+
- spec/spec_helper.rb
|