Skip to content

Instantly share code, notes, and snippets.

@jleedev
Forked from kognate/gist:994423
Created May 27, 2011 00:46
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 jleedev/994429 to your computer and use it in GitHub Desktop.
Save jleedev/994429 to your computer and use it in GitHub Desktop.
tumbledry
class TumbleDRYer
def initialize filename
@hash = Hash.new 0
@links = Hash.new 0
previous = nil
open(filename).read.scan /\w+|\W+/ do |word|
unless previous.nil?
@links[[previous,word]] += 1
end
@hash[word] += 1
previous = word
end
end
attr_reader :hash, :links
end
def test
TumbleDRYer.new('sample.sql')
end
=> {["id", "`),\n "]=>1, ["nickname", "` "]=>1, [") ", "NOT"]=>15, ["NOT", " "]=>18, [" '',\n `", "nickname"]=>1, [" ", "auto_increment"]=>3, ["(", "70"]=>1, ["authors", "` (\n `"]=>1, ["3", " ;\n \n"]=>2, ["default", " '',\n `"]=>7, ["` ", "int"]=>6, [" ;\n \n", "CREATE"]=>3, [" ", "TABLE"]=>4, ["id", "` "]=>3, ["(", "20"]=>1, [" '',\n `", "contact"]=>1, [" ", "NOT"]=>3, ["`),\n ", "KEY"]=>1, ["00", "',\n `"]=>1, ["document_id", "` "]=>1, ["50", ") "]=>7, ["firstname", "` "]=>1, ["=", "14"]=>1, [" (`", "id"]=>3, ["password", "` "]=>1, ["',\n `", "filename"]=>1, ["date", " "]=>1, [",\n `", "author_id"]=>1, ["0", "',\n) "]=>1, ["(", "11"]=>6, ["(", "50"]=>7, [" ", "default"]=>13, ["KEY", " `"]=>1, ["0000", "-"]=>1, [" '", "0000"]=>1, ["',\n `", "document_id"]=>1, ["0", "',\n `"]=>2, ["contact", "` "]=>1, ["=", "3"]=>2, ["AUTO_INCREMENT", "="]=>3, ["` ", "text"]=>2, ["11", ") "]=>6, ["document", "` (`"]=>1, [" ", "NULL"]=>18, ["TYPE", "="]=>4, [" `", "authors"]=>1, ["20", ") "]=>1, ["PRIMARY", " "]=>3, ["` (`", "title"]=>1, [",\n `", "title"]=>1, ["TABLE", " `"]=>4, ["NULL", ",\n "]=>1, ["00", "-"]=>1, ["NULL", ",\n `"]=>1, ["KEY", " (`"]=>3, ["MyISAM", " "]=>3, [" ", "KEY"]=>3, ["default", " '',\n "]=>2, [" '", "0"]=>3, [" '',\n ", "PRIMARY"]=>2, ["id", "`)\n) "]=>2, [",\n ", "PRIMARY"]=>1, ["14", " ;\n"]=>1, ["date", "` "]=>1, ["text", " "]=>2, ["`)\n) ", "TYPE"]=>3, ["NULL", " "]=>16, ["description", "` "]=>3, [" `", "document"]=>1, ["',\n) ", "TYPE"]=>1, ["default", " '"]=>4, ["int", "("]=>6, [",\n `", "firstname"]=>1, ["varchar", "("]=>9, ["categories_documents", "` (\n `"]=>1, ["` (\n `", "id"]=>3, ["author_id", "` "]=>1, ["` (\n `", "category_id"]=>1, [" `", "categories_documents"]=>1, ["filename", "` "]=>1, ["title", "` "]=>1, [" `", "documents"]=>1, ["name", "` "]=>2, [" '',\n `", "description"]=>3, ["categories", "` (\n `"]=>1, ["',\n `", "date"]=>1, ["documents", "` (\n `"]=>1, ["MyISAM", " ;\n \n"]=>1, ["=", "MyISAM"]=>4, ["70", ") "]=>1, [" `", "categories"]=>1, ["category_id", "` "]=>1, ["` ", "varchar"]=>9, [" '',\n `", "name"]=>1, [" '',\n `", "password"]=>1, ["CREATE", " "]=>4, ["title", "`)\n) "]=>1, ["-", "00"]=>2, ["` ", "date"]=>1, ["auto_increment", ",\n `"]=>3, [",\n `", "name"]=>1, [" ", "AUTO_INCREMENT"]=>3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment