roka 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in roka.gemspec
4
+ gemspec
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.
@@ -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.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # enable local usage from cloned repo
4
+ root = File.expand_path('../..', __FILE__)
5
+ $LOAD_PATH << "#{root}/lib" if File.exist?("#{root}/Gemfile")
6
+
7
+ require 'roka'
8
+
9
+ text = ARGV[0]
10
+ puts Roka.convert(text) if text
@@ -0,0 +1,12 @@
1
+ require 'roka/version'
2
+ require 'roka/converter'
3
+
4
+ module Roka
5
+
6
+ extend self
7
+
8
+ def convert(str)
9
+ Converter.new(str).tap(&:convert).results
10
+ end
11
+
12
+ end
@@ -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
@@ -0,0 +1,3 @@
1
+ module Roka
2
+ VERSION = "0.1.0"
3
+ end
@@ -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: []