Create MarkovChain. Js

This commit is contained in:
nazizmari 2023-07-26 21:51:26 +03:30 committed by GitHub
parent 4e1e9704be
commit abbfdebee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,51 @@
function markovChainGenerator(text) {
const textArr = text.split(' ')
const markovChain = {};
for (let i = 0; i < textArr.length; i++)
 {
let word = textArr[i].toLowerCase().replace(/[\W_]/, "")
if (!markovChain[word])
 {
markovChain[word] = []
}
if (textArr[i + 1]) 
{
markovChain[word].push(textArr[i +1].toLowerCase().replace(/[\W_]/, ""));
}
}
return markovChain
}
function likelyhood(chain, prev, word) 
{
const prevArr = chain[prev]
const num = prevArr.filter
(w => w === word).length
return `The likelihood that ${word} will come after ${prev} is ${num} out of ${prevArr.length}`}