markovChain
This commit is contained in:
16166
assets/text/50shades.txt
Normal file
16166
assets/text/50shades.txt
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
346651
assets/text/TheSubspaceEmissaryWorldConquest.txt
Normal file
346651
assets/text/TheSubspaceEmissaryWorldConquest.txt
Normal file
File diff suppressed because one or more lines are too long
8268
assets/text/goete.txt
Normal file
8268
assets/text/goete.txt
Normal file
File diff suppressed because it is too large
Load Diff
121743
assets/text/shakespeare.txt
Normal file
121743
assets/text/shakespeare.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -8,3 +8,33 @@ they are they.
|
|||||||
we are we.
|
we are we.
|
||||||
are we good people?
|
are we good people?
|
||||||
this is the end.
|
this is the end.
|
||||||
|
Luke, I am your Father.
|
||||||
|
Where am I?
|
||||||
|
They shall learn to speak in porper english.
|
||||||
|
Where the tips touch there the sins begin.
|
||||||
|
As a hard working person, I do not work at all.
|
||||||
|
There is no such thing as free will.
|
||||||
|
20 bugs is 20 bugs.
|
||||||
|
Then end of the World is yesterday evening.
|
||||||
|
Tomorow morning is Sunday.
|
||||||
|
Sunday is a good day.
|
||||||
|
You are a horrible person.
|
||||||
|
This boxing sack is very hard.
|
||||||
|
Let a bro hit.
|
||||||
|
This is not it.
|
||||||
|
This is it.
|
||||||
|
Where is your Father?
|
||||||
|
Good evenning and Hello.
|
||||||
|
I am in danger!
|
||||||
|
At golfing you need to be good at putting.
|
||||||
|
A hole is very deep.
|
||||||
|
Your hole you dig there is very deep.
|
||||||
|
This is hard work.
|
||||||
|
Thus we shall work together.
|
||||||
|
Thus you are a bad person.
|
||||||
|
What did he say to you?
|
||||||
|
What is she yapping about?
|
||||||
|
Are they here yet?
|
||||||
|
Are we anticheat yet?
|
||||||
|
Luke is my coworker.
|
||||||
|
This is very hard.
|
||||||
122
sketches/GameOfLive.js
Normal file
122
sketches/GameOfLive.js
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
let entities = [];
|
||||||
|
let entityWidth = 10;
|
||||||
|
|
||||||
|
function setup() {
|
||||||
|
createCanvas(windowWidth, windowHeight);
|
||||||
|
|
||||||
|
fill(20,150,200)
|
||||||
|
|
||||||
|
// Glider
|
||||||
|
entities.push({ x: 0, y: 0 });
|
||||||
|
entities.push({ x: entityWidth, y: 0 });
|
||||||
|
entities.push({ x: entityWidth * 2, y: 0 });
|
||||||
|
entities.push({ x: 0, y: entityWidth });
|
||||||
|
entities.push({ x: entityWidth, y: entityWidth * 2 });
|
||||||
|
|
||||||
|
entities.push({ x: entityWidth * 3, y: entityWidth * 4 });
|
||||||
|
entities.push({ x: entityWidth * 4, y: entityWidth * 4 });
|
||||||
|
entities.push({ x: entityWidth * 5, y: entityWidth * 4 });
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw() {
|
||||||
|
background(255);
|
||||||
|
drawEntities();
|
||||||
|
runLife();
|
||||||
|
summonRandomCubes(50, width);
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawEntities() {
|
||||||
|
for (let cell of entities) {
|
||||||
|
if (cell.x < -width/2 || cell.x > width/2 || cell.y < -height/2 || cell.y > height/2){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
square(width / 2 + cell.x, height / 2 + cell.y, entityWidth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function runLife() {
|
||||||
|
let next = [];
|
||||||
|
|
||||||
|
let candidates = [];
|
||||||
|
for (let cell of entities) {
|
||||||
|
if (!isInArray(candidates, cell.x, cell.y)) {
|
||||||
|
candidates.push({ x: cell.x, y: cell.y });
|
||||||
|
}
|
||||||
|
for (let dx = -1; dx <= 1; dx++) {
|
||||||
|
for (let dy = -1; dy <= 1; dy++) {
|
||||||
|
if (dx === 0 && dy === 0) continue;
|
||||||
|
let nx = cell.x + dx * entityWidth;
|
||||||
|
let ny = cell.y + dy * entityWidth;
|
||||||
|
if (!isInArray(candidates, nx, ny)) {
|
||||||
|
candidates.push({ x: nx, y: ny });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply rules
|
||||||
|
for (let cand of candidates) {
|
||||||
|
let neighbors = 0;
|
||||||
|
for (let cell of entities) {
|
||||||
|
let dx = Math.abs(cand.x - cell.x);
|
||||||
|
let dy = Math.abs(cand.y - cell.y);
|
||||||
|
if ((dx === entityWidth && dy === 0) ||
|
||||||
|
(dx === 0 && dy === entityWidth) ||
|
||||||
|
(dx === entityWidth && dy === entityWidth)) {
|
||||||
|
neighbors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let isAlive = isInArray(entities, cand.x, cand.y);
|
||||||
|
if (isAlive && (neighbors === 2 || neighbors === 3)) {
|
||||||
|
next.push({ x: cand.x, y: cand.y });
|
||||||
|
}
|
||||||
|
if (!isAlive && neighbors === 3) {
|
||||||
|
next.push({ x: cand.x, y: cand.y });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entities = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isInArray(arr, x, y) {
|
||||||
|
for (let item of arr) {
|
||||||
|
if (item.x === x && item.y === y) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mousePressed() {
|
||||||
|
let newX = Math.round((mouseX - width / 2) / entityWidth) * entityWidth;
|
||||||
|
let newY = Math.round((mouseY - height / 2) / entityWidth) * entityWidth;
|
||||||
|
|
||||||
|
let alreadyExists = false;
|
||||||
|
for (let cell of entities) {
|
||||||
|
if (cell.x === newX && cell.y === newY) {
|
||||||
|
alreadyExists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!alreadyExists) {
|
||||||
|
entities.push({ x: newX, y: newY });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function summonRandomCubes(amount, area){
|
||||||
|
for (let i = 0; i < amount; i++){
|
||||||
|
let newX = Math.round((random(-area, area) - width / 2) / entityWidth) * entityWidth;
|
||||||
|
let newY = Math.round((random(-area, area) - height / 2) / entityWidth) * entityWidth;
|
||||||
|
let alreadyExists = false;
|
||||||
|
for (let cell of entities) {
|
||||||
|
if (cell.x === newX && cell.y === newY) {
|
||||||
|
alreadyExists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!alreadyExists) {
|
||||||
|
entities.push({ x: newX, y: newY });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function windowResized() {
|
||||||
|
resizeCanvas(windowWidth, windowHeight);
|
||||||
|
}
|
||||||
@@ -4,28 +4,38 @@ let wordListPlain = [];
|
|||||||
let startWord = "";
|
let startWord = "";
|
||||||
let finalText = "";
|
let finalText = "";
|
||||||
let cleanedText = "";
|
let cleanedText = "";
|
||||||
|
let wordToIndex = {};
|
||||||
|
|
||||||
async function setup() {
|
async function setup() {
|
||||||
trainingText = await loadStrings('assets/text/cpdv_text_only.txt');
|
trainingText = await loadStrings('assets/text/TheSubspaceEmissaryWorldConquest.txt');
|
||||||
createCanvas(windowWidth, windowHeight)
|
createCanvas(windowWidth, windowHeight);
|
||||||
background(255, 255, 230)
|
background(255, 255, 230);
|
||||||
|
|
||||||
console.log(trainingText);
|
console.log(trainingText);
|
||||||
trainingText = splitText(trainingText);
|
trainingText = splitText(trainingText);
|
||||||
console.log(trainingText);
|
console.log(trainingText);
|
||||||
for (i = 0; i < trainingText.length; i++) {
|
|
||||||
if (wordList.indexOf(trainingText[i]) == -1) {
|
wordToIndex = {};
|
||||||
if (i % 1000 == 0) {
|
wordList = [];
|
||||||
console.log('i',i);
|
wordListPlain = [];
|
||||||
}
|
|
||||||
wordList.push([trainingText[i], []]);
|
for (let i = 0; i < trainingText.length; i++) {
|
||||||
wordListPlain.push(trainingText[i]);
|
let word = trainingText[i];
|
||||||
for (j = i; j < trainingText.length; j++) {
|
let nextWord = trainingText[i + 1];
|
||||||
if (trainingText[j] == wordList[wordList.length - 1][0]) {
|
|
||||||
if (trainingText[j + 1] != undefined){
|
if (wordToIndex[word] === undefined) {
|
||||||
wordList[wordList.length - 1][1].push(trainingText[j + 1]);
|
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,19 +43,24 @@ async function setup() {
|
|||||||
startWord = wordList[round(random(0, wordList.length - 1))][0];
|
startWord = wordList[round(random(0, wordList.length - 1))][0];
|
||||||
finalText = startWord;
|
finalText = startWord;
|
||||||
|
|
||||||
for (i = 0; i < 100; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
startWord = generateNextWord(startWord);
|
startWord = generateNextWord(startWord);
|
||||||
console.log(startWord)
|
console.log(startWord);
|
||||||
|
if (startWord === '.' || startWord === ',' || startWord === ';' || startWord === ':' || startWord === '!' || startWord === '?') {
|
||||||
|
finalText += startWord;
|
||||||
|
} else {
|
||||||
finalText += " " + startWord;
|
finalText += " " + startWord;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
textSize(19)
|
textSize(19);
|
||||||
console.log(finalText)
|
console.log(finalText);
|
||||||
text(finalText, 10, height / 2)
|
text(finalText, width / 2-400, height / 2, 800, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitText(text) {
|
function splitText(text) {
|
||||||
for (i = 0; i < text.length; i++) {
|
cleanedText = "";
|
||||||
|
for (let i = 0; i < text.length; i++) {
|
||||||
cleanedText += " " + text[i];
|
cleanedText += " " + text[i];
|
||||||
}
|
}
|
||||||
cleanedText = cleanedText
|
cleanedText = cleanedText
|
||||||
@@ -57,29 +72,27 @@ function splitText(text) {
|
|||||||
.replaceAll(":", " : ")
|
.replaceAll(":", " : ")
|
||||||
.replaceAll('"', ' " ')
|
.replaceAll('"', ' " ')
|
||||||
.replaceAll("'", " ' ")
|
.replaceAll("'", " ' ")
|
||||||
.replaceAll(" "," ")
|
.replaceAll("`", " ` ")
|
||||||
.replaceAll(" "," ")
|
.replaceAll("´", " ´ ")
|
||||||
|
.replaceAll(" ", " ")
|
||||||
|
.replaceAll(" ", " ")
|
||||||
.split(" ");
|
.split(" ");
|
||||||
return cleanedText;
|
return cleanedText;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateNextWord(currentWord) {
|
function generateNextWord(currentWord) {
|
||||||
let currentWordIndex = getWordIndex(currentWord);
|
let currentWordIndex = getWordIndex(currentWord);
|
||||||
let nextWordIndex = round(
|
// If no possible next words, return "."
|
||||||
random(
|
if (wordList[currentWordIndex][1].length === 0) {
|
||||||
0, wordList[currentWordIndex][1].length - 1
|
|
||||||
)
|
|
||||||
);
|
|
||||||
//retun . if end of text
|
|
||||||
if (wordList[currentWordIndex][1][nextWordIndex] == undefined){
|
|
||||||
return '.';
|
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) {
|
function getWordIndex(word) {
|
||||||
return wordListPlain.indexOf(word);
|
return wordToIndex[word];
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
|
|||||||
Reference in New Issue
Block a user