-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov.py
112 lines (91 loc) · 3.64 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# markov.py
# Authors: {Annie Lin, Joanne Koong}
# Date: {December 7, 2015}
# Emails: {annielin@college.harvard.edu, joannekoong@college.harvard.edu}
# ----------------
# Our implementation of search, Markov Chains, and weighted Markov chains.
import random
import json
class Search():
def __init__(self):
self.data = []
def parse_text(self, text_file):
with open(text_file) as f:
jsons = f.read()
self.data = json.loads(jsons)
def response(self):
# Just return a random answer related to the tag
rando = random.randint(0, len(self.data) - 1)
return self.data[rando]
def searching(self, text_file):
self.parse_text(text_file)
return self.response()
class MarkovWeighting():
def __init__(self):
self.markov = []
def parse_text(self, text_file):
with open(text_file) as f:
data = f.read()
# Create a list of all words
data = [i for i in data.split(' ') if i != '']
data = [i.lower() for i in data if i.isalpha()]
self.markov = {i:[] for i in data}
pos = 0
while pos < len(data) - 1:
# Add a word to the word-key's list if it succeeds that word
self.markov[data[pos]].append(data[pos+1])
pos += 1
def seeding(self):
new = {k:v for k,v in zip(range(len(self.markov)), [i for i in self.markov])}
# Randomly pick a starting point
length_sentence = random.randint(15, 20)
seed = random.randint(0, len(new) - 1)
sentence_data = [new[seed]]
current_word = new[seed]
while len(sentence_data) < length_sentence:
if len(self.markov[current_word]) == 0:
self.seeding()
next_index = random.randint(0, len(self.markov[current_word]) - 1)
# Append the random next word to the sentence
next_word = self.markov[current_word][next_index]
sentence_data.append(next_word)
current_word = next_word
return ' '.join([i for i in sentence_data])
def marking(self, text_file):
self.parse_text(text_file)
return self.seeding()
class Markov():
def __init__(self):
self.markov = []
def parse_text(self, text_file):
with open(text_file) as f:
data = f.read()
# Create a list of all words
data = [i for i in data.split(' ') if i != '']
data = [i.lower() for i in data if i.isalpha()]
self.markov = {i:[] for i in data}
pos = 0
while pos < len(data) - 1:
# Add a word to the word-key's list if it succeeds that word
if data[pos+1] not in self.markov[data[pos]]:
self.markov[data[pos]].append(data[pos+1])
pos += 1
def seeding(self):
new = {k:v for k,v in zip(range(len(self.markov)), [i for i in self.markov])}
# Randomly pick a starting point
length_sentence = random.randint(15, 20)
seed = random.randint(0, len(new) - 1)
sentence_data = [new[seed]]
current_word = new[seed]
while len(sentence_data) < length_sentence:
if len(self.markov[current_word]) == 0:
self.seeding()
next_index = random.randint(0, len(self.markov[current_word]) - 1)
# Append the random next word to the sentence
next_word = self.markov[current_word][next_index]
sentence_data.append(next_word)
current_word = next_word
return ' '.join([i for i in sentence_data])
def marking(self, text_file):
self.parse_text(text_file)
return self.seeding()