-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_pingyam.rb
77 lines (70 loc) · 1.68 KB
/
lib_pingyam.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Converter
def initialize(base_rom=0)
@dict = read_dict(base_rom)
end
def convert_syllable(word, method, mods=nil)
w = @dict[word.downcase]
if w
syllable = w[method]
if mods
get_modifications(syllable, mods)
else
method == 11 ? w : syllable
end
else
word
end
end
def read_dict(base_rom=0)
pwd = Dir.pwd
Dir.chdir(File.dirname(__FILE__))
file = File.read("pingyam/pingyambiu")
Dir.chdir(pwd)
dict = {}
file.each_line do |line|
line_split = line.chomp.split("\t")
dict[line_split[base_rom]] = line_split
end
dict
end
def convert_line(line, method, mods=nil)
line_array = line.split(/\s+/)
result = ""
if method == 11
11.times do |c|
line_array.each do |word|
result << convert_syllable(word, c, mods) + " "
end
result << "\n"
end
else
line_array.each do |word|
result << convert_syllable(word, method, mods) + " "
end
end
result.gsub(/\s+\Z/, "")
end
def check_syllable(word)
w = @dict[word.downcase]
w ? true : false
end
def to_superscript(syllable)
num_hash = {
"1" => "¹", "2" => "²", "3" => "³",
"4" => "⁴", "5" => "⁵", "6" => "⁶",
"7" => "⁷", "8" => "⁸", "9" => "⁹"
}
syllable.gsub!(/([1-9])/) { |d| num_hash[d] }
end
def normalize_yale(syllable)
num_hash = {
"7" => "1", "8" => "3", "9" => "6"
}
syllable.gsub!(/([789])/) { |d| num_hash[d] }
end
def get_modifications(syllable, mods)
normalize_yale(syllable) if mods[:yale]
to_superscript(syllable) if mods[:superscript]
syllable
end
end