markovChain

This commit is contained in:
Jürg Hallenbarter
2026-05-13 08:52:30 +02:00
parent f80711c391
commit 897f63fa7c
8 changed files with 493953 additions and 46 deletions

View File

@@ -4,28 +4,38 @@ let wordListPlain = [];
let startWord = "";
let finalText = "";
let cleanedText = "";
let wordToIndex = {};
async function setup() {
trainingText = await loadStrings('assets/text/cpdv_text_only.txt');
createCanvas(windowWidth, windowHeight)
background(255, 255, 230)
trainingText = await loadStrings('assets/text/TheSubspaceEmissaryWorldConquest.txt');
createCanvas(windowWidth, windowHeight);
background(255, 255, 230);
console.log(trainingText);
trainingText = splitText(trainingText);
console.log(trainingText);
for (i = 0; i < trainingText.length; i++) {
if (wordList.indexOf(trainingText[i]) == -1) {
if (i % 1000 == 0) {
console.log('i',i);
}
wordList.push([trainingText[i], []]);
wordListPlain.push(trainingText[i]);
for (j = i; j < trainingText.length; j++) {
if (trainingText[j] == wordList[wordList.length - 1][0]) {
if (trainingText[j + 1] != undefined){
wordList[wordList.length - 1][1].push(trainingText[j + 1]);
}
}
}
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);
}
}
@@ -33,53 +43,56 @@ async function setup() {
startWord = wordList[round(random(0, wordList.length - 1))][0];
finalText = startWord;
for (i = 0; i < 100; i++) {
for (let i = 0; i < 100; i++) {
startWord = generateNextWord(startWord);
console.log(startWord)
finalText += " " + startWord;
console.log(startWord);
if (startWord === '.' || startWord === ',' || startWord === ';' || startWord === ':' || startWord === '!' || startWord === '?') {
finalText += startWord;
} else {
finalText += " " + startWord;
}
}
textSize(19)
console.log(finalText)
text(finalText, 10, height / 2)
textSize(19);
console.log(finalText);
text(finalText, width / 2-400, height / 2, 800, height);
}
function splitText(text) {
for (i = 0; i < text.length; i++) {
cleanedText = "";
for (let i = 0; i < text.length; i++) {
cleanedText += " " + text[i];
}
cleanedText = cleanedText
.replaceAll(".", " . ")
.replaceAll("!", " ! ")
.replaceAll("?", " ? ")
.replaceAll(",", " , ")
.replaceAll(";", " ; ")
.replaceAll(":", " : ")
.replaceAll('"', ' " ')
.replaceAll("'", " ' ")
.replaceAll(" "," ")
.replaceAll(" "," ")
.split(" ");
.replaceAll(".", " . ")
.replaceAll("!", " ! ")
.replaceAll("?", " ? ")
.replaceAll(",", " , ")
.replaceAll(";", " ; ")
.replaceAll(":", " : ")
.replaceAll('"', ' " ')
.replaceAll("'", " ' ")
.replaceAll("`", " ` ")
.replaceAll("´", " ´ ")
.replaceAll(" ", " ")
.replaceAll(" ", " ")
.split(" ");
return cleanedText;
}
function generateNextWord(currentWord) {
let currentWordIndex = getWordIndex(currentWord);
let nextWordIndex = round(
random(
0, wordList[currentWordIndex][1].length - 1
)
);
//retun . if end of text
if (wordList[currentWordIndex][1][nextWordIndex] == undefined){
// If no possible next words, return "."
if (wordList[currentWordIndex][1].length === 0) {
return '.';
}
return wordList[currentWordIndex][1][nextWordIndex];
let nextWordIndex = floor(random(0, wordList[currentWordIndex][1].length));
let nextWord = wordList[currentWordIndex][1][nextWordIndex];
return nextWord !== undefined ? nextWord : '.';
}
function getWordIndex(word) {
return wordListPlain.indexOf(word);
return wordToIndex[word];
}
function draw() {