forked from pub12/MarkovText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genMarkovText.py
54 lines (42 loc) · 1.19 KB
/
genMarkovText.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
# from nltk.tokenize import RegexpTokenizer
import nltk
import re
import pprint
import random
import sys
import getopt
import glob
import markov
def checkargs():
keyLen = 1
maxWordInSentence = 20
genNSentences = 5
fileList = []
if len(sys.argv) < 3:
print( "Usage: " + sys.argv[0] + " -w <sentence word length> -n <sentences to generate> -d <dictionary file> ")
exit(0)
else:
arg = {}
options = getopt.getopt(sys.argv[1:], 'k:w:n:d:')
for item in options[0]:
if(item):
arg[ item[0] ] = item[1]
# pprint.pprint(arg)
maxWordInSentence = int(arg[ '-w'])
genNSentences = int(arg[ '-n' ])
dictFile = arg['-d']
return( maxWordInSentence, genNSentences, dictFile)
def main(maxWordInSentence, dictFile, genNSentences=50):
#Create new Markov class
markovObj = markov.Markov(dictFile=dictFile, maxWordInSentence= maxWordInSentence)
twitterText = []
for _ in range( genNSentences ):
text = markovObj.genText()
print( text )
if len(text) <= 140 and text.endswith('.'):
twitterText.append(text)
print("\n\n")
pprint.pprint(twitterText)
if __name__ == "__main__":
(maxWordInSentence, genNSentences, dictFile) = checkargs()
main(maxWordInSentence, dictFile, genNSentences)