cpmct32

sketch.js

Apr 28th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. var brain;
  2.  
  3. var totalGuesses = 0;
  4. var correctGuesses = 0;
  5.  
  6. 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"];
  7.  
  8. var textsize = 24;
  9. // Commented out because they get defined in the word lists
  10. //var words_EN;
  11. //var words_DE;
  12.  
  13. function setup() {
  14. brain = new NeuralNetwork(8, 8, 2);
  15. createCanvas(400, 400);
  16. }
  17.  
  18. function draw() {
  19. background(155);
  20. guessRandom();
  21. for (let i = 0; i < 2000; i++) {
  22. train();
  23. }
  24. fill(0);
  25. textSize(textsize);
  26. text("Guesses: " + totalGuesses, 40, 100)
  27. text("Right Guesses: " + correctGuesses, 40, 100 + textsize);
  28. text("Ratio: " + Math.floor(100 * correctGuesses / totalGuesses) + "%", 40, 100 + 2 * textsize);
  29. text("Total Train Guesses: " + totalGuesses * 4000, 40, 100 + 3 * textsize);
  30. }
  31.  
  32. function resetGuessCount() {
  33. totalGuesses = 0;
  34. correctGuesses = 0;
  35. }
  36.  
  37. function guess(word) {
  38. let wordarray = numerizeWord(word).split('').map(Number);
  39. let prediction = brain.predict(wordarray);
  40. if (prediction[0] > prediction[1]) console.log("English");
  41. else console.log("German");
  42. }
  43.  
  44. function guessRandom() {
  45. totalGuesses++;
  46. if (Math.random() > 0.5) {
  47.  
  48. word_EN = words_EN[Math.floor(Math.random() * words_EN.length)];
  49. //let wordarray_EN = numerizeWord(word_EN).split('').map(Number);
  50. let wordarray_EN = numerizeWord(word_EN);
  51. let prediction_EN = brain.predict(wordarray_EN);
  52. if (prediction_EN[0] > prediction_EN[1]) {
  53. fill(0, 255, 0);
  54. correctGuesses++;
  55. } else {
  56. fill(255, 0, 0);
  57. }
  58.  
  59. } else {
  60.  
  61. word_DE = words_DE[Math.floor(Math.random() * words_DE.length)];
  62. //let wordarray_DE = numerizeWord(word_DE).split('').map(Number);
  63. let wordarray_DE = numerizeWord(word_DE);
  64. let prediction_DE = brain.predict(wordarray_DE);
  65. if (prediction_DE[0] > prediction_DE[1]) {
  66. fill(255, 0, 0);
  67. } else {
  68. fill(0, 255, 0);
  69. correctGuesses++;
  70. }
  71. }
  72. //rect(0, height - 40, width-1, 39);
  73. }
  74.  
  75. function train() {
  76. word_EN = words_EN[Math.floor(Math.random() * words_EN.length)];
  77. //let wordarray_EN = numerizeWord(word_EN).split('').map(Number);
  78. let wordarray_EN = numerizeWord(word_EN);
  79. brain.train(wordarray_EN, [1, 0]);
  80.  
  81. word_DE = words_DE[Math.floor(Math.random() * words_DE.length)];
  82. //let wordarray_DE = numerizeWord(word_DE).split('').map(Number);
  83. let wordarray_DE = numerizeWord(word_DE);
  84. brain.train(wordarray_DE, [0, 1]);
  85. }
  86.  
  87. function numerizeWordVOW(word) {
  88. word = word.replace(/a/g, 1);
  89. word = word.replace(/e/g, 1);
  90. word = word.replace(/i/g, 1);
  91. word = word.replace(/o/g, 1);
  92. word = word.replace(/u/g, 1);
  93. word = word.replace(/[^0-9]/g, '0');
  94. return word;
  95. }
  96.  
  97. function numerizeWord(word) {
  98. let array = [];
  99. for (let i = 0; i < word.length; i++) {
  100. let char = word.charAt(i);
  101. let num = (alphabet.indexOf(char) + 1) / 26;
  102. array.push(num);
  103. //word = word.replace(word.charAt(i), num);
  104. }
  105. return array;
  106. }
Add Comment
Please, Sign In to add comment