faker-japanese 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/LICENSE.naist-jdic +31 -0
- data/README.md +116 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/example/your_friends.rb +8 -0
- data/lib/faker/japanese.rb +5 -0
- data/lib/faker/japanese/name.rb +32051 -0
- data/script/generate.rb +78 -0
- data/spec/.rspec +1 -0
- data/spec/name_spec.rb +45 -0
- data/spec/spec_helper.rb +6 -0
- metadata +112 -0
data/script/generate.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
DEFAULT_DIC_PATH = '/var/lib/chasen/dic/naist-jdic-utf8/naist-jdic.dic'
|
5
|
+
NAME_CLASS_PATH = File.dirname(__FILE__) + '/../lib/faker/japanese/name.rb'
|
6
|
+
|
7
|
+
def parse_dic(dic_path)
|
8
|
+
names = Hash.new {|h,k| h[k] = [] }
|
9
|
+
File.open(dic_path).each do |line|
|
10
|
+
if type = line[/\(品詞 \(名詞 固有名詞 人名 (姓|名)\)\)/, 1]
|
11
|
+
key = type == '姓' ? :last : :first
|
12
|
+
name = line[/\(\(見出し語 \((.+) \d+\)\)/, 1]
|
13
|
+
yomi = line[/\(読み (.+?)\)/, 1]
|
14
|
+
if match = yomi[/^\{(.+)\}$/, 1]
|
15
|
+
yomis = match.split('/')
|
16
|
+
yomis.each do |yomi|
|
17
|
+
names[key] << [name, yomi]
|
18
|
+
end
|
19
|
+
else
|
20
|
+
names[key] << [name, yomi]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
names
|
25
|
+
end
|
26
|
+
|
27
|
+
def write_class(names)
|
28
|
+
erb = ERB.new(DATA.read, nil, '-')
|
29
|
+
file = File.open(NAME_CLASS_PATH, 'w')
|
30
|
+
file.write(erb.result(binding))
|
31
|
+
end
|
32
|
+
|
33
|
+
def main
|
34
|
+
dic_path = ARGV[0] || DEFAULT_DIC_PATH
|
35
|
+
names = parse_dic(dic_path)
|
36
|
+
write_class(names)
|
37
|
+
end
|
38
|
+
|
39
|
+
main
|
40
|
+
|
41
|
+
__END__
|
42
|
+
|
43
|
+
module Faker
|
44
|
+
module Japanese
|
45
|
+
module Name
|
46
|
+
extend self
|
47
|
+
|
48
|
+
<%- [:first, :last].each do |key| -%>
|
49
|
+
@<%= key %>_names = [
|
50
|
+
<%- names[key].each do |name, yomi| -%>
|
51
|
+
['<%= name %>', '<%= yomi %>'],
|
52
|
+
<%- end -%>
|
53
|
+
]
|
54
|
+
<%- end -%>
|
55
|
+
|
56
|
+
def name
|
57
|
+
first, last = first_name, last_name
|
58
|
+
name = [last, first].join(' ')
|
59
|
+
set_yomi(name, last.yomi + ' ' + first.yomi)
|
60
|
+
name
|
61
|
+
end
|
62
|
+
|
63
|
+
[:first_name, :last_name].each do |m|
|
64
|
+
define_method m do
|
65
|
+
chosen = instance_variable_get("@#{m}s").rand
|
66
|
+
name = chosen[0]
|
67
|
+
set_yomi(name, chosen[1])
|
68
|
+
name
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def set_yomi(name, yomi)
|
73
|
+
name.instance_variable_set('@yomi', yomi)
|
74
|
+
def name.yomi; @yomi end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/name_spec.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Faker::Japanese::Name do
|
4
|
+
before do
|
5
|
+
@expect = {
|
6
|
+
:first_name => [['良子', 'リョウコ'], ['武', 'タケシ'], ['修三', 'シュウゾウ']],
|
7
|
+
:last_name => [['木村', 'キムラ'], ['田中', 'タナカ'], ['佐藤', 'サトウ']]
|
8
|
+
}
|
9
|
+
@expect.each do |k, v|
|
10
|
+
Faker::Japanese::Name.instance_variable_set("@#{k}s", v)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "#name" do
|
15
|
+
expect = {
|
16
|
+
:first_name => ['太郎', 'タロウ'],
|
17
|
+
:last_name => ['安藤', 'アンドウ']
|
18
|
+
}
|
19
|
+
expect.each do |k, v|
|
20
|
+
name = v[0]
|
21
|
+
name.stub!(:yomi => v[1])
|
22
|
+
Faker::Japanese::Name.should_receive(k).and_return(name)
|
23
|
+
end
|
24
|
+
name = Faker::Japanese::Name.name
|
25
|
+
name.should == "#{expect[:last_name][0]} #{expect[:first_name][0]}"
|
26
|
+
name.yomi.should == "#{expect[:last_name][1]} #{expect[:first_name][1]}"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "#first_name, #last_name" do
|
30
|
+
@expect.each do |k, v|
|
31
|
+
names = v.map {|n| n[0] }
|
32
|
+
yomis = v.map {|n| n[1] }
|
33
|
+
name = Faker::Japanese::Name.send(k)
|
34
|
+
names.should include name
|
35
|
+
yomis.should include name.yomi
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "#set_yomi" do
|
40
|
+
expect = 'ヨミ'
|
41
|
+
o = Object.new
|
42
|
+
Faker::Japanese::Name.set_yomi(o, expect)
|
43
|
+
o.yomi.should == expect
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faker-japanese
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 0.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- tily
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-03 00:00:00 +09:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: faker
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 13
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 2
|
47
|
+
- 9
|
48
|
+
version: 1.2.9
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Faker in Japanese
|
52
|
+
email: tily05@gmail.com
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files:
|
58
|
+
- LICENSE
|
59
|
+
- LICENSE.naist-jdic
|
60
|
+
- README.md
|
61
|
+
files:
|
62
|
+
- .document
|
63
|
+
- .gitignore
|
64
|
+
- LICENSE
|
65
|
+
- LICENSE.naist-jdic
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- VERSION
|
69
|
+
- example/your_friends.rb
|
70
|
+
- lib/faker/japanese.rb
|
71
|
+
- lib/faker/japanese/name.rb
|
72
|
+
- script/generate.rb
|
73
|
+
- spec/.rspec
|
74
|
+
- spec/name_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
has_rdoc: true
|
77
|
+
homepage: http://github.com/tily/ruby-faker-japanese
|
78
|
+
licenses: []
|
79
|
+
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options:
|
82
|
+
- --charset=UTF-8
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.3.7
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Faker in Japanese
|
110
|
+
test_files:
|
111
|
+
- spec/name_spec.rb
|
112
|
+
- spec/spec_helper.rb
|