roka 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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +73 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/roka +10 -0
- data/lib/roka.rb +12 -0
- data/lib/roka/converter.rb +177 -0
- data/lib/roka/version.rb +3 -0
- data/roka.gemspec +25 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 497e882b9a0a284194ebb131fbb50a00c5ed3fd3
|
4
|
+
data.tar.gz: a46eef1091e38182f23dff720fe4333b3d095125
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2acdaf6c30fc419b0ba6f4faceab0a13c5183bda348727e860062e1627e891b9653ccc21b0dcad1c35024b655c058e313ba1263d8e5693ea164fb7b53ec6b57d
|
7
|
+
data.tar.gz: 1ba7ca636288931eaeeeabdfac330fd2ceec9868be28a24ba6d38811c095e19f760eff3940c76de0f357b0462bcb034d33adcbdeee4f6c16f1160c2d09175bb6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Yuki Iwanaga
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
roka
|
2
|
+
====
|
3
|
+
|
4
|
+
Romaji to kana converter
|
5
|
+
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
|
10
|
+
Add this line to your Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'roka'
|
14
|
+
```
|
15
|
+
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
require 'roka'
|
22
|
+
|
23
|
+
[
|
24
|
+
'oyasai',
|
25
|
+
'ono',
|
26
|
+
'ohno',
|
27
|
+
'omi',
|
28
|
+
'otto',
|
29
|
+
'kohama',
|
30
|
+
'kondo',
|
31
|
+
'kyari-pamyupamyu',
|
32
|
+
'hohno',
|
33
|
+
'hyogakyo',
|
34
|
+
].each do |romaji|
|
35
|
+
puts romaji
|
36
|
+
p Roka.convert(romaji)
|
37
|
+
end
|
38
|
+
# oyasai
|
39
|
+
# ["オヤサイ"]
|
40
|
+
# ono
|
41
|
+
# ["オノ", "オノオ", "オノウ"]
|
42
|
+
# ohno
|
43
|
+
# ["オノ", "オオノ", "オノオ", "オノウ", "オオノオ", "オオノウ"]
|
44
|
+
# omi
|
45
|
+
# ["オミ", "オオミ", "オウミ"]
|
46
|
+
# otto
|
47
|
+
# ["オット", "オオット", "オウット", "オットオ", "オットウ", "オオットオ", "オオットウ", "オウットオ", "オウットウ"]
|
48
|
+
# kohama
|
49
|
+
# ["コハマ"]
|
50
|
+
# kondo
|
51
|
+
# ["コンド", "コンドオ", "コンドウ"]
|
52
|
+
# kyari-pamyupamyu
|
53
|
+
# ["キャリーパミュパミュ"]
|
54
|
+
# hohno
|
55
|
+
# ["ホノ", "ホオノ", "ホノオ", "ホノウ", "ホオノオ", "ホオノウ"]
|
56
|
+
# hyogaki
|
57
|
+
# ["ヒョガキ", "ヒョオガキ", "ヒョウガキ"]
|
58
|
+
```
|
59
|
+
|
60
|
+
### CLI
|
61
|
+
|
62
|
+
```sh
|
63
|
+
$ roka rokadesu
|
64
|
+
ロカデス
|
65
|
+
ロオカデス
|
66
|
+
ロウカデス
|
67
|
+
```
|
68
|
+
|
69
|
+
|
70
|
+
License
|
71
|
+
-------
|
72
|
+
|
73
|
+
This project is released under the MIT license. See `LICENSE` file for details.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "roka"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/exe/roka
ADDED
data/lib/roka.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'nkf'
|
2
|
+
|
3
|
+
class Roka::Converter
|
4
|
+
|
5
|
+
VOWELS_INDEX = %w[a i u e o ]
|
6
|
+
VOWELS_KANA = %w[ア イ ウ エ オ]
|
7
|
+
|
8
|
+
PREFIX_CHANGES = {
|
9
|
+
'ts' => %w[ツァ ツィ ツ ツェ ツォ],
|
10
|
+
|
11
|
+
'xy' => %w[ャ ィ ュ ェ ョ ],
|
12
|
+
'x' => %w[ァ ィ ゥ ェ ォ ],
|
13
|
+
|
14
|
+
'k' => %w[カ キ ク ケ コ ],
|
15
|
+
's' => %w[サ シ ス セ ソ ],
|
16
|
+
't' => %w[タ チ ツ テ ト ],
|
17
|
+
'n' => %w[ナ ニ ヌ ネ ノ ],
|
18
|
+
'h' => %w[ハ ヒ フ ヘ ホ ],
|
19
|
+
'm' => %w[マ ミ ム メ モ ],
|
20
|
+
'y' => %w[ヤ イ ユ イェ ヨ ],
|
21
|
+
'r' => %w[ラ リ ル レ ロ ],
|
22
|
+
'l' => %w[ラ リ ル レ ロ ],
|
23
|
+
'w' => %w[ワ ウィ ウ ウェ ヲ ],
|
24
|
+
|
25
|
+
'g' => %w[ガ ギ グ ゲ ゴ ],
|
26
|
+
'z' => %w[ザ ジ ズ ゼ ゾ ],
|
27
|
+
'j' => %w[ジャ ジ ジュ ジェ ジョ],
|
28
|
+
'd' => %w[ダ ヂ ヅ デ ド ],
|
29
|
+
'b' => %w[バ ビ ブ ベ ボ ],
|
30
|
+
'v' => %w[ヴァ ヴィ ヴ ヴェ ヴォ],
|
31
|
+
|
32
|
+
'f' => %w[ファ フィ フ フェ フォ],
|
33
|
+
'p' => %w[パ ピ プ ペ ポ ],
|
34
|
+
|
35
|
+
'c' => %w[カ シ ク セ コ ],
|
36
|
+
}
|
37
|
+
|
38
|
+
PATTERN_CHANGES = {
|
39
|
+
/n([^aeiou])/ => 'nn\1',
|
40
|
+
/([^aeiou])\1/ => 'xtu\1',
|
41
|
+
/([kg])w([aeiou])/ => '\1ux\2',
|
42
|
+
/([td])w([aeiou])/ => '\1ox\2',
|
43
|
+
/([sc])h([aeiou])/ => '\1ixy\2',
|
44
|
+
/([kgszjtcdnhbpmrl])y([aeiou])/ => '\1ixy\2',
|
45
|
+
/([td])h([aeiou])/ => '\1exy\2',
|
46
|
+
}
|
47
|
+
|
48
|
+
EXCEPTIONS = {
|
49
|
+
'-' => 'ー',
|
50
|
+
'nn' => 'ン',
|
51
|
+
'xka' => 'ヵ',
|
52
|
+
'xke' => 'ヶ',
|
53
|
+
'xtu' => 'ッ',
|
54
|
+
'xtsu' => 'ッ',
|
55
|
+
'xwa' => 'ヮ',
|
56
|
+
'shi' => 'シ',
|
57
|
+
'chi' => 'チ',
|
58
|
+
}
|
59
|
+
|
60
|
+
attr_reader :determined
|
61
|
+
|
62
|
+
def initialize(str)
|
63
|
+
@buffer = regulate(str)
|
64
|
+
@determined = []
|
65
|
+
end
|
66
|
+
|
67
|
+
def convert
|
68
|
+
return if eos?
|
69
|
+
parse && convert
|
70
|
+
end
|
71
|
+
|
72
|
+
def parse
|
73
|
+
parse_exception \
|
74
|
+
|| parse_pattern \
|
75
|
+
|| parse_prefix \
|
76
|
+
|| parse_vowel \
|
77
|
+
|| consume(1)
|
78
|
+
end
|
79
|
+
|
80
|
+
def parse_exception
|
81
|
+
EXCEPTIONS.each do |seq, replacement|
|
82
|
+
if @buffer.start_with?(seq)
|
83
|
+
consume(seq.size, replacement)
|
84
|
+
return true
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
false
|
89
|
+
end
|
90
|
+
|
91
|
+
def parse_pattern
|
92
|
+
PATTERN_CHANGES.each do |pattern, replacement|
|
93
|
+
r = %r{^#{pattern.source}}
|
94
|
+
buffer = @buffer.sub(r, replacement)
|
95
|
+
|
96
|
+
unless buffer == @buffer
|
97
|
+
@buffer = buffer
|
98
|
+
return true
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
false
|
103
|
+
end
|
104
|
+
|
105
|
+
def parse_prefix
|
106
|
+
PREFIX_CHANGES.each do |prefix, changes|
|
107
|
+
if @buffer.start_with?(prefix)
|
108
|
+
l = prefix.size
|
109
|
+
vowel = @buffer[l]
|
110
|
+
|
111
|
+
if (i = VOWELS_INDEX.index(vowel))
|
112
|
+
consume(l + 1, changes[i])
|
113
|
+
expand_long_sound(vowel)
|
114
|
+
return true
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
false
|
120
|
+
end
|
121
|
+
|
122
|
+
def parse_vowel(consonant = nil)
|
123
|
+
vowel = @buffer[0]
|
124
|
+
if (i = VOWELS_INDEX.index(vowel))
|
125
|
+
consume(1, VOWELS_KANA[i])
|
126
|
+
expand_long_sound(vowel)
|
127
|
+
true
|
128
|
+
else
|
129
|
+
false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def consume(len, replacement = nil)
|
134
|
+
replacement = @buffer[0...len] if replacement.nil?
|
135
|
+
@determined << replacement if replacement
|
136
|
+
@buffer = @buffer[len..-1].to_s
|
137
|
+
true
|
138
|
+
end
|
139
|
+
|
140
|
+
def expand_long_sound(vowel)
|
141
|
+
return unless 'o' == vowel
|
142
|
+
peak = @buffer[0]
|
143
|
+
|
144
|
+
if 'h' == peak
|
145
|
+
u1 = self.class.new(@buffer).tap(&:parse).determined
|
146
|
+
if u1[0] == 'h'
|
147
|
+
consume(1, false)
|
148
|
+
@determined << ['オ']
|
149
|
+
end
|
150
|
+
elsif !(VOWELS_INDEX + %w[n y]).include?(peak)
|
151
|
+
@determined << ['オ', 'ウ']
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def eos?
|
156
|
+
@buffer.empty?
|
157
|
+
end
|
158
|
+
|
159
|
+
def results
|
160
|
+
tree = ['']
|
161
|
+
|
162
|
+
@determined.each do |det|
|
163
|
+
if det.is_a?(Array)
|
164
|
+
tree += tree.product(det).map(&:join)
|
165
|
+
else
|
166
|
+
tree.each { |t| t << det }
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
tree
|
171
|
+
end
|
172
|
+
|
173
|
+
def regulate(str)
|
174
|
+
NKF.nkf('-m0 -Z1 -w', str.to_s.downcase).gsub(/[^a-z-]+/, ' ')
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
data/lib/roka/version.rb
ADDED
data/roka.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'roka/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'roka'
|
8
|
+
spec.version = Roka::VERSION
|
9
|
+
spec.authors = ['Yuki Iwanaga']
|
10
|
+
spec.email = ['yuki@creasty.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Romaji to kana converter}
|
13
|
+
spec.description = %q{Romaji to kana converter}
|
14
|
+
spec.homepage = 'https://github.com/creasty/roka'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: roka
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuki Iwanaga
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Romaji to kana converter
|
56
|
+
email:
|
57
|
+
- yuki@creasty.com
|
58
|
+
executables:
|
59
|
+
- roka
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- exe/roka
|
73
|
+
- lib/roka.rb
|
74
|
+
- lib/roka/converter.rb
|
75
|
+
- lib/roka/version.rb
|
76
|
+
- roka.gemspec
|
77
|
+
homepage: https://github.com/creasty/roka
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Romaji to kana converter
|
101
|
+
test_files: []
|