Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var brain;
- var totalGuesses = 0;
- var correctGuesses = 0;
- var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
- var textsize = 24;
- // Commented out because they get defined in the word lists
- //var words_EN;
- //var words_DE;
- function setup() {
- brain = new NeuralNetwork(8, 8, 2);
- createCanvas(400, 400);
- }
- function draw() {
- background(155);
- guessRandom();
- for (let i = 0; i < 2000; i++) {
- train();
- }
- fill(0);
- textSize(textsize);
- text("Guesses: " + totalGuesses, 40, 100)
- text("Right Guesses: " + correctGuesses, 40, 100 + textsize);
- text("Ratio: " + Math.floor(100 * correctGuesses / totalGuesses) + "%", 40, 100 + 2 * textsize);
- text("Total Train Guesses: " + totalGuesses * 4000, 40, 100 + 3 * textsize);
- }
- function resetGuessCount() {
- totalGuesses = 0;
- correctGuesses = 0;
- }
- function guess(word) {
- let wordarray = numerizeWord(word).split('').map(Number);
- let prediction = brain.predict(wordarray);
- if (prediction[0] > prediction[1]) console.log("English");
- else console.log("German");
- }
- function guessRandom() {
- totalGuesses++;
- if (Math.random() > 0.5) {
- word_EN = words_EN[Math.floor(Math.random() * words_EN.length)];
- //let wordarray_EN = numerizeWord(word_EN).split('').map(Number);
- let wordarray_EN = numerizeWord(word_EN);
- let prediction_EN = brain.predict(wordarray_EN);
- if (prediction_EN[0] > prediction_EN[1]) {
- fill(0, 255, 0);
- correctGuesses++;
- } else {
- fill(255, 0, 0);
- }
- } else {
- word_DE = words_DE[Math.floor(Math.random() * words_DE.length)];
- //let wordarray_DE = numerizeWord(word_DE).split('').map(Number);
- let wordarray_DE = numerizeWord(word_DE);
- let prediction_DE = brain.predict(wordarray_DE);
- if (prediction_DE[0] > prediction_DE[1]) {
- fill(255, 0, 0);
- } else {
- fill(0, 255, 0);
- correctGuesses++;
- }
- }
- //rect(0, height - 40, width-1, 39);
- }
- function train() {
- word_EN = words_EN[Math.floor(Math.random() * words_EN.length)];
- //let wordarray_EN = numerizeWord(word_EN).split('').map(Number);
- let wordarray_EN = numerizeWord(word_EN);
- brain.train(wordarray_EN, [1, 0]);
- word_DE = words_DE[Math.floor(Math.random() * words_DE.length)];
- //let wordarray_DE = numerizeWord(word_DE).split('').map(Number);
- let wordarray_DE = numerizeWord(word_DE);
- brain.train(wordarray_DE, [0, 1]);
- }
- function numerizeWordVOW(word) {
- word = word.replace(/a/g, 1);
- word = word.replace(/e/g, 1);
- word = word.replace(/i/g, 1);
- word = word.replace(/o/g, 1);
- word = word.replace(/u/g, 1);
- word = word.replace(/[^0-9]/g, '0');
- return word;
- }
- function numerizeWord(word) {
- let array = [];
- for (let i = 0; i < word.length; i++) {
- let char = word.charAt(i);
- let num = (alphabet.indexOf(char) + 1) / 26;
- array.push(num);
- //word = word.replace(word.charAt(i), num);
- }
- return array;
- }
Add Comment
Please, Sign In to add comment