-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkov_elem.py
33 lines (27 loc) · 1.06 KB
/
Markov_elem.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
import numpy as np
class Markov_elem:
def __init__(self, word):
self._word = word
self._followings = []
self._following_nbr = []
self._following_proba = []
def add_following(self, following_word):
if following_word in self._followings:
self._following_nbr[self._followings.index(following_word)] += 1
self._followings.append(following_word)
self._following_nbr.append(1)
def calc_following_proba(self):
all = sum(self._following_nbr)
self._following_proba = []
for nbr in self._following_nbr:
self._following_proba.append(nbr / all)
def get_following_proba(self, word):
if word not in self._followings:
return 0
return self._following_proba[self._followings.index(word)]
def pick_next(self):
if not self._following_proba:
raise Exception("Can't pick word without it's probability")
return np.random.choice(self._followings, 1, p=self._following_proba)[0]
def getword(self):
return self._word