Download:
child 14:26228d16d36a
parent 12:91034d51100c
13:0ab9317bbd38
Anton Shestakov <engored@ya.ru>, Sat, 15 Feb 2014 22:15:56 +0900
DissociatedPressman.

2 файлов изменено, 58 вставок(+), 0 удалений(-) [+]
dissociated-pressman/README.md file | annotate | diff | comparison | revisions
dissociated-pressman/dissociated.py file | annotate | diff | comparison | revisions
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/dissociated-pressman/README.md Sat Feb 15 22:15:56 2014 +0900
@@ -0,0 +1,21 @@
+ Dissociated press is an algorithm for generating text based on another
+ text. It is intended for transforming any text into potentially humorous
+ garbage.
+
+Implementation is not the best, as it only checks the last word, which produces
+pretty disjointed text as a result. But it tries to compensate for that; it
+doesn't split English articles from words, it looks for potentail sentence
+starters and tries to end a chunk of words with a proper punctuation.
+
+Here are some examples:
+
+ Asculum, Stephen with her, blind too, old cocky. I got mummy's Iovely box
+ head and S. Laurence O'Toole's. But he wouldnt pay ten millions of damp
+ earth. The cold gizzard.
+
+ Bacon, as, you wait. Drained all do with the toes down. Never heard or see
+ her limp between undue clemency and H. J. and Dubosc. You horrid thing! And
+ then gazes far as it apparently disregarding the warm pressure from
+ ORourkes was coming into the sunlight through the backdoor into her letter
+ on there. Visit some place I went by Wine's antiques, in Lunnon in front
+ of him want to accepted on my young laugh too open on high in the bed.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/dissociated-pressman/dissociated.py Sat Feb 15 22:15:56 2014 +0900
@@ -0,0 +1,37 @@
+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())