-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_markov.py
More file actions
65 lines (53 loc) · 1.39 KB
/
a_markov.py
File metadata and controls
65 lines (53 loc) · 1.39 KB
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
"""
m = Markov('ab')
m.predict('a')
'b'
get_table('ab')
{'a' :{'b':1}}
"""
##import random
##import urllib.request as req
##
##def fetch_url(url):
## fin = req.urlopen(url)
## return fin.read()
##
##def write_text(txt, filename, enc='utf8'):
## with open(filename, 'w', encoding=enc) as fout:
## fout.write(txt)
## #done, fout will be closed
##
##def get_markov(filename, enc='utf8'):
## with open(filename, encoding=enc) as fin:
## txt = fin.read()
## return Markov(txt)
class Markov:
def __init__(self, data):
''' This is the constructor docstring'''
self.table = None
def predict(self, data_in):
return 'b'
## options = self.table.get(data_in, {})
## if not options:
## raise KeyError('{data_in} not found')
## possibles = []
## for dataA, count in options.items():
## for i in range(count):
## possibles.append(dataA)
## #import pdb;pdb.set_trace()
## return random.choice(possibles)
def get_table(data):
results = {}
for i , dataA in enumerate(data):
try:
dataB = data[i+1]
except IndexError:
break
char_dict = results.get(dataA, {})
char_dict.setdefault(dataB, 0)
char_dict[dataB] += 1
results[dataA] = char_dict
return results
"""
results is an empty diction
"""