Alustava toteutus Markovin ketjuille
This commit is contained in:
parent
d956305fb0
commit
7dddb59c31
3 changed files with 46 additions and 1 deletions
|
@ -17,6 +17,8 @@ Seuraavan nuotin luominen Markovin ketjulla vaatii kaikkien mahdollisten seuraav
|
||||||
## Lähteet (Tähän asti)
|
## Lähteet (Tähän asti)
|
||||||
[Wikipedia: Trie](https://en.wikipedia.org/wiki/Trie)
|
[Wikipedia: Trie](https://en.wikipedia.org/wiki/Trie)
|
||||||
|
|
||||||
|
[Wikipedia: Markov chain](https://en.wikipedia.org/wiki/Markov_chain)
|
||||||
|
|
||||||
[Albert Au Yeung "Implementing Trie in Python" (Luettu 15.09.2022)](https://albertauyeung.github.io/2020/06/15/python-trie.html/#implementing-trie-in-python-1)
|
[Albert Au Yeung "Implementing Trie in Python" (Luettu 15.09.2022)](https://albertauyeung.github.io/2020/06/15/python-trie.html/#implementing-trie-in-python-1)
|
||||||
|
|
||||||
[Vijaykrishna Ram "Trie Data Structure in C/C++" (Luettu 15.09.2022)](https://www.digitalocean.com/community/tutorials/trie-data-structure-in-c-plus-plus)
|
[Vijaykrishna Ram "Trie Data Structure in C/C++" (Luettu 15.09.2022)](https://www.digitalocean.com/community/tutorials/trie-data-structure-in-c-plus-plus)
|
||||||
|
|
43
src/markov_ketju.py
Normal file
43
src/markov_ketju.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
from trie import Trie
|
||||||
|
from collections import deque
|
||||||
|
from random import choices
|
||||||
|
|
||||||
|
class MarkovKetju:
|
||||||
|
def __init__(self, aste=1, trie=Trie()):
|
||||||
|
self._trie = trie
|
||||||
|
self._aste = aste
|
||||||
|
self._menneet_tilat = deque()
|
||||||
|
|
||||||
|
def kasittele_opetusdata(self, opetusdata: list):
|
||||||
|
self._trie = Trie()
|
||||||
|
|
||||||
|
for jono in opetusdata:
|
||||||
|
if len(jono) > self._aste:
|
||||||
|
alkio = deque(jono[0:self._aste+1])
|
||||||
|
self._trie.lisaa(alkio)
|
||||||
|
|
||||||
|
for i in range(self._aste+1, len(jono)):
|
||||||
|
alkio.popleft()
|
||||||
|
alkio.append(jono[i])
|
||||||
|
self._trie.lisaa(alkio)
|
||||||
|
|
||||||
|
def aseta_alkuosa(self, alkuosa):
|
||||||
|
if len(alkuosa) < self._aste:
|
||||||
|
raise ValueError("Liian lyhyt alkuosa!")
|
||||||
|
self._menneet_tilat = deque([alkuosa[i] for i in range(0, self._aste)])
|
||||||
|
|
||||||
|
def seuraava(self):
|
||||||
|
if len(self._menneet_tilat) < self._aste:
|
||||||
|
raise ValueError("Markovin ketjulle ei ole asetettu sopivaa alkuosaa!")
|
||||||
|
|
||||||
|
todennakoisyydet, merkit = self._trie.etsi_seuraavat(self._menneet_tilat)
|
||||||
|
|
||||||
|
if todennakoisyydet and merkit:
|
||||||
|
seuraava = choices(merkit, weights=todennakoisyydet)[0]
|
||||||
|
|
||||||
|
self._menneet_tilat.popleft()
|
||||||
|
self._menneet_tilat.append(seuraava)
|
||||||
|
|
||||||
|
return seuraava
|
||||||
|
|
||||||
|
return None
|
|
@ -74,7 +74,7 @@ class Trie:
|
||||||
#Etsitään alkuosaa vastaava solmu
|
#Etsitään alkuosaa vastaava solmu
|
||||||
for merkki in alkuosa:
|
for merkki in alkuosa:
|
||||||
if merkki not in solmu.lapset:
|
if merkki not in solmu.lapset:
|
||||||
return []
|
return (None, None)
|
||||||
solmu = solmu.lapset[merkki]
|
solmu = solmu.lapset[merkki]
|
||||||
|
|
||||||
#Listaa kaikki alkuosan lapset
|
#Listaa kaikki alkuosan lapset
|
||||||
|
|
Reference in a new issue