Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, random, re
- libsdir = '/root/phenny/madlibs/'
- listfile_path = '/root/phenny/libst.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):
- if input.group(1) == 'would':
- sentence = "I would [verb] "+random.choice(['her','his'])+" [noun]"
- elif input.group(1) == 'should':
- sentence = "You should [verb] me with a(n) [adj] [noun]!"
- else:
- sentence = input.nick + " is " + getword('adj') + '.'
- else:
- sentence = input.group(2)
- if input.group(1) == 'libs' and input.group(2) == '!keywords':
- tstring = ''
- for fname in sorted(os.listdir(libsdir)):
- tstring = ', '.join([tstring, '['+fname[:-4]+']'])
- tstring = tstring[2:]
- phenny.say("Keywords include: "+tstring)
- listfile = open(listfile_path, 'w')
- listfile.write(tstring)
- sentence = ""
- elif input.group(1) == 'libs' and input.group(2) == '!macros':
- tstring = ''
- for fname in sorted(os.listdir(libsdir+'macros/')):
- tstring = ', '.join([tstring, '{'+fname[:-4]+'}'])
- tstring = tstring[2:]
- phenny.say("Macros include: "+tstring)
- sentence = ""
- elif input.group(1) == 'libs' and input.group(2) == '!addlibs':
- tstring = ''
- for fname in sorted(os.listdir(libsdir+'addlibs/')):
- tstring = ', '.join([tstring, '<'+fname[:-4]+'>'])
- tstring = tstring[2:]
- phenny.say("Addlibs include: "+tstring)
- sentence = ""
- elif input.group(1) == 'libs' and input.group(2) == '!version':
- phenny.say("Version 0.8c!")
- 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())
- elif input.group(1) == 'libs' and input.group(2)[0] == '<' and input.group(2)[-1] == '>':
- sentence = open(libsdir+'addlibs/'+input.group(2)[1:-1]+'.txt').read()
- 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.lower()+'.txt'
- try:
- openword = open(path).read().splitlines()
- except IOError:
- openword = ["[INVALID KEYWORD]"]
- rword = random.choice(openword)
- if not wtype[0].islower():
- rword = rword.title()
- if not wtype[1].islower():
- rword = rword.upper()
- return rword
- madlibs_main.commands = ['libs','would','should']
- madlibs_main.priority = 'high'
- madlibs_main.thread = False
- def addlib(phenny, input):
- if not '.' in input.group(2):
- if not input.group(3):
- phenny.say('Error.')
- else:
- if os.path.exists(libsdir+'addlibs/'+input.group(2)+'.txt'):
- phenny.say('This Madlib already exists.')
- else:
- open((libsdir+'addlibs/'+input.group(2)+'.txt'),"w").write(input.group(3))
- phenny.say('Madlib created.')
- addlib.rule = (['addlib','addlibs'],'(.*?)(?:$|\s+)(.*)')
- addlib.priority = 'high'
- addlib.thread = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement