-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateText.py
42 lines (34 loc) · 1.07 KB
/
generateText.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
from pymarkovchain import MarkovChain
import os
import sys
DATABASE_PATH = "markov_database"
def consolidateText(textDir):
accumulatedText = ""
for textFile in os.listdir(textDir):
try:
f = open(textDir + textFile, 'r')
accumulatedText += f.read()
f.close()
except:
sys.stdout.write("Error reading %s\n", textFile)
return accumulatedText
def generateTEDTalk(minWords=100):
textDir = "TED/fullTexts/"
accumulatedText = consolidateText(textDir)
markov = MarkovChain(DATABASE_PATH)
markov.generateDatabase(accumulatedText, n=3)
# Loop over sentence generation to make the talk
talk = ""
while len(talk.split()) < minWords:
talk += markov.generateString()
talk += ". "
# Add applause at the end :)
return talk.replace("(Applause)", "") + "(Applause)"
if __name__ == '__main__':
if len(sys.argv) > 1:
fname = sys.argv[1]
fout = open(fname, 'w')
fout.write(generateTEDTalk())
fout.close()
else:
print(generateTEDTalk())