By the way, the title of this post "Colorless green ideas sleep furiously" is a syntactically correct but nonsensical sentence devised by Noam Chomsky. Noam Chomsky pioneered the field of generative grammars. The ChomskyBot implements a simple generative grammar.
The sentences generated by the bot are similar to the language of Noam Chomsky's works, and I guess the pun is intended.
Of course, I couldn't resist the temptation to write a Ruby version of the Chomsky bot:
class ChomskyBot
@@phrase_elems = [ "intro", "subject", "verb", "object" ]
def initialize(intro_file, subject_file, verb_file, object_file)
@@phrase_elems.each do |e|
instance_eval("@#{e}s = []")
instance_eval("File.open(#{e}_file).each_line" +
"{ |l| @#{e}s.push l.chop }")
end
end
def generate_lines(n)
lines = []
n.times do
@@phrase_elems.each do |e|
eval("lines << @#{e}s.slice!(rand(@#{e}s.size-1)) << ' '")
end
end
lines
end
def paragraph
generate_lines(5).join
end
end
I tried to make it as simple as I could get away with. I shaved quite a few lines using eval, hope it doesn't hurt readability to much.
You'll need some phrase files to play with it. You can find those here:
Introduction phrases
Subject phrases
Verb phrases
Object sentences
You can try the original version of the ChomskyBot online. It's written in Perl (source) by Kevin McGovan. For more information, pay a visit to Chomsky bot inventor John Lawlers website.
No comments:
Post a Comment