#!/usr/bin/python
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# I don't bother to provide a copy of the GNU General Public License
# along with this program, but you can get one from the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Seth Golub http://www.sethoscope.net/
# 2007-01-03  version 1.0


import random
import re
import sys

grammar = {
 'number': ['two', 'three', 'a dozen', 'fifty'],
 'subject_single': ['farmer', 'cow', 'frog', 'pig', 'weasel', 'fireman',
                    'buffalo', 'dog', 'cat', 'snake', 'hornet'],
 'subject_plural': ['farmers', 'cows', 'frogs', 'pigs', 'weasels', 'firemen',
                    'buffalo', 'dogs', 'cats', 'snakes', 'hornets'],
 'adj2': ['one-legged', 'one-armed', 'three-legged', 'greased', 'blind',
          'drunk', 'boiled'],
 'subject' : ['a %subject_single%', 'a %adj2% %subject_single%',
              '%number% %subject_plural%', '%number% %adj2% %subject_plural%'],

 'noun': ['banjo', 'bacon', 'chair', 'yo-yo', 'weasel', 'pie'],
 'plnoun': ['banjos', 'bacon', 'chairs', 'yo-yos', 'weasels', 'pies'],
 'inplace': ['henhouse', '%noun% factory',
             'sack full of %plnoun%',
             'barrel of %plnoun%'],
 'onplace': ['astroturf', 'the moon', 'a log'],
 'bodypart': ['leg', 'arm'],
 'condition': ['broken %bodypart%', 'hangover'],
 'obj_phrase': ['%subject% in a %inplace%',
                '%subject% on %onplace%',
                '%subject% with a %condition%'],
 'adj1': ['quick', 'smooth', 'slick', 'busy', 'tired', 'confused', 'crooked', 'dumb', 'happy'],
 'sentence': ['as %adj1% as %obj_phrase%'],
}

def resolve_one(matchob):
  return random.choice(grammar[matchob.group()[1:-1]])

regex = re.compile('%[^%]+%')
def resolve(text):
  numsubs = 1
  while numsubs > 0:
    (text, numsubs) = regex.subn(resolve_one, text)
  return text

count = 1
if len(sys.argv) > 1:
  count = int(sys.argv[1])
  if len(sys.argv) > 2:
    grammar['adj1'] = [sys.argv[2]]
for n in range(count):  
  print resolve('%sentence%')

