Anton Shestakov <engored@ya.ru>, Sat, 15 Feb 2014 22:15:56 +0900
DissociatedPressman.
dissociated-pressman/dissociated.py
Permissions: -rw-r--r--
WORD_RE = re.compile(r'''(?:(?:an?|the) )?[\w']+[.,:;!?—-]*''', re.U | re.M) class DissociatedPressman: def __init__(self, filename): with open(filename, 'r') as f: source = WORD_RE.findall(f.read()) for index, word in enumerate(source): next_index = (index + 1) % len(source) chains.setdefault(word, []).append(source[next_index]) if word[0] == word[0].upper(): self.starts = list(starts) def generate(self, words=100, style=True): word = random.choice(self.starts) chain = self.chains[word] while len(result) < words or (style and not word.endswith(('.', '!', '?'))): word = random.choice(chain) chain = self.chains[word] if __name__ == '__main__': pressman = DissociatedPressman('ulysses.txt') print(pressman.generate())