Skip to content

Instantly share code, notes, and snippets.

@ritec
Last active October 4, 2016 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritec/f16a4a3e7e266b18dfcf to your computer and use it in GitHub Desktop.
Save ritec/f16a4a3e7e266b18dfcf to your computer and use it in GitHub Desktop.
This Script will populate the nicknames table from the data in the Nickname and Diminutive Names Lookup Project.
puts "Populate Nickname table"
# Processes file in CSV format from http://code.google.com/p/nickname-and-diminutive-names-lookup/
puts "Truncating nicknames table"
Nickname.delete_all
puts "Downloading nicknames CSV file from http://code.google.com/p/nickname-and-diminutive-names-lookup/"
file = Net::HTTP.get 'nickname-and-diminutive-names-lookup.googlecode.com', '/files/names1.2.csv'
puts "Done"
puts "Parsing file"
file.split("\n").in_groups_of(100, false) do |group|
group.each do |line|
names = line.chomp.split(',')
diminutive = names[0]
result = Nickname.create!(:name => diminutive)
result.update_attributes!(:nickname_id => result.id)
names[1..-1].each do |nickname|
Nickname.create!(:name => nickname, :nickname_id => result.id)
end
end
puts "Processed #{Nickname.count} records"
end
puts "Finished processing #{Nickname.count} records"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment