romajinizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -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
data/Gemfile.lock ADDED
@@ -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)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Joe Ellis
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.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = romajinizer
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to romajinizer
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Joe Ellis. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
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 = "romajinizer"
16
+ gem.homepage = "http://github.com/joeellis/romajinizer"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A gem for converting between hiragana, katakana, and romaji}
19
+ gem.description = %Q{A gem for converting between hiragana, katakana, and romaji}
20
+ gem.email = "joe@squarefour.net"
21
+ gem.authors = ["Joe Ellis"]
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
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "romajinizer #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,312 @@
1
+ # coding: utf-8
2
+
3
+ #
4
+ # kana2rom.rb
5
+ # A Ruby module for converting between hiragana, katakana and romaji.
6
+ #
7
+ # ---------------------------------------------------------------------------------
8
+ # K.Kodama 2002.06
9
+ # This script is distributed freely in the sense of GNU General Public License.
10
+ # http://www.gnu.org/licenses/gpl.html
11
+ #
12
+ # ---------------------------------------------------------------------------------
13
+ # Paul Chapman (paul [a../t] longweekendmobile 2010-04-01)
14
+ # Repaired script to work with modern Ruby versions (1.86+), added comments,
15
+ # made it support gaijin friendly transliterations!
16
+ # ---------------------------------------------------------------------------------
17
+ # USAGE
18
+ #
19
+ # Include kana2rom
20
+ #
21
+ # kana2rom(str) かな --> ロ-マ字 変換 / hira/katakana ->> romaji conv
22
+ # rom2kata(str) ロ-マ字 --> 片仮名 変換 / romaji --> katakana conv
23
+ # rom2hira(str) ロ-マ字 --> 平仮名 変換 / romaji --> hiragana conv
24
+ # hira2kata(str) 平仮名 --> 片仮名 変換 / hiragana --> katakana conv
25
+ # kata2hira(str) 片仮名 --> 平仮名 変換 / katakana ->> hiragana conv
26
+ # kana2kana(str) attempts either to either, returns unique strings only
27
+ #
28
+ # ---------------------------------------------------------------------------------
29
+
30
+ module Kana2rom
31
+
32
+ Kana2romH={
33
+ "ア"=>"a", "イ"=>"i", "ウ"=>"u", "エ"=>"e","オ"=>"o",
34
+ "あ"=>"a", "い"=>"i", "う"=>"u", "え"=>"e","お"=>"o",
35
+ "カ"=>"ka", "キ"=>"ki", "ク"=>"ku", "ケ"=>"ke", "コ"=>"ko",
36
+ "か"=>"ka", "き"=>"ki", "く"=>"ku", "け"=>"ke", "こ"=>"ko",
37
+ "ガ"=>"ga", "ギ"=>"gi", "グ"=>"gu", "ゲ"=>"ge", "ゴ"=>"go",
38
+ "が"=>"ga", "ぎ"=>"gi", "ぐ"=>"gu", "げ"=>"ge", "ご"=>"go",
39
+ "サ"=>"sa", "シ"=>"si", "ス"=>"su", "セ"=>"se", "ソ"=>"so",
40
+ "さ"=>"sa", "し"=>"shi","す"=>"su", "せ"=>"se", "そ"=>"so",
41
+ "ザ"=>"za", "ジ"=>"ji", "ズ"=>"zu", "ゼ"=>"ze", "ゾ"=>"zo",
42
+ "ざ"=>"za", "じ"=>"ji", "ず"=>"zu", "ぜ"=>"ze", "ぞ"=>"zo",
43
+ "タ"=>"ta", "チ"=>"chi","ツ"=>"tsu","テ"=>"te", "ト"=>"to",
44
+ "た"=>"ta", "ち"=>"chi","つ"=>"tsu","て"=>"te", "と"=>"to",
45
+ "ダ"=>"da", "ヂ"=>"dji","ヅ"=>"dzu","デ"=>"de", "ド"=>"do",
46
+ "だ"=>"da", "ぢ"=>"dji","づ"=>"dzu","で"=>"de", "ど"=>"do",
47
+ "ナ"=>"na", "ニ"=>"ni", "ヌ"=>"nu", "ネ"=>"ne", "ノ"=>"no",
48
+ "な"=>"na", "に"=>"ni", "ぬ"=>"nu", "ね"=>"ne", "の"=>"no",
49
+ "ハ"=>"ha", "ヒ"=>"hi", "フ"=>"fu", "ヘ"=>"he", "ホ"=>"ho",
50
+ "は"=>"ha", "ひ"=>"hi", "ふ"=>"fu", "へ"=>"he", "ほ"=>"ho",
51
+ "バ"=>"ba", "ビ"=>"bi", "ブ"=>"bu", "ベ"=>"be", "ボ"=>"bo",
52
+ "ば"=>"ba", "び"=>"bi", "ぶ"=>"bu", "べ"=>"be", "ぼ"=>"bo",
53
+ "パ"=>"pa", "ピ"=>"pi", "プ"=>"pu", "ペ"=>"pe", "ポ"=>"po",
54
+ "ぱ"=>"pa", "ぴ"=>"pi", "ぷ"=>"pu", "ぺ"=>"pe", "ぽ"=>"po",
55
+ "マ"=>"ma", "ミ"=>"mi", "ム"=>"mu", "メ"=>"me", "モ"=>"mo",
56
+ "ま"=>"ma", "み"=>"mi", "む"=>"mu", "め"=>"me", "も"=>"mo",
57
+ "ヤ"=>"ya", "ユ"=>"yu", "ヨ"=>"yo",
58
+ "や"=>"ya", "ゆ"=>"yu", "よ"=>"yo",
59
+ "ラ"=>"ra", "リ"=>"ri", "ル"=>"ru","レ"=>"re","ロ"=>"ro",
60
+ "ら"=>"ra", "り"=>"ri", "る"=>"ru","れ"=>"re","ろ"=>"ro",
61
+ "ワ"=>"wa", "ヰ"=>"wi", "ヱ"=>"we", "ヲ"=>"wo", "ン"=>"nn",
62
+ "わ"=>"wa", "ゐ"=>"wi", "ゑ"=>"we", "を"=>"wo", "ん"=>"nn",
63
+ "ァ"=>"xa", "ィ"=>"xi", "ゥ"=>"xu", "ェ"=>"xe", "ォ"=>"xo",
64
+ "ぁ"=>"xa", "ぃ"=>"xi", "ぅ"=>"xu", "ぇ"=>"xe", "ぉ"=>"xo",
65
+ "ッ"=>"xtsu","ャ"=>"xya", "ュ"=>"xyu", "ョ"=>"xyo",
66
+ "っ"=>"xtsu","ゃ"=>"xya", "ゅ"=>"xyu", "ょ"=>"xyo",
67
+ "ヴ"=>"vu", "ヵ"=>"xka","ヶ"=>"ga","ヮ"=>"xwa",
68
+ "ゎ"=>"xwa",
69
+ "ー"=>"-", "−"=>"-", "゛"=>'"', "゜"=>"'", "、"=>",", "。"=>".",
70
+ ":"=>":", " " => " ", "@" => "@", "(" => "(", ")" => ")",
71
+ " " => " "
72
+ }
73
+
74
+ Kana2romH2={
75
+ "てぃ" => "ti", "でぃ" => "di"
76
+ }
77
+ # 1 character romaji patterns
78
+ Rom2KataH1={
79
+ "a"=>"ア", "i"=>"イ", "u"=>"ウ", "e"=>"エ", "o"=>"オ", "-"=>"ー"
80
+ }
81
+
82
+ # 2 character romaji patterns
83
+ Rom2KataH2={
84
+ "xa"=>"ァ", "xi"=>"ィ", "xu"=>"ゥ", "xe"=>"ェ", "xo"=>"ォ",
85
+ "ka"=>"カ", "ki"=>"キ", "ku"=>"ク", "ke"=>"ケ", "ko"=>"コ",
86
+ "ca"=>"カ", "cu"=>"ク", "co"=>"コ",
87
+ "ga"=>"ガ", "gi"=>"ギ", "gu"=>"グ", "ge"=>"ゲ", "go"=>"ゴ",
88
+ "sa"=>"サ", "si"=>"シ", "su"=>"ス", "se"=>"セ", "so"=>"ソ",
89
+ "za"=>"ザ", "zi"=>"ジ", "zu"=>"ズ", "ze"=>"ゼ", "zo"=>"ゾ",
90
+ "ja"=>"ジャ","ji"=>"ジ", "ju"=>"ジュ","je"=>"ジェ","jo"=>"ジョ",
91
+ "ta"=>"タ", "ti"=>"チ", "tsu"=>"ツ", "te"=>"テ", "to"=>"ト",
92
+ "da"=>"ダ", "di"=>"ヂ", "du"=>"ヅ", "de"=>"デ", "do"=>"ド",
93
+ "na"=>"ナ", "ni"=>"ニ", "nu"=>"ヌ", "ne"=>"ネ", "no"=>"ノ",
94
+ "ha"=>"ハ", "hi"=>"ヒ", "hu"=>"フ", "he"=>"ヘ", "ho"=>"ホ",
95
+ "ba"=>"バ", "bi"=>"ビ", "bu"=>"ブ", "be"=>"ベ", "bo"=>"ボ",
96
+ "pa"=>"パ", "pi"=>"ピ", "pu"=>"プ", "pe"=>"ペ", "po"=>"ポ",
97
+ "va"=>"ヴァ","vi"=>"ヴィ","vu"=>"ヴ", "ve"=>"ヴェ","vo"=>"ヴォ",
98
+ "fa"=>"ファ","fi"=>"フィ","fu"=>"フ", "fe"=>"フェ","fo"=>"フォ",
99
+ "ma"=>"マ", "mi"=>"ミ", "mu"=>"ム", "me"=>"メ", "mo"=>"モ",
100
+ "ya"=>"ヤ", "yi"=>"イ", "yu"=>"ユ", "ye"=>"イェ", "yo"=>"ヨ",
101
+ "ra"=>"ラ", "ri"=>"リ", "ru"=>"ル", "re"=>"レ", "ro"=>"ロ",
102
+ "la"=>"ラ", "li"=>"リ", "lu"=>"ル", "le"=>"レ", "lo"=>"ロ",
103
+ "wa"=>"ワ", "wi"=>"ヰ", "wu"=>"ウ", "we"=>"ヱ", "wo"=>"ヲ",
104
+ "nn"=>"ン"
105
+ }
106
+
107
+ # 3 character romaji patterns
108
+ Rom2KataH3={
109
+ "tsu"=>"ツ",
110
+ "xka"=>"ヵ", "xke"=>"ヶ",
111
+ "xwa"=>"ヮ", "xtsu"=>"ッ", "xya"=>"ャ", "xyu"=>"ュ", "xyo"=>"ョ",
112
+ "kya"=>"キャ", "kyi"=>"キィ", "kyu"=>"キュ", "kye"=>"キェ", "kyo"=>"キョ",
113
+ "gya"=>"ギャ", "gyi"=>"ギィ", "gyu"=>"ギュ", "gye"=>"ギェ", "gyo"=>"ギョ",
114
+ "sya"=>"シャ", "syi"=>"シィ", "syu"=>"シュ", "sye"=>"シェ", "syo"=>"ショ",
115
+ "sha"=>"シャ", "shi"=>"シ", "shu"=>"シュ", "she"=>"シェ", "sho"=>"ショ",
116
+ "zya"=>"ジャ", "zyi"=>"ジィ", "zyu"=>"ジュ", "zye"=>"ジェ", "zyo"=>"ジョ",
117
+ "jya"=>"ジャ", "jyi"=>"ジィ", "jyu"=>"ジュ", "jye"=>"ジェ", "jyo"=>"ジョ",
118
+ "tya"=>"チャ", "tyi"=>"チィ", "tyu"=>"チュ", "tye"=>"チェ", "tyo"=>"チョ",
119
+ "cya"=>"チャ", "cyi"=>"チィ", "cyu"=>"チュ", "cye"=>"チェ", "cyo"=>"チョ",
120
+ "cha"=>"チャ", "chi"=>"チ", "chu"=>"チュ", "che"=>"チェ", "cho"=>"チョ",
121
+ "tha"=>"テャ", "thi"=>"ティ", "thu"=>"テュ", "the"=>"テェ", "tho"=>"テョ",
122
+ "dya"=>"ヂャ", "dyi"=>"ヂィ", "dyu"=>"ヂュ", "dye"=>"ヂェ", "dyo"=>"ヂョ",
123
+ "dha"=>"デャ", "dhi"=>"ディ", "dhu"=>"デュ", "dhe"=>"デェ", "dho"=>"デョ",
124
+ "nya"=>"ニャ", "nyi"=>"ニィ", "nyu"=>"ニュ", "nye"=>"ニェ", "nyo"=>"ニョ",
125
+ "hya"=>"ヒャ", "hyi"=>"ヒィ", "hyu"=>"ヒュ", "hye"=>"ヒェ", "hyo"=>"ヒョ",
126
+ "bya"=>"ビャ", "byi"=>"ビィ", "byu"=>"ビュ", "bye"=>"ビェ", "byo"=>"ビョ",
127
+ "pya"=>"ピャ", "pyi"=>"ピィ", "pyu"=>"ピュ", "pye"=>"ピェ", "pyo"=>"ピョ",
128
+ "mya"=>"ミャ", "myi"=>"ミィ", "myu"=>"ミュ", "mye"=>"ミェ", "myo"=>"ミョ",
129
+ "rya"=>"リャ", "ryi"=>"リィ", "ryu"=>"リュ", "rye"=>"リェ", "ryo"=>"リョ",
130
+ "lya"=>"リャ", "lyi"=>"リィ", "lyu"=>"リュ", "lye"=>"リェ", "lyo"=>"リョ"
131
+ }
132
+
133
+ Kata2hiraH={
134
+ "ア"=>"あ", "イ"=>"い", "ウ"=>"う", "エ"=>"え", "オ"=>"お",
135
+ "カ"=>"か", "キ"=>"き", "ク"=>"く", "ケ"=>"け", "コ"=>"こ",
136
+ "ガ"=>"が", "ギ"=>"ぎ", "グ"=>"ぐ", "ゲ"=>"げ", "ゴ"=>"ご",
137
+ "サ"=>"さ", "シ"=>"し", "ス"=>"す", "セ"=>"せ", "ソ"=>"そ",
138
+ "ザ"=>"ざ", "ジ"=>"じ", "ズ"=>"ず", "ゼ"=>"ぜ", "ゾ"=>"ぞ",
139
+ "タ"=>"た", "チ"=>"ち", "ツ"=>"つ", "テ"=>"て", "ト"=>"と",
140
+ "ダ"=>"だ", "ヂ"=>"ぢ", "ヅ"=>"づ", "デ"=>"で", "ド"=>"ど",
141
+ "ナ"=>"な", "ニ"=>"に", "ヌ"=>"ぬ", "ネ"=>"ね", "ノ"=>"の",
142
+ "ハ"=>"は", "ヒ"=>"ひ", "フ"=>"ふ", "ヘ"=>"へ", "ホ"=>"ほ",
143
+ "バ"=>"ば", "ビ"=>"び", "ブ"=>"ぶ", "ベ"=>"べ", "ボ"=>"ぼ",
144
+ "パ"=>"ぱ", "ピ"=>"ぴ", "プ"=>"ぷ", "ペ"=>"ぺ", "ポ"=>"ぽ",
145
+ "マ"=>"ま", "ミ"=>"み", "ム"=>"む", "メ"=>"め", "モ"=>"も",
146
+ "ヤ"=>"や", "ユ"=>"ゆ", "ヨ"=>"よ",
147
+ "ラ"=>"ら", "リ"=>"り", "ル"=>"る", "レ"=>"れ", "ロ"=>"ろ",
148
+ "ワ"=>"わ", "ヰ"=>"ゐ", "ヱ"=>"ゑ", "ヲ"=>"を", "ン"=>"ん",
149
+ "ァ"=>"ぁ", "ィ"=>"ぃ", "ゥ"=>"ぅ", "ェ"=>"ぇ", "ォ"=>"ぉ",
150
+ "ッ"=>"っ", "ャ"=>"ゃ", "ュ"=>"ゅ", "ョ"=>"ょ",
151
+ "ヴ"=>"う゛", "ヵ"=>"か", "ヶ"=>"が", "ヮ"=>"ゎ"
152
+ }
153
+
154
+ Hira2kataH={}; Kata2hiraH.each_pair{|k,v| Hira2kataH[v]=k}; Hira2kataH["か"]="カ"; Hira2kataH["が"]="ガ"
155
+
156
+ def kana2rom
157
+ s=""
158
+ self.each_char do |c|
159
+ if (Kana2romH.key?(c))
160
+ s += Kana2romH[c]
161
+ else
162
+ s += c
163
+ end
164
+ end
165
+
166
+ s=s.gsub(/(k)([aiueo])(")/,'g\2').gsub(/(s)([aiueo])(")/,'z\2').gsub(/(t)([aiueo])(")/,'d\2')
167
+ s=s.gsub(/(h)([aiueo])(")/,'b\2').gsub(/([fh])([aiueo])(')/,'p\2').gsub(/u"/,'vu') # [半]濁点゛゜
168
+ #---------------------------------------------------------
169
+ s=s.gsub(/\s(xtsu)?\s/,'xtsu') # Remove spaces before/after hanging 'っ'
170
+ #---------------------------------------------------------
171
+ sw=s;
172
+ while nil!=sw.gsub!(/(xtsu)([ckgszjtdhfbpmyrwnv])/,'\2\2') do; s=sw; end # ッカ-->xtsuka-->kka
173
+ #---------------------------------------------------------
174
+ # Compound Phoneme Pattern Rollbacks
175
+ # NB: Uses regex backrefs like "\1y\3" where \1 = 1st capture grp, y='y' and \3 = 3rd capture grp
176
+ #---------------------------------------------------------
177
+ s=s.gsub(/( +x)(.*)/,'x\2') # Avoid hanging chisaii moji due to leading spaces
178
+ s=s.gsub(/(ch)(ixy)([aueo])/,'\1\3') # チョ-->chixyo-->cho
179
+ s=s.gsub(/([kgszjtdnhfbpmr])(ixy)([auo])/,'\1y\3') # キャ-->kixya-->kya
180
+ s=s.gsub(/([kgszjtdnhfbpmr])(ix)([ie])/,'\1y\3') # キィ-->kixi-->kyi
181
+ #---------------------------------------------------------
182
+ s=s.gsub(/(sh)(y)([aueo])/,'\1\3') # シュ-->shyu-->shu
183
+ s=s.gsub(/(j)(y)([aueo])/,'\1\3') # ジュ-->jyu-->ju
184
+ #---------------------------------------------------------
185
+ s=s.gsub(/([td])(exy)([aueo])/,'\1h\3') # テャ-->texya-->tha
186
+ s=s.gsub(/([td])(ex)([ie])/,'\1\3') # ティ-->texi-->ti
187
+ s=s.gsub(/([td])(oxu)/,'\1oo') # ドゥ-->toxu-->too
188
+ s=s.gsub(/(tsu)(x)([aiueo])/,'ts\3') # ツァ-->tsuxa-->tsa
189
+ s=s.gsub(/([d])(oxy)/,'\1o\'y') # ドュ-->doxyu-->doyu
190
+ #---------------------------------------------------------
191
+ s=s.gsub(/(vux)([aieo])/ ,'v\2') # ヴァヴィヴェヴォ, ヴァ-->vuxa-->va
192
+ s=s.gsub(/(vuxy)([aueo])/ ,'vy\2') # ヴュ-->vuxyu-->vyu
193
+ s=s.gsub(/(ixe)/ ,'iye') # イェ-->ixe-->iye
194
+ s=s.gsub(/(hoxe)/ ,'howe') # ホェ-->hoxe-->howe
195
+ s=s.gsub(/(fux)([aieo])/ ,'f\2') # ファフィフェフォ, ファ-->fuxa-->fa
196
+ s=s.gsub(/(fuxy)([aueo])/,'fy\2') # フュ-->fuxyu-->fyu
197
+ s=s.gsub(/(ux)([ieo])/, 'w\2') # ウァウィウェ, ウァ-->uxa-->wa
198
+ #---------------------------------------------------------
199
+ s=s.strip.gsub(/(xtsu)$/,'h!') # Recombine hanging 'っ' followed by EOL
200
+ s=s.gsub(/([aiueo]?)(\-)/, '\1\1') # Replace boubiki chars and double preceding vowel
201
+ #---------------------------------------------------------
202
+ # Cleanup specifically for source strings that contain spaces!
203
+ s=s.gsub(/( +)([^a-z|A-z])/, '\2') # Remove spaces before any non-alphabetical char
204
+ s=s.gsub(/(n')/,'n') # ン-->nn-->n
205
+ s=s.gsub(/(nn)/,'n') # ン-->nn-->n
206
+ s=s.gsub(/( n)[^a-z|A-Z]?$/,'n') # Fix "n" appearing as separate word
207
+ s=s.gsub(/\s{2,}/, ' ') # Remove duplicate spaces!
208
+ #---------------------------------------------------------
209
+ return s
210
+ end
211
+
212
+ def rom2kata
213
+ ## THIS LINE DOES NOT WORK IN RECENT RUBY VERSIONS!!! r=""; w=[]; chars=str.split(//e)
214
+ result=""
215
+ word_buffer=[]
216
+ chars=self.each_char.collect{|c| c}
217
+ loop do
218
+ case word_buffer.size
219
+ ##### When 0 characters in the buffer
220
+ when 0 then
221
+ if chars.size > 0
222
+ word_buffer.push(chars.shift)
223
+ else
224
+ return result
225
+ end
226
+ ##### Patterns with 1 roman character
227
+ when 1 then
228
+ if word_buffer[0] =~ /[aiueo-]/
229
+ result += Rom2KataH1[word_buffer[0]]
230
+ word_buffer = [] # a-->ア
231
+ elsif word_buffer[0] =~ /[xkcgszjtdnhbpvfmyrlw']/
232
+ if chars.size > 0
233
+ word_buffer.push(chars.shift)
234
+ else
235
+ return result + (word_buffer[0].gsub(/n/,"ン"))
236
+ end
237
+ else
238
+ result += word_buffer.shift
239
+ end
240
+ ##### Patterns with 2 roman characters
241
+ when 2 then
242
+ if Rom2KataH2.key?(word_buffer.join)
243
+ result += Rom2KataH2[word_buffer.join]
244
+ word_buffer = []
245
+ elsif word_buffer.join =~ /([kgszjtcdnhbpmrl]y)|([stcd]h)|ts|(x[wytk])/ # goto 3
246
+ if chars.size > 0
247
+ # Consume next letter from source array
248
+ word_buffer.push(chars.shift)
249
+ else
250
+ return result + (word_buffer.join.gsub(/n/,"ン"))
251
+ end
252
+ elsif word_buffer.join == "n'"
253
+ result += "ン"
254
+ word_buffer.shift(2) # n'--> ン
255
+ elsif word_buffer[0] == "n"
256
+ result += "ン"
257
+ word_buffer.shift # nk-->ンk
258
+ elsif word_buffer[0] == word_buffer[1]
259
+ result += "ッ"
260
+ word_buffer.shift # kk-->ッk
261
+ else
262
+ result += word_buffer.shift;
263
+ end
264
+ ##### Patterns with 3 roman characters
265
+ when 3 then
266
+ if Rom2KataH3.key?(word_buffer.join)
267
+ result += Rom2KataH3[word_buffer.join]
268
+ word_buffer=[]
269
+ elsif word_buffer[0] == "n"
270
+ result += "ン"
271
+ word_buffer.shift
272
+ else
273
+ result += word_buffer.shift
274
+ end
275
+ end
276
+ end
277
+ end
278
+
279
+ def kata2hira(str)
280
+ s=""; str.each_char{|c| s+=( Kata2hiraH.key?(c) ? Kata2hiraH[c] : c )}
281
+ return s
282
+ end
283
+
284
+ def hira2kata(str)
285
+ s=""; str.each_char{|c|if(Hira2kataH.key?(c))then s+=Hira2kataH[c];else s+=c; end}
286
+ return s
287
+ end
288
+
289
+ # Added by Paul 2009-05-12 22:31
290
+ def kana2kana(str1)
291
+ result = []
292
+ str2 = Kana2rom::hira2kata(str1)
293
+ str3 = Kana2rom::kata2hira(str1)
294
+ result << str1
295
+ result << str2 if str2.length > 0 and str1 !=str2
296
+ result << str3 if str3.length > 0 and str2 !=str3 and str3 != str1
297
+ return result
298
+ end
299
+
300
+ def rom2hira
301
+ return kata2hira(rom2kata)
302
+ end
303
+
304
+ alias :to_hiragana :rom2hira
305
+ alias :to_katakana :rom2kata
306
+ alias :to_romaji :kana2rom
307
+
308
+ end
309
+
310
+ class String
311
+ include Kana2rom
312
+ end
@@ -0,0 +1,24 @@
1
+ #coding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe "Romajinizer" do
5
+ it "should convert romaji to hiragana properly" do
6
+ "tsukue".to_hiragana.should == "つくえ"
7
+ "kinnyoubi".to_hiragana.should == "きんようび"
8
+ "kin'youbi".to_hiragana.should == "きんようび"
9
+ "konnya".to_hiragana.should == "こんや"
10
+ "konnnichi".to_hiragana.should == "こんにち"
11
+ "kaetta".to_hiragana.should == "かえった"
12
+ end
13
+
14
+ it "should convert romaji to katakana properly" do
15
+ "tsukue".to_katakana.should == "ツクエ"
16
+ end
17
+
18
+ it "should convert kana to romaji properly" do
19
+ "つくえ".to_romaji.should == "tsukue"
20
+ "きんようび".to_romaji.should == "kinyoubi"
21
+ "こんや".to_romaji.should == "konya"
22
+ "こんにち".to_romaji.should == "konnichi"
23
+ end
24
+ 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 'romajinizer'
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,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: romajinizer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Joe Ellis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-09 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.2
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ description: A gem for converting between hiragana, katakana, and romaji
61
+ email: joe@squarefour.net
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .rspec
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - lib/romajinizer.rb
79
+ - spec/romajinizer_spec.rb
80
+ - spec/spec_helper.rb
81
+ has_rdoc: true
82
+ homepage: http://github.com/joeellis/romajinizer
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+
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
+ hash: -2520419241099903732
96
+ segments:
97
+ - 0
98
+ version: "0"
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
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.5.0
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: A gem for converting between hiragana, katakana, and romaji
112
+ test_files:
113
+ - spec/romajinizer_spec.rb
114
+ - spec/spec_helper.rb