#!/usr/bin/ruby # # Expects output as from script/find_misspellings. # Done in two parts to give the human operator a chance to verify changes. # # script/find_misspellings > x # vi x # cat x | script/correct_names # ################################################################################ require File.expand_path('../../config/boot.rb', __FILE__) require File.expand_path('../../config/environment.rb', __FILE__) file = ARGV[0] puts "Getting corrections from #{file}..." groups = [[]] File.open(file) do |fh| fh.each_line do |line| line.chomp! if line.blank? groups << [] if groups.last != [] elsif line.match(/^(\*)?(\d+)\t(.*)/) groups.last << [$2, $3, $1 == '*'] end end end puts "Pushing corrections into database..." for group in groups correct_id = nil for id, name, legit in group if legit correct_id = id correct_name = name elsif correct_id puts "#{name} (#{id}) --> #{correct_name} (#{correct_id})" Name.connection.update %( UPDATE names SET correct_spelling_id = #{correct_id}, deprecated = 1 WHERE id = #{id} ) end end end