forked from Oneman2feet/Poem-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov.py
70 lines (54 loc) · 2 KB
/
markov.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
63
64
65
66
67
68
69
70
import random, shelve
#parsed corpus in the form of a dictionary
#with keys being the seed words
#and data being the words that have come after that word
def makecorpus(filename):
name = {}
poems = open(filename).readlines()
poems = [ [word.lower().replace('(','').replace(')','').replace('\r','').replace("--","").replace('"',"").replace('_',"")
for word in line.replace('\n',' \n').split(' ')
if word!='' and word!='\n']
for line in poems if line.strip()!="" ]
for line in poems:
for i in xrange(0,len(line)-1):
if line[i] in name.keys():
name[line[i]].append(line[i+1])
else:
name[line[i]] = [line[i+1]]
return name
def makeline(startword,corpus):
word = startword.lower()
startword = startword[0:1].upper()+startword[1:].lower()
return startword + " " + recursivemakeline(word,corpus,10)
def recursivemakeline(word,corpus,depth):
nextw = nextword(word,corpus)
return "" if (nextw=='\n' or depth == 0) else nextw + " " + recursivemakeline(nextw,corpus,depth-1)
def nextword(word,corpus):
#return a random next word based on the corpus
if word not in corpus.keys():
return '\n'
return random.choice(corpus[word])
def getSeed(corpus):
return random.choice(corpus.keys())
def makepoem(n,corpus):
poems = shelve.open("poems")
c = poems[corpus]
poem = []
for i in xrange(0,n):
poem.append(makeline(getSeed(c),c))
return poem
def makeShelve(name,filename):
poems = shelve.open("poems")
if name in poems.keys():
del poems[name]
poems[name] = makecorpus(filename)
print poems
if __name__ == '__main__':
#shakespeare = makecorpus("sonnets.txt")
#print shakespeare
#makeShelve("shakespeare","sonnets.txt")
#makeShelve("whitman","whitman.txt")
#makeShelve("poe","poe.txt")
#makeShelve("dickinson","dickinson.txt")
poems = shelve.open("poems")
#print makepoem(8,'poe')