#!/usr/bin/python import urllib2 from numpy.random import random, randint def getOne(theList): i = randint(len(theList)) return theList[i] def randomNounphrase(allLists): # make up a random noun phrase using the lists. # 2/5 of the time: noun. # 2/5 of the time: adjective noun. # 1/5 of the time: adjective adjective noun. [firstnameList, lastnameList, locationList, institutionList, institutionModifierList, adjectiveList, adverbList, nounList, personalTitleList, suffixList, templateList] = allLists r = random() if (r < 0.4): return getOne(nounList) elif (r < 0.8): return getOne(adjectiveList) + ' ' + getOne(nounList) else: return getOne(adjectiveList) + ' ' + getOne(adjectiveList) + ' ' + getOne(nounList) def extractListFromURL(theURL): str = urllib2.urlopen(theURL).read() str = str[str.find('STARTLIST'): str.find('ENDLIST')] # isolate the list... str = str.lstrip('STARTLIST') # chop off this word myList = str.split(',') for i,x in enumerate(myList): myList[i] = x.strip() return myList """ Main program starts here """ # Suck in all those lists. prefixURL = 'http://ecs.victoria.ac.nz/Groups/AI/' firstnameList = extractListFromURL(prefixURL + 'TitleGeneratorFirstNames') lastnameList = extractListFromURL(prefixURL + 'TitleGeneratorLastNames') locationList = extractListFromURL(prefixURL + 'TitleGeneratorLocations') institutionList = extractListFromURL(prefixURL + 'TitleGeneratorInstitutions') institutionModifierList = extractListFromURL(prefixURL + 'TitleGeneratorInstitution-modifiers') adjectiveList = extractListFromURL(prefixURL + 'TitleGeneratorAdjectives') adverbList = extractListFromURL(prefixURL + 'TitleGeneratorAdverbs') nounList = extractListFromURL(prefixURL + 'TitleGeneratorNouns') personalTitleList = extractListFromURL(prefixURL + 'TitleGeneratorTitles') suffixList = extractListFromURL(prefixURL + 'TitleGeneratorSuffixes') templateList = extractListFromURL(prefixURL + 'TitleGeneratorTemplates') allLists = [firstnameList, lastnameList, locationList, institutionList, institutionModifierList, adjectiveList, adverbList, nounList, personalTitleList, suffixList, templateList] for i in range(10): # invent a speaker person = getOne(personalTitleList) person = person + ' ' + getOne(firstnameList) person = person + ' ' + getOne(lastnameList) + ',' if random() < 0.5: person = person + ' ' + getOne(institutionModifierList) person = person + ' ' + getOne(institutionList) + ' of' person = person + ' ' + getOne(locationList) + ' - ' # pick a template for the title talk = templateList[randint(len(templateList))] out = talk.split() title = '' for word in out: if (word == '{nounphrase}'): title = title + randomNounphrase(allLists) elif (word == '{nounphrase}:'): title = title + randomNounphrase(allLists) + ':' elif (word == '{nounphrase}.'): title = title + randomNounphrase(allLists) + '.' elif (word == '{adjective}'): title = title + getOne(adjectiveList) else: title = title + word title = title + ' ' print '' print person print title