13:0ab9317bbd38
Anton Shestakov <engored@ya.ru>, Sat, 15 Feb 2014 22:15:56 +0900
DissociatedPressman.

dissociated-pressman/dissociated.py

Permissions: -rw-r--r--

Other formats: Feeds:
import random
import re
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())
chains = {}
starts = set()
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():
starts.add(word)
self.chains = chains
self.starts = list(starts)
def generate(self, words=100, style=True):
word = random.choice(self.starts)
chain = self.chains[word]
result = [word]
while len(result) < words or (style and not word.endswith(('.', '!', '?'))):
word = random.choice(chain)
result.append(word)
chain = self.chains[word]
return ' '.join(result)
if __name__ == '__main__':
pressman = DissociatedPressman('ulysses.txt')
print(pressman.generate())