Skip to content

Instantly share code, notes, and snippets.

@Eric-Guo
Created June 7, 2020 06:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eric-Guo/48f275929680cf52da649664e36a6815 to your computer and use it in GitHub Desktop.
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
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