-
Notifications
You must be signed in to change notification settings - Fork 0
/
c2spn.rb
89 lines (69 loc) · 1.55 KB
/
c2spn.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
78
79
80
81
82
83
84
85
#!/usr/bin/env ruby
require 'optparse'
require 'ruby-pinyin'
Version = 'Version:20141226, Copyright by Yulu Ma. All Rights Reserved.'
options = {}
lines = []
shoupinarray = []
counts = 0
keyCode = {
"a" => "2",
"b" => "2",
"c" => "2",
"d" => "3",
"e" => "3",
"f" => "3",
"g" => "4",
"h" => "4",
"i" => "4",
"j" => "5",
"k" => "5",
"l" => "5",
"m" => "6",
"n" => "6",
"o" => "6",
"p" => "7",
"q" => "7",
"r" => "7",
"s" => "7",
"t" => "8",
"u" => "8",
"v" => "8",
"w" => "9",
"x" => "9",
"y" => "9",
"z" => "9"
}
option_parser = OptionParser.new do |opts|
opts.on('-i INPUTFILE', 'UTF-8 encodeing text file, with each row is a chinese string.') do |inputfile|
options[:input] = inputfile
end
opts.on('-o OUTPUT', 'Result text file, with each row T9 digis.') do |outputfile|
options[:output] = outputfile
end
opts.on('-v','Show current verion.') do
puts Version.to_s
end
end
option_parser.parse!
def read_file(io, lines)
io.readlines.each do |line|
lines << line.chomp
end
end
if options[:input] && options[:output]
read_file(File.open(options[:input]), lines)
puts "Converting... wait a minute...\n"
lines.each do |line|
shoupin = String.new()
PinYin.of_string(line).each do |word|
shoupin << keyCode[word[0]].to_s
end
shoupinarray << shoupin
counts += 1
end
File.open(options[:output], 'w') { |file| file.write(shoupinarray.join("\n")) }
puts "Success! totally #{counts} records converted.\n"
else
puts "Usage: c2spn -h for help.\n"
end