Created
June 7, 2020 06:26
-
-
Save Eric-Guo/48f275929680cf52da649664e36a6815 to your computer and use it in GitHub Desktop.
Translate the Chinese name (char) into English last_name and first_name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/all' | |
require 'chinese_pinyin' | |
require 'csv' | |
CSV.open('name_t.csv', 'w') do |csv| | |
csv << %w[chinese_name last_name first_name] | |
file = File.new('/Users/guochunzhong/Downloads/name_list.txt') | |
file.each do |line| | |
values = [] | |
name = line.strip | |
py = Pinyin.t(line.strip) | |
pys = py.split(' ') | |
puts "#{file.lineno}: #{name}, #{pys.first&.titleize} #{pys[1..]&.join('')&.titleize}" | |
values << name | |
values << pys[1..]&.join('')&.titleize | |
values << pys.first&.titleize | |
csv << values | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment