Advertisement
MolSno

Untitled

Feb 12th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. import os, random, re
  2.  
  3. libsdir = 'C:\\Python27\\madlibs\\'
  4. listfile_path = 'C:\\Python27\\libst.txt'
  5. macrolistfile_path = 'C:\\Python27\\madlibs\\macros.txt'
  6.  
  7. class Phrase(object):
  8.     def __init__(self, text='', keyword='', name=''):
  9.         self.text = text
  10.         self.keyword = keyword
  11.         self.name = name
  12.  
  13. def madlibs_main(phenny, input):
  14.     if not input.group(2):
  15.         sentence = input.nick + " is " + getword('adj') + '.'
  16.     else:
  17.         sentence = input.group(2)
  18.     if input.group(1) == 'would':
  19.         sentence = "I would [verb] "+random.choice(['her','his'])+" [noun]."
  20.     elif input.group(1) == 'should':
  21.         sentence = "You should [verb] [name] with a [noun]!"
  22.         elif input.group(1) == 'describe':
  23.                 sentence = "[int] "+input.nick+" is a [adj] [noun]!"
  24.        
  25.     if input.group(1) == 'libs' and input.group(2) == '!keywords':
  26.         tstring = ", ".join(os.listdir(libsdir))
  27.         phenny.say("Commands include: "+tstring)
  28.         listfile = open(listfile_path, 'w')
  29.         listfile.write(tstring)
  30.         sentence = ""
  31.     if input.group(1) == 'libs' and input.group(2) == '!macros':
  32.         tstring = ", ".join(os.listdir(libsdir+'macros\\'))
  33.         phenny.say("Macros include: "+tstring)
  34.         macrolistfile = open(macrolistfile_path, 'w')
  35.         macrolistfile.write(tstring)
  36.         sentence = ""
  37.     elif input.group(1) == 'libs' and input.group(2) == '!version':
  38.         phenny.say("Version 0.6a!")
  39.         sentence = ""
  40.     elif input.group(1) == 'libs' and input.group(2)[0] == '{' and input.group(2)[-1] == '}':
  41.         sentence = random.choice(open(libsdir+'macros\\'+input.group(2)[1:-1]+'.txt').read().splitlines())
  42.        
  43.     phenny.say(madlibs(sentence))
  44.    
  45. def madlibs(sentence):
  46.     phrases = [Phrase()]
  47.     pi = 0
  48.     sli = 0
  49.     # separate the sentence into phrases
  50.     for c in sentence:
  51.         if c == '[':
  52.             phrases.append(Phrase())
  53.             pi+= 1
  54.             phrases[pi].text+= c
  55.         elif c == ']':
  56.             phrases[pi].text+= c
  57.             phrases.append(Phrase())
  58.             pi+= 1
  59.         else:
  60.             phrases[pi].text+= c
  61.         sli+= 1
  62.     #tag the phrases
  63.     pli = 0
  64.     for aphrase in phrases:
  65.         if aphrase.text:
  66.             if aphrase.text[0] == '[' and aphrase.text[-1] == ']':
  67.                 if not ':' in aphrase.text:
  68.                     aphrase.keyword= aphrase.text[1:-1]
  69.                 else:
  70.                     aphrase.keyword= aphrase.text[1:aphrase.text.find(':')]
  71.                     aphrase.name= aphrase.text[aphrase.text.find(':')+1:-1]
  72.     #replace appropriate phrases
  73.     wordcache = {'dumbbot': 'Miltank'}
  74.     for aphrase in phrases:
  75.         if aphrase.keyword:
  76.             if not aphrase.name:
  77.                 aphrase.text = getword(aphrase.keyword)
  78.             else:
  79.                 if wordcache.has_key(aphrase.keyword+aphrase.name):
  80.                     aphrase.text = wordcache[aphrase.keyword+aphrase.name]
  81.                 else:
  82.                     wordcache[aphrase.keyword+aphrase.name] = getword(aphrase.keyword)
  83.                     aphrase.text = wordcache[aphrase.keyword+aphrase.name]
  84.     sentence=''
  85.     for aphrase in phrases:
  86.         sentence+= aphrase.text
  87.     return sentence
  88.    
  89. def getword(wtype):
  90.     if re.match('\W', wtype):
  91.         return "[INVALID KEYWORD]"
  92.     path = libsdir+wtype+'.txt'
  93.     try:
  94.         openword = open(path).read().splitlines()
  95.     except IOError:
  96.         openword = ["[INVALID KEYWORD]"]
  97.     return random.choice(openword)
  98.  
  99. madlibs_main.commands = ['libs','would','should','describe']
  100. madlibs_main.priority = 'high'
  101. madlibs_main.thread = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement