Skip to content

Instantly share code, notes, and snippets.

@elorest
Created April 1, 2010 19:57
Show Gist options
  • Save elorest/352283 to your computer and use it in GitHub Desktop.
Save elorest/352283 to your computer and use it in GitHub Desktop.
xdef distance(a, b)
return a.length if (0 == b.length)
return b.length if (0 == a.length)
unpack_rule = ($KCODE =~ /^U/i) ? 'U*' : 'C*'
mapper = lambda{|str|
str = str.unpack(unpack_rule)
map = []
str.each_with_index {|letter,idx|
occurrence_count = 0
str[0,idx+1].each { |curr_val| occurrence_count += 1 if curr_val == letter }
map.push("#{letter}-#{occurrence_count}")
}
map
}
map1 = mapper.call a
map2 = mapper.call b
diff_map = (map2.length > map1.length) ?
map2 - map1 : map1 - map2
return diff_map.length
end
ChromeToCategory.all.each do |ctc|
unless ctc.category_id.blank?
oc = Category.find(ctc.category_id)
nc = Category.find_by_old_id(ctc.category_id)
begin
unless oc.ancestors.first.name.downcase == "autos" && distance(oc.ancestors.second.name.downcase, ctc.vehicle_name.split('>').first.strip.downcase) <= 1
if nc.ancestors.first.name.downcase == "autos" && distance(nc.ancestors.second.name.downcase, ctc.vehicle_name.split('>').first.strip.downcase) <= 1
ctc.category_id = nc.id
ctc.save
else
puts "Error! with ChromeToCategory id ##{ctc.id}"
end
else
# puts "Notice! ChromeToCategory id ##{ctc.id} appears to be right."
end
rescue NoMethodError
puts "Error! nil category for ChromeToCategory id ##{ctc.id}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment