-
Notifications
You must be signed in to change notification settings - Fork 0
/
c2py.rb
62 lines (49 loc) · 1.34 KB
/
c2py.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
#!/usr/bin/env ruby
#
# Authored by Yulu Ma, All Rights Reserved.
#
require 'optparse'
require 'ruby-pinyin'
Version = 'Version:20141226, Copyright by Yulu Ma. All Rights Reserved.'
options = {}
lines = []
pinyinarray = []
counts = 0
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 the "-" separated pingyin string.') 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)
pinyin = String.new()
puts "Converting... wait a minute...\n"
lines.each do |line|
pinyin = ""
PinYin.of_string(line).each do |word|
if pinyin.length == 0
pinyin << word.to_s.downcase
else
pinyin << "-" + word.to_s.downcase
end
end
pinyinarray << pinyin
counts += 1
end
File.open(options[:output], 'w') { |file| file.write(pinyinarray.join("\n")) }
puts "Success! totally #{counts} records converted.\n"
else
puts "Usage: c2py -h for help.\n"
end