Created
March 28, 2013 06:16
-
-
Save tormaroe/5261088 to your computer and use it in GitHub Desktop.
A small script to parse a text file and produce output for Wordle.
This file contains 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
# Encoding: UTF-8 | |
Encoding.default_external = "UTF-8" | |
require 'benchmark' | |
BLACKLIST = %w(på og er om fra med i to det å | |
ditt til ved fra for av en din | |
du at vi har vil nå det som dere | |
kan vår så) | |
$words = {} | |
def parse_words file | |
puts "Parsing words.." | |
File.readlines(file).each do |line| | |
line.split.each do |word| | |
word = word.strip.gsub /[\,\.\!\:]$/, "" | |
next if word == "" | |
next if BLACKLIST.include? word.downcase | |
if $words.include? word | |
$words[word] += 1 | |
else | |
$words[word] = 1 | |
end | |
end | |
end | |
end | |
def trim_rare_words | |
puts "Trimming rare words.." | |
$words.select! {|word, count| count > 1000 } | |
end | |
def wordle_output stream | |
puts "Outputing.." | |
$words.each do |word, count| | |
stream.puts "#{word}:#{count}" | |
end | |
end | |
puts Benchmark.measure { | |
parse_words ARGV.shift | |
trim_rare_words | |
File.open("out.txt", "w") {|f| wordle_output f } | |
puts "DONE" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment