Advertisement
MolSno

madlibs v0.8c

Feb 19th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1. import os, random, re
  2.  
  3. libsdir = '/root/phenny/madlibs/'
  4. listfile_path = '/root/phenny/libst.txt'
  5.  
  6. class Phrase(object):
  7.     def __init__(self, text='', keyword='', name=''):
  8.         self.text = text
  9.         self.keyword = keyword
  10.         self.name = name
  11.  
  12. def madlibs_main(phenny, input):
  13.     if not input.group(2):
  14.         if input.group(1) == 'would':
  15.             sentence = "I would [verb] "+random.choice(['her','his'])+" [noun]"
  16.         elif input.group(1) == 'should':
  17.             sentence = "You should [verb] me with a(n) [adj] [noun]!"
  18.         else:
  19.             sentence = input.nick + " is " + getword('adj') + '.'
  20.     else:
  21.         sentence = input.group(2)
  22.         if input.group(1) == 'libs' and input.group(2) == '!keywords':
  23.             tstring = ''
  24.             for fname in sorted(os.listdir(libsdir)):
  25.                 tstring = ', '.join([tstring, '['+fname[:-4]+']'])
  26.             tstring = tstring[2:]
  27.             phenny.say("Keywords include: "+tstring)
  28.             listfile = open(listfile_path, 'w')
  29.             listfile.write(tstring)
  30.             sentence = ""
  31.         elif input.group(1) == 'libs' and input.group(2) == '!macros':
  32.             tstring = ''
  33.             for fname in sorted(os.listdir(libsdir+'macros/')):
  34.                 tstring = ', '.join([tstring, '{'+fname[:-4]+'}'])
  35.             tstring = tstring[2:]
  36.             phenny.say("Macros include: "+tstring)
  37.             sentence = ""
  38.                 elif input.group(1) == 'libs' and input.group(2) == '!addlibs':
  39.             tstring = ''
  40.             for fname in sorted(os.listdir(libsdir+'addlibs/')):
  41.                 tstring = ', '.join([tstring, '<'+fname[:-4]+'>'])
  42.             tstring = tstring[2:]
  43.             phenny.say("Addlibs include: "+tstring)
  44.             sentence = ""
  45.         elif input.group(1) == 'libs' and input.group(2) == '!version':
  46.             phenny.say("Version 0.8c!")
  47.             sentence = ""
  48.         elif input.group(1) == 'libs' and input.group(2)[0] == '{' and input.group(2)[-1] == '}':
  49.             sentence = random.choice(open(libsdir+'macros/'+input.group(2)[1:-1]+'.txt').read().splitlines())
  50.         elif input.group(1) == 'libs' and input.group(2)[0] == '<' and input.group(2)[-1] == '>':
  51.                         sentence = open(libsdir+'addlibs/'+input.group(2)[1:-1]+'.txt').read()
  52.     phenny.say(madlibs(sentence))
  53.    
  54. def madlibs(sentence):
  55.     phrases = [Phrase()]
  56.     pi = 0
  57.     sli = 0
  58.     # separate the sentence into phrases
  59.     for c in sentence:
  60.         if c == '[':
  61.             phrases.append(Phrase())
  62.             pi+= 1
  63.             phrases[pi].text+= c
  64.         elif c == ']':
  65.             phrases[pi].text+= c
  66.             phrases.append(Phrase())
  67.             pi+= 1
  68.         else:
  69.             phrases[pi].text+= c
  70.         sli+= 1
  71.     #tag the phrases
  72.     pli = 0
  73.     for aphrase in phrases:
  74.         if aphrase.text:
  75.             if aphrase.text[0] == '[' and aphrase.text[-1] == ']':
  76.                 if not ':' in aphrase.text:
  77.                     aphrase.keyword= aphrase.text[1:-1]
  78.                 else:
  79.                     aphrase.keyword= aphrase.text[1:aphrase.text.find(':')]
  80.                     aphrase.name= aphrase.text[aphrase.text.find(':')+1:-1]
  81.     #replace appropriate phrases
  82.     wordcache = {'dumbbot': 'Miltank'}
  83.     for aphrase in phrases:
  84.         if aphrase.keyword:
  85.             if not aphrase.name:
  86.                 aphrase.text = getword(aphrase.keyword)
  87.             else:
  88.                 if wordcache.has_key(aphrase.keyword+aphrase.name):
  89.                     aphrase.text = wordcache[aphrase.keyword+aphrase.name]
  90.                 else:
  91.                     wordcache[aphrase.keyword+aphrase.name] = getword(aphrase.keyword)
  92.                     aphrase.text = wordcache[aphrase.keyword+aphrase.name]
  93.     sentence=''
  94.     for aphrase in phrases:
  95.         sentence+= aphrase.text
  96.     return sentence
  97.    
  98. def getword(wtype):
  99.     if re.match('\W', wtype):
  100.         return "[INVALID KEYWORD]"
  101.     path = libsdir+wtype.lower()+'.txt'
  102.     try:
  103.         openword = open(path).read().splitlines()
  104.     except IOError:
  105.         openword = ["[INVALID KEYWORD]"]
  106.     rword = random.choice(openword)
  107.     if not wtype[0].islower():
  108.         rword = rword.title()
  109.     if not wtype[1].islower():
  110.         rword = rword.upper()
  111.     return rword
  112.  
  113. madlibs_main.commands = ['libs','would','should']
  114. madlibs_main.priority = 'high'
  115. madlibs_main.thread = False
  116.  
  117. def addlib(phenny, input):
  118.         if not '.' in input.group(2):
  119.                 if not input.group(3):
  120.                         phenny.say('Error.')
  121.                 else:
  122.                         if os.path.exists(libsdir+'addlibs/'+input.group(2)+'.txt'):
  123.                                 phenny.say('This Madlib already exists.')
  124.                         else:
  125.                                 open((libsdir+'addlibs/'+input.group(2)+'.txt'),"w").write(input.group(3))
  126.                                 phenny.say('Madlib created.')
  127. addlib.rule = (['addlib','addlibs'],'(.*?)(?:$|\s+)(.*)')
  128. addlib.priority = 'high'
  129. addlib.thread = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement