-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (72 loc) · 2.47 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
import os
from pprint import pprint
import sys
from Corpus import Corpus
from importFile import importStrFromFile
from MarkovBot import MarkovBot
from MarkovDict import MarkovDict
def corporaPaths():
corpora_root = "./corpora/"
corpora_filenames = [
# "Sorcerers-Stone-78500-words.txt"
"hunger-games.txt"
# "test/depth.txt"
]
corpora_paths = [corpora_root+filename for filename in corpora_filenames]
# exceptions = [
# "Sorcerers-Stone-78500-words.txt"
# ]
# corpora_paths = [
# corpora_root + filename
# for filename in os.listdir(corpora_root)
# if filename not in exceptions
# ]
return corpora_paths
def prompt():
return input(">>> ")
def getDepth():
if len(sys.argv) < 2:
return 1
if sys.argv[1] == "--depth":
return int(sys.argv[2])
if len(sys.argv) > 3 and sys.argv[2] == "--depth":
return int(sys.argv[3])
return 1
def main():
print("Creating new markov dict...")
forward_markov_dict = MarkovDict(source=None, depth=getDepth())
reverse_markov_dict = MarkovDict(source=None, depth=getDepth())
print("Starting for loop to add corpora...")
for corpus_path in corporaPaths():
corpus = Corpus(importStrFromFile(corpus_path))
print("Adding corpus with path '" + corpus_path + "'...")
forward_markov_dict.add(corpus)
reverse_markov_dict.add(list(reversed(corpus)))
print("Initializing MarkovBot...")
bot = MarkovBot(forward_markov_dict, reverse_markov_dict)
print("\nWelcome to MarkovBot! Type a message. Type 'exit()' to quit.")
message = prompt()
while message != "exit()":
print(bot.response(topic=message.split()[0]))
message = prompt()
def test():
print("Creating new markov dict...")
print(getDepth())
corpus_path = "./corpora/test/depth.txt"
corpus = Corpus(importStrFromFile(corpus_path))
print(corpus)
reverse_corpus = list(reversed(corpus))
print(reverse_corpus)
forward_markov_dict = MarkovDict(source=corpus, depth=getDepth())
reverse_markov_dict = MarkovDict(source=reverse_corpus, depth=getDepth())
pprint(forward_markov_dict.dict)
pprint(reverse_markov_dict.dict)
bot = MarkovBot(forward_markov_dict, reverse_markov_dict)
# pprint(bot.forward_dict.dict)
# pprint(bot.reverse_dict.dict)
print(bot.response(topic="markov"))
if len(sys.argv) > 1 and sys.argv[1] == "--test":
test()
else:
main()