56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
let shakespeareArray;
|
|
function preload() {
|
|
loadStrings("shakespeare.txt", handleShakespeare);
|
|
}
|
|
|
|
function setup() {
|
|
createCanvas(400, 400);
|
|
console.log(shakespeareArray);
|
|
const shakespeareTable = new Map();
|
|
for (i = 0; i < shakespeareArray.length; i++) {
|
|
const word = shakespeareArray[i];
|
|
|
|
// prettier-ignore
|
|
if (!(shakespeareTable.get(word))) { // in => all object keys
|
|
shakespeareTable.set(word, new Map());
|
|
}
|
|
wordMap = shakespeareTable.get(word);
|
|
let nextWord;
|
|
if (shakespeareArray.length > i) {
|
|
nextWord = shakespeareArray[i + 1];
|
|
}
|
|
if (wordMap.get(nextWord)) {
|
|
wordMap.set(nextWord, wordMap.get(nextWord) + 1);
|
|
} else {
|
|
wordMap.set(nextWord, 1);
|
|
}
|
|
}
|
|
console.log(shakespeareTable);
|
|
const probabilityPostfix = "___PROBABILITY___"
|
|
shakespeareTable.forEach((meowmeowmeow) => { // CONTINUE HERE
|
|
console.log("UWU")
|
|
let combinedAmount = 0
|
|
for (nextWord in word) {
|
|
combinedAmount += word.get(nextWord)
|
|
}
|
|
for (nextWord in word) {
|
|
probabilityNextWord = word.get(nextWord) / combinedAmount
|
|
word.set(nextWord + probabilityPostfix, probabilityNextWord)
|
|
console.log(probabilityNextWord)
|
|
}
|
|
})
|
|
|
|
console.log(shakespeareTable);
|
|
}
|
|
|
|
function draw() {
|
|
background(220);
|
|
}
|
|
|
|
function handleShakespeare(data) {
|
|
joinedShakespeareString = data.join(" ");
|
|
cleanedShakespearString = joinedShakespeareString.replaceAll(/\s+/g, " ");
|
|
shakespeareArray = joinedShakespeareString
|
|
.split(/\s*(\b|\W|\s)\s*/)
|
|
.filter((item) => item !== "");
|
|
}
|