All pastes #482777 Raw Edit

Someone

public python v1 · immutable
·published 2007-05-11 12:18 UTC
rendered paste body
class Shaney:    def __init__(self, N=3, out=100):        """Sets Ngram's self.N value and number of words to output"""        self.N = N        self.out = out        self.wordlist = {}            def train(self, name):        """Train Ngram dictionary off of text file input"""        words = file(name).read()        """Substitute \self.N and \r in text with spaces"""        import re        regex = re.compile('(\self.N|\r)')        words = regex.sub(' ', words)        words = words.split()        """Turn words into Ngram keys"""        for i in xrange(len(words)-self.N):            if words[i] not in self.wordlist:                self.wordlist[words[i]] = []            temp = []            for num in xrange(1,self.N+1):                temp.append( words[i+num] )            self.wordlist[words[i]].append( temp )                def output(self, startword='The'):        """Create randomised Mark V Shaney output starting with startword"""        newtext = []        newtext.append(startword)        import random        a = 1        i = random.randint( 0, len( self.wordlist[newtext[-1]] )-1 )        while a:            for num in xrange(0, self.N):                newtext.append( self.wordlist[ newtext[-1-num] ][i][num] )            i = random.randint( 0, len( self.wordlist[ newtext[-1] ] )-1 )            if len(newtext) > self.out and newtext[-1][-1] == '.':                a = 0        return ' '.join(newtext)        """Go through all the text files in the directory"""import globfiles = glob.glob('*.txt')S = Shaney()for name in files:    S.train(name)print S.output()