This copied a little weird. For a “correct” copy, see
Private Paste.
#!/usr/bin/env ruby -w
# Today (March 21, 2008) there was a documentary on The History
# Channel about the "Bible Code." This "code" is where you take all
# the letters of the bible, and look through them to see if
# "interesting" words appear in the bible and their letters are
# equidistantly spaced. For example, the word "hitler" might appear
# somewhere where there are 10 other characters between the H and the
# I, 10 characters between the I and the T, and so on.
#
# Some people on this documentary (which included professors at
# prestigious universities at CalTech) seem to take this to mean that
# the bible has some sort of divinely inspired code that can be used
# to predict the future. I think this is complete and utter
# non-sense, and this program is meant to demonstrate that.
#
# You give this script a list of words to search for (the dictionary;
# each word is on its own line) and a piece of text to search
# through. The program looks for each word in the dictionary in the
# text and if it finds a match, it tells you where the program started
# to find the word (the character where the first letter occured), and
# the "code length" (the number of other characters between the
# letters of the word).
#
# My claim is that "interesting" words will occur in any large enough
# piece of text, so try running this against the bible, Moby Dick,
# Paradise Lost, and so on and see what you get.
#
# Author: Chris Johnson, ccjohnson@gmail.com
# Date: March 21, 2008
# Make sure we're given two arguments
if (ARGV.length != 2) then
puts "Use: ./codes.rb "
puts " is the file containing the list of words to " +
"search for, "
puts " and is the text to search through."
puts "E.g.: ./codes.rb dictionary.txt bible.txt"
exit 1
end
# Make sure we can read the dictionary file
if (!File.readable? ARGV[0]) then
puts "The dictionary file can not be read."
exit 1
end
# Make sure we can read the search text file
if (!File.readable? ARGV[1]) then
puts "The search text file can not be read."
exit 1
end
# Open the files
dictionary_file = File.open(ARGV[0], "r")
text_file = File.open(ARGV[1], "r")
$dictionary = [] # The array of dictionary words
$text = '' # One very long string containing the text
# Get our data out of the files
dictionary_file.each { |l| $dictionary « l.chomp }
text_file.each { |l| $text « l.chomp }
# Get rid of white space and punctuation characters in the search
# text, and make everything lower case (since case shouldn't matter in
# what we're searching for)
$text.gsub!(/\s+|[;.,()]/, '').downcase!
# We'll store all of the words we find
$finds = []
$dictionary.each do |word|
chars = word.split ''
offset = 0
while true do
# Jump out if we can't even find the letter we're looking for in
# the remaining text.
break unless first = $text.index(chars[0], offset)
offset = first + 1
break unless second = $text.index(chars[1], offset)
length = second - first
last_find = second
found = true # We'll start off optimistically
chars[2 .. chars.length].each do |char|
if last_find + length >= $text.length or
$text[last_find + length].chr != char then
found = false
break
end
last_find += length
end
if found then
puts "The word '#{word}' was found with code length #{length} " +
"starting at character #{first + 1}"
$finds break
end
end
end
# Report our findings
if $finds.empty? then
puts "Nothing was found."
else
puts "There were #{$finds.length} finds in total"
end