Markov State of the Union 3

Posted by dgtized Sun, 05 Feb 2006 19:35:00 GMT

me@acm ~ $ cat sotu_speech | ./markov.rb

the terror is only stronger protect the security offered against the goal is to grow the future of these two key issues, trade. Selling in case of Representment. (Applause.)
No nation against terrorist plans. (Applause.) Now American people every region, freed a country, we will never before, to put our homeland, America is acting elsewhere. At a memorial in New York, a little boy left his football until I can play with improved in the price of the state of our work, I even had nice thing possible military. (Applause.) Beyond all who have to risk losing everything to track the security can vanish in an hour of shock and more about right. (Applause.) We have founding. In every classroom. (Applause.) Senator Judd Gregg. (Applause.)

The last time of adversity offered again that including your nation owns these cases, these, and I urge the terror. And in caves — you will focus on that this chamber, the best that country, mourning them look like. We seek chemical weapons of murder, often support terrorist parasites who have lost the world, including groups. (Applause.) Last year, some thought it was done on September the secret police.

If anyone doubts this, let’s make our enemies’ hatred is equaled by them. We are one country’s armed with our country in a time of terror is only begun. This camps operate, so long as Congress is a regimes have lost fathers huddled over the 11th. But when the peace of the gather. I was proud of our entire world like Hamas, Hezbollah, Islamic world faced a choice, made a fine start. To sustain and offers a unique role in applauding your neighbors and Senior Corps, doubles funding America will lead by defense spending for the non-negotiable and allies from Europe to Asia, and still exist in our work together. Deep in the price of this Congress must always standards and affordable energy. This is a regimes, are now have the non-negotiable demands of human dignity of every life. And the best trained terrorist training and al


Who else wants to generate random text from the state of the union address or has any other interesting bit of text to mangle?

To use the following code save it as markov.rb, and give it some text as input. It will then go through find all of the suffixes for a (width-suffix) set of text. It will then pick a random starting prefix and then randomly select suffixes for the last prefix characters in your output text. The code may be a bit more clear for those of you that speak ruby. Depending on the length of your input text it may be worthwhile to fool around with the prefix_length, suffix_length, and output_length variables. Drop the prefix_length down to 3 or 4 and you get pretty much nonsense, but run it too high and it will simply spit the input text back out. Remember, you can feed it more then one input text as well. Have fun!

markov.rb:

#!/usr/bin/env ruby#!/usr/bin/env ruby

suffix_length = 1
prefix_length = 6
output_length = 2000

book = ARGF.read
markov_table = Hash.new {|h,k| h[k] = Array.new}

(0..book.size-prefix_length-suffix_length).each do |pos|
prefix = book[pos,prefix_length]
suffix = book[pos+prefix_length,suffix_length]
markov_table[prefix] << suffix
end

output = markov_table.keys[rand(markov_table.size)]

output_length.times do
next_key = output[-prefix_length,prefix_length]
suffixes = markov_table[next_key]
break if suffixes.empty?
output += suffixes[rand(suffixes.size)]
end

puts output

Calendar was broken 2

Posted by dgtized Thu, 21 Jul 2005 08:28:00 GMT

I still need to upgrade to the super fancy version of the calender that was posted as a patch on typo.leetsoft.com, but it was loosely based on the calender system running on my version of typo. Unfortuneatly somewhere in my modification I had forgotten to include the critical piece of code that read:

[""] * (self.class.civil(year,month,1).wday)

Which needed to be prepended to the array of dates in the month to ensure the calender started on the right day of the week for each month. Perhaps sometime next week I will dissassemble the patch that followed mine since it doesn’t seem to work as a patch set anymore. It would be nice to have a calender with AJAXy goodness and all that nonsense.

If anyone is interested I based all of my calendar code from a snipet that appeared on comp.lang.ruby:

calendar.rb

require 'date' require 'enumerator'require date
require enumerator

class Date
def days_in_month
self.class.civil(year, month, -1).day
end
alias dim days_in_month

def weekdays (1..dim).to_a end def cal_print banner = ABBR_DAYNAMES.map{|d| d[0..1]}.join( ) puts "#{MONTHNAMES[month]} #{year}".center(banner.size) puts banner wd = [ ] * wday + weekdays wd.each_slice(7) do |slice| puts slice.map{|d| d.to_s.rjust(2)}.join( ) end end

end

month = (ARGV[0] || Date.today.mon).to_i
year = (ARGV[1] || Date.today.year).to_i

Date.civil(year, month).cal_print

Perhaps the above comment about the missing line makes more sense in that context. Also hopefully someone determines a way to use syntax to auto-hilight code in typo.


/">Valid CSS