Created
February 4, 2011 15:02
-
-
Save jsomers/811194 to your computer and use it in GitHub Desktop.
A script that finds unique word templates
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
words = File.open("./TWL06.txt").read.downcase.gsub("\r", "").split("\n") | |
words.select! {|w| w.length == 6} | |
# Approach: | |
# 1. Generate an index that maps 2-templates (like "_l___x") to words ("climax"). | |
# 2. Find 2-templates that map to just one word. | |
def templates(word) | |
"Finds all fifteen (6! / 4! * 2!) 2-templates for a given word." | |
inds = []; (0..4).collect {|i| (i + 1..5).each {|j| inds << [i, j]}} | |
ts = [] | |
inds.each do |ij| | |
w = word.split('') | |
((0..5).to_a - ij).each { |x| w[x] = "_" } | |
ts << w.join | |
end | |
return ts | |
end | |
# Step 1. | |
index = {} | |
words.each do |word| | |
templates(word).each do |t| | |
if index[t] then index[t] << word else index[t] = [word] end | |
end | |
end | |
# Step 2. | |
p index.select {|k, v| v.length == 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment