-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-chains.py
executable file
·62 lines (52 loc) · 1.6 KB
/
generate-chains.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import defaultdict
import random
import re
def guess_encoding(in_text):
for enc in ('utf-7', 'utf-8', 'iso-8859-1', ):
try:
return in_text.decode(enc)
except Exception as e:
pass
with file("all.txt") as f:
text = f.read()
text = '\n'.join([guess_encoding(l) for l in text.split('\n')])
for regex, replacement in ((r'(?:\r?\n){2,}', '\n\n'), (r'\ufeff', ''), (r'\+(\w+.*\w+)\+', '\1'), ):
text = re.sub(regex, replacement, text)
chains = {}
word_splitter = re.compile('''
(
\w+(?:'\w+)? # 'normale' Woerter eventuell mit 's
| (?:St|pp)\. # oder St. und pp.
| \.\.\. # oder ...
| [,.;:!?&-] # oder Satzzeichen wie , oder .
)
''', re.X | re.U)
context = ''
for word in word_splitter.findall(text):
if context not in chains:
chains[context] = defaultdict(int)
chains[context][word] += 1
context = word
def random_word(first):
return random.choice(chains[first].keys())
def generate_sentence(first):
result = first.capitalize()
while True:
if first in ('.','!', '?'):
return result
second = random_word(first)
if second not in (',', ';', ':', '.', '!', '?'):
result += ' '
result += second
first = second
if __name__ == '__main__':
import sys
try:
num_sentences = int(sys.argv[1])
except Exception:
num_sentences = 5
print "# Starte Unsinn..."
for word in random.sample(chains.keys(), num_sentences):
print generate_sentence(word)