Files
p5js/sketches/MarkovChain.js
2026-06-01 11:33:23 +02:00

103 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
let trainingText = "";
let wordList = [];
let wordListPlain = [];
let startWord = "";
let finalText = "";
let cleanedText = "";
let wordToIndex = {};
async function setup() {
trainingText = await loadStrings('assets/text/50shades.txt');
createCanvas(windowWidth, windowHeight);
background(255, 255, 230);
console.log(trainingText);
trainingText = splitText(trainingText);
console.log(trainingText);
wordToIndex = {};
wordList = [];
wordListPlain = [];
for (let i = 0; i < trainingText.length; i++) {
let word = trainingText[i];
let nextWord = trainingText[i + 1];
if (wordToIndex[word] === undefined) {
wordToIndex[word] = wordList.length;
wordList.push([word, []]);
wordListPlain.push(word);
}
if (nextWord !== undefined) {
let idx = wordToIndex[word];
wordList[idx][1].push(nextWord);
}
if (i % 1000 === 0) {
console.log('i', i);
}
}
console.log(wordList);
startWord = wordList[round(random(0, wordList.length - 1))][0];
finalText = startWord;
for (let i = 0; i < 100; i++) {
startWord = generateNextWord(startWord);
console.log(startWord);
if (startWord === '.' || startWord === ',' || startWord === ';' || startWord === ':' || startWord === '!' || startWord === '?') {
finalText += startWord;
} else {
finalText += " " + startWord;
}
}
textSize(windowHeight / 18);
console.log(finalText);
text(finalText, 10, 10, windowWidth-20, windowHeight-20);
}
function splitText(text) {
cleanedText = "";
for (let i = 0; i < text.length; i++) {
cleanedText += " " + text[i];
}
cleanedText = cleanedText
.replaceAll(".", " . ")
.replaceAll("!", " ! ")
.replaceAll("?", " ? ")
.replaceAll(",", " , ")
.replaceAll(";", " ; ")
.replaceAll(":", " : ")
.replaceAll('"', ' " ')
.replaceAll("'", " ' ")
.replaceAll("`", " ` ")
.replaceAll("´", " ´ ")
.replaceAll(" ", " ")
.replaceAll(" ", " ")
.split(" ");
return cleanedText;
}
function generateNextWord(currentWord) {
let currentWordIndex = getWordIndex(currentWord);
// If no possible next words, return "."
if (wordList[currentWordIndex][1].length === 0) {
return '.';
}
let nextWordIndex = floor(random(0, wordList[currentWordIndex][1].length));
let nextWord = wordList[currentWordIndex][1][nextWordIndex];
return nextWord !== undefined ? nextWord : '.';
}
function getWordIndex(word) {
return wordToIndex[word];
}
function draw() {
}
function mousePressed() {
}