Created
July 8, 2012 10:24
-
-
Save dcadenas/3070384 to your computer and use it in GitHub Desktop.
Script to explore chess opening lines based on most popular google results
This file contains hidden or 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
#!/usr/bin/env ruby | |
#Script to explore chess opening lines based on most popular of google's top 100 results | |
#Usage: | |
#▸ ./goopening "1. e4 e5 2." | |
#1: nf3 (35) | |
#2: qh5 (7) | |
#3: bc4 (4) | |
#4: nc6 (2) | |
require 'open-uri' | |
require 'nokogiri' | |
require 'cgi' | |
require 'stringio' | |
def normalize(alg) | |
alg = alg.gsub(/\.\S/, '. ') | |
alg = alg.gsub(/(\d)\./, '\1') | |
alg.downcase | |
end | |
def find_chess_move(dirty_string) | |
move_regexp = /([kqnpbr](([abcdefgh]?[1-8]?))?x?[abcdefgh][1-8]|o-o(-o)?|ooo?)/ | |
dirty_string.scan(move_regexp).flatten.first | |
end | |
def get_strings_at_the_right_of_each_match(text_of_search_page, move_list) | |
size_of_algebraic_notation_movement = 15 | |
text_of_search_page.scan(/#{move_list}(.{#{size_of_algebraic_notation_movement}})/).flatten | |
end | |
move_list = ARGV[0] | |
move_list = normalize(move_list) | |
text_of_search_page = Nokogiri(open("http://www.google.com/search?q=#{CGI.escape(move_list)}&num=100&complete=0").read).text | |
text_of_search_page = normalize(text_of_search_page) | |
strings_at_the_right_of_each_match = get_strings_at_the_right_of_each_match(text_of_search_page, move_list) | |
count_hash = {} | |
strings_at_the_right_of_each_match.map{|a| find_chess_move(a)}.compact.map do |move| | |
count_hash[move] = 0 unless count_hash[move] | |
count_hash[move] += 1 | |
end | |
best = count_hash.to_a.sort{|a, b| b.last <=> a.last } | |
i = 0 | |
puts best.map{|b| b.last == 1 ? nil : "#{i+=1}: #{b.first} (#{b.last})"}.compact |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment