Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, random, re
- libsdir = 'C:\\Python27\\madlibs\\'
- listfile_path = 'C:\\Python27\\libst.txt'
- macrolistfile_path = 'C:\\Python27\\madlibs\\macros.txt'
- class Phrase(object):
- def __init__(self, text='', keyword='', name=''):
- self.text = text
- self.keyword = keyword
- self.name = name
- def madlibs_main(phenny, input):
- if not input.group(2):
- sentence = input.nick + " is " + getword('adj') + '.'
- else:
- sentence = input.group(2)
- if input.group(1) == 'would':
- sentence = "I would [verb] "+random.choice(['her','his'])+" [noun]."
- elif input.group(1) == 'should':
- sentence = "You should [verb] [name] with a [noun]!"
- elif input.group(1) == 'describe':
- sentence = "[int] "+input.nick+" is a [adj] [noun]!"
- if input.group(1) == 'libs' and input.group(2) == '!keywords':
- tstring = ", ".join(os.listdir(libsdir))
- phenny.say("Commands include: "+tstring)
- listfile = open(listfile_path, 'w')
- listfile.write(tstring)
- sentence = ""
- if input.group(1) == 'libs' and input.group(2) == '!macros':
- tstring = ", ".join(os.listdir(libsdir+'macros\\'))
- phenny.say("Macros include: "+tstring)
- macrolistfile = open(macrolistfile_path, 'w')
- macrolistfile.write(tstring)
- sentence = ""
- elif input.group(1) == 'libs' and input.group(2) == '!version':
- phenny.say("Version 0.6a!")
- sentence = ""
- elif input.group(1) == 'libs' and input.group(2)[0] == '{' and input.group(2)[-1] == '}':
- sentence = random.choice(open(libsdir+'macros\\'+input.group(2)[1:-1]+'.txt').read().splitlines())
- phenny.say(madlibs(sentence))
- def madlibs(sentence):
- phrases = [Phrase()]
- pi = 0
- sli = 0
- # separate the sentence into phrases
- for c in sentence:
- if c == '[':
- phrases.append(Phrase())
- pi+= 1
- phrases[pi].text+= c
- elif c == ']':
- phrases[pi].text+= c
- phrases.append(Phrase())
- pi+= 1
- else:
- phrases[pi].text+= c
- sli+= 1
- #tag the phrases
- pli = 0
- for aphrase in phrases:
- if aphrase.text:
- if aphrase.text[0] == '[' and aphrase.text[-1] == ']':
- if not ':' in aphrase.text:
- aphrase.keyword= aphrase.text[1:-1]
- else:
- aphrase.keyword= aphrase.text[1:aphrase.text.find(':')]
- aphrase.name= aphrase.text[aphrase.text.find(':')+1:-1]
- #replace appropriate phrases
- wordcache = {'dumbbot': 'Miltank'}
- for aphrase in phrases:
- if aphrase.keyword:
- if not aphrase.name:
- aphrase.text = getword(aphrase.keyword)
- else:
- if wordcache.has_key(aphrase.keyword+aphrase.name):
- aphrase.text = wordcache[aphrase.keyword+aphrase.name]
- else:
- wordcache[aphrase.keyword+aphrase.name] = getword(aphrase.keyword)
- aphrase.text = wordcache[aphrase.keyword+aphrase.name]
- sentence=''
- for aphrase in phrases:
- sentence+= aphrase.text
- return sentence
- def getword(wtype):
- if re.match('\W', wtype):
- return "[INVALID KEYWORD]"
- path = libsdir+wtype+'.txt'
- try:
- openword = open(path).read().splitlines()
- except IOError:
- openword = ["[INVALID KEYWORD]"]
- return random.choice(openword)
- madlibs_main.commands = ['libs','would','should','describe']
- madlibs_main.priority = 'high'
- madlibs_main.thread = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement