Skip to content

Commit 24f407b

Browse files
committed
fix issue #5
1 parent d229750 commit 24f407b

File tree

11 files changed

+133
-60
lines changed

11 files changed

+133
-60
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
*.swp
3+
.vscode/

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
source 'https://rubygems.org'
2+
3+
gemspec

Gemfile.lock

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
PATH
2+
remote: .
3+
specs:
4+
interscript (0.9)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
debase (0.2.4.1)
10+
debase-ruby_core_source (>= 0.10.2)
11+
debase-ruby_core_source (0.10.7)
12+
diff-lcs (1.3)
13+
rake (12.3.3)
14+
rspec (3.9.0)
15+
rspec-core (~> 3.9.0)
16+
rspec-expectations (~> 3.9.0)
17+
rspec-mocks (~> 3.9.0)
18+
rspec-core (3.9.0)
19+
rspec-support (~> 3.9.0)
20+
rspec-expectations (3.9.0)
21+
diff-lcs (>= 1.2.0, < 2.0)
22+
rspec-support (~> 3.9.0)
23+
rspec-mocks (3.9.0)
24+
diff-lcs (>= 1.2.0, < 2.0)
25+
rspec-support (~> 3.9.0)
26+
rspec-support (3.9.0)
27+
ruby-debug-ide (0.7.0)
28+
rake (>= 0.8.1)
29+
30+
PLATFORMS
31+
ruby
32+
33+
DEPENDENCIES
34+
bundler (~> 2.0)
35+
debase
36+
interscript!
37+
rake (~> 12.0)
38+
rspec
39+
ruby-debug-ide
40+
41+
BUNDLED WITH
42+
2.0.1

bin/interscript

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ require_relative '../lib/interscript'
55
if ARGV.empty?
66
puts "write source file, source format, and output file"
77
else
8-
args = Hash[ ARGV.flat_map{|s| s.scan(/--?([^=\s]+)(?:=(\S+))?/) } ]
8+
args = Hash[ARGV.flat_map { |s| s.scan(/--?([^=\s]+)(?:=(\S+))?/) }]
99
input = ARGV[0]
1010
system_code = args["system"]
1111
output_file = args["output"]
1212

1313
raise "Please enter the system code with --system={system_code}" unless system_code
1414

1515
if output_file
16-
Interscript.instance.transliterate_file(system_code, input, output_file)
16+
Interscript.transliterate_file(system_code, input, output_file)
1717
else
18-
puts Interscript.instance.transliterate(system_code, IO.read(input))
18+
puts Interscript.transliterate(system_code, IO.read(input))
1919
end
2020
end
21-
22-

bin/rspec

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rspec' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
require "pathname"
12+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13+
Pathname.new(__FILE__).realpath)
14+
15+
bundle_binstub = File.expand_path("../bundle", __FILE__)
16+
17+
if File.file?(bundle_binstub)
18+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19+
load(bundle_binstub)
20+
else
21+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23+
end
24+
end
25+
26+
require "rubygems"
27+
require "bundler/setup"
28+
29+
load Gem.bin_path("rspec-core", "rspec")

interscript.gemspec

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ Gem::Specification.new do |spec|
2020
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
2121
spec.require_paths = ["lib"]
2222
spec.bindir = 'bin'
23-
spec.add_development_dependency "bundler", "~> 1.7"
24-
spec.add_development_dependency "rake", "~> 10.0"
23+
24+
spec.add_development_dependency "bundler", "~> 2.0"
25+
spec.add_development_dependency "debase"
26+
spec.add_development_dependency "rake", "~> 12.0"
2527
spec.add_development_dependency "rspec"
28+
spec.add_development_dependency "ruby-debug-ide"
2629
end

lib/interscript.rb

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
1-
require 'yaml'
2-
require 'singleton'
1+
# frozen_string_literal: true
32

4-
class Interscript
5-
include Singleton
6-
7-
SYSTEM_DEFINITIONS_PATH = File.expand_path('../../maps', __FILE__)
3+
require 'yaml'
84

9-
def initialize
10-
@systems = {}
11-
end
5+
# Transliteration
6+
module Interscript
7+
SYSTEM_DEFINITIONS_PATH = File.expand_path('../maps', __dir__)
128

13-
def transliterate_file(system_code, input_file, output_file)
14-
input = File.read(input_file)
15-
output = transliterate(system_code, input)
9+
class << self
10+
def transliterate_file(system_code, input_file, output_file)
11+
input = File.read(input_file)
12+
output = transliterate(system_code, input)
1613

17-
File.open(output_file, "w") do |f|
18-
f.puts(output)
14+
File.open(output_file, "w") do |f|
15+
f.puts(output)
16+
end
17+
puts "Output written to: #{output_file}"
1918
end
20-
puts "Output written to: #{output_file}"
21-
end
22-
23-
def load_system_definition(system_code)
24-
@systems[system_code] ||= YAML.load_file(File.join(SYSTEM_DEFINITIONS_PATH, "#{system_code}.yaml"))
25-
end
26-
27-
def get_system(system_code)
28-
@systems[system_code]
29-
end
3019

31-
def system_char_map(system_code)
32-
get_system(system_code)["map"]["characters"]
33-
end
34-
35-
def system_rules(system_code)
36-
get_system(system_code)["map"]["rules"]
37-
end
20+
def load_system_definition(system_code)
21+
YAML.load_file(File.join(SYSTEM_DEFINITIONS_PATH, "#{system_code}.yaml"))
22+
end
3823

39-
def transliterate(system_code, string)
40-
load_system_definition(system_code)
24+
def transliterate(system_code, string)
25+
system = load_system_definition(system_code)
4126

42-
# TODO: also need to support regular expressions via system_rules(system_code), before system_char_map
27+
rules = system["map"]["rules"] || []
28+
charmap = system["map"]["characters"] || {}
4329

44-
character_map = system_char_map(system_code)
30+
rules.each do |r|
31+
string.gsub! %r{#{r["pattern"]}}, r["result"]
32+
end
4533

46-
string.split('').map do |char|
47-
converted_char = character_map[char] ? character_map[char] : char
48-
string[char] = converted_char
49-
end.join('')
34+
string.split('').map do |char|
35+
charmap[char] || char
36+
end.join('')
37+
end
5038
end
51-
5239
end
53-

maps/bgnpcgn-rus-Cyrl-Latn-1947.yaml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,17 @@ tests:
153153
154154
map:
155155
rules:
156-
- pattern: "/([ЄФІЦАаЕеИиЙйОоУуЫыЮюЯяії])\u0415/"
156+
- pattern: ([ЄФІЦАаЕеИиЙйОоУуЫыЮюЯяії])\u0415
157157
result: "\\1YE"
158-
- pattern: "/^\u0415/"
159-
result: "YE"
160-
- pattern: "/([йьъ])\u0415/"
158+
- pattern: \b\u0415
159+
result: YE
160+
- pattern: ([йьъ])\u0415
161161
result: "\\1YE"
162-
- pattern: "/([ЄФІЦАаЕеИиЙйОоУуЫыЮюЯяії])\u0435/"
162+
- pattern: ([ЄФІЦАаЕеИиЙйОоУуЫыЮюЯяії])\u0435
163163
result: "\\1ye"
164-
- pattern: "/^\u0435/"
165-
result: "ye"
166-
- pattern: "/([йьъ])\u0435/"
164+
- pattern: \b\u0435
165+
result: ye
166+
- pattern: ([йьъ])\u0435
167167
result: "\\1ye"
168168

169169
characters:
@@ -198,7 +198,7 @@ map:
198198
"\u042b": "Y"
199199
"\u042c": "\u2019"
200200
"\u042d": "E"
201-
"\u042e": "Y"
201+
"\u042e": "Yu"
202202
"\u042f": "Ya"
203203
"\u0430": "a"
204204
"\u0431": "b"
@@ -218,7 +218,7 @@ map:
218218
"\u0440": "r"
219219
"\u0441": "s"
220220
"\u0442": "t"
221-
"\u0443": ""
221+
"\u0443": "u"
222222
"\u0444": "f"
223223
"\u0445": "kh"
224224
"\u0446": "ts"
@@ -229,5 +229,5 @@ map:
229229
"\u044b": "y"
230230
"\u044c": "\u2019"
231231
"\u044d": "e"
232-
"\u044e": "y"
232+
"\u044e": "yu"
233233
"\u044f": "ya"

spec/interscript_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Interscript do
4+
it "converts rus using bgnpcgn-rus-Cyrl-Latn-1947" do
5+
system = YAML.load_file "maps/bgnpcgn-rus-Cyrl-Latn-1947.yaml"
6+
system["tests"].each do |test|
7+
result = Interscript.transliterate "bgnpcgn-rus-Cyrl-Latn-1947", test["source"]
8+
expect(result).to eq test["expected"]
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)