|
| 1 | + |
| 2 | +<!doctype html> |
| 3 | +<html lang="en"> |
| 4 | + <head> |
| 5 | + <title>brain.js tutorial</title> |
| 6 | + <style> |
| 7 | + .articleImage { |
| 8 | + width:180px; |
| 9 | + } |
| 10 | + |
| 11 | + .ratingItem { |
| 12 | + padding-bottom: 30px; |
| 13 | + } |
| 14 | + |
| 15 | + .suggestedItems { |
| 16 | + display: flex; |
| 17 | + flex-wrap: wrap; |
| 18 | + width:650px; |
| 19 | + } |
| 20 | + |
| 21 | + .suggestionItem { |
| 22 | + padding-right:15px; |
| 23 | + padding-top: 30px; |
| 24 | + } |
| 25 | + </style> |
| 26 | + </head> |
| 27 | + <body onLoad="nextTry();"> |
| 28 | + <script src=" https://unpkg.com/[email protected]/browser.min.js" ></script> |
| 29 | + <script src="itemsInStock.js"></script> |
| 30 | + <script> |
| 31 | + const color_normalization_factor = 100; |
| 32 | + const neckline_normalization_factor = 10; |
| 33 | + const price_normalization_factor = 100; |
| 34 | + |
| 35 | + let trainingData = [ |
| 36 | + { input: {color: color_black, hasPrinting: false, neckline: neckline_round, price: 0.1999 }, output: { wanted: 0 } } |
| 37 | + ]; |
| 38 | + let ratingItem; |
| 39 | + |
| 40 | + function nextTry(rating){ |
| 41 | + document.getElementById('result').innerHTML = ''; |
| 42 | + |
| 43 | + if(undefined !== ratingItem){ |
| 44 | + trainingData.push({ input: ratingItem.trainingInformation, output: { wanted: rating / 4 } }); |
| 45 | + } |
| 46 | + |
| 47 | + const network = new brain.NeuralNetwork({ |
| 48 | + activation: 'sigmoid', |
| 49 | + hiddenLayers: [4] |
| 50 | + }); |
| 51 | + network.train(trainingData); |
| 52 | + |
| 53 | + let suggestionItemsText = ''; |
| 54 | + let suggestionItems = []; |
| 55 | + for(i=0;i<itemsInStock.length;i++){ |
| 56 | + let item = getNormalizedItemFromStock(i); |
| 57 | + item.wanted = network.run(item.trainingInformation).wanted; |
| 58 | + suggestionItems.push(item); |
| 59 | + } |
| 60 | + |
| 61 | + suggestionItems.sort(function(a,b){ |
| 62 | + return b.wanted - a.wanted; |
| 63 | + }); |
| 64 | + |
| 65 | + for(i=0;i<suggestionItems.length;i++){ |
| 66 | + suggestionItem = suggestionItems[i]; |
| 67 | + suggestionItemsText += |
| 68 | + '<div class="suggestionItem">' + |
| 69 | + getFormattedItem(suggestionItem) + ' <br/> ' + |
| 70 | + 'wanted: ' + Math.round(suggestionItem.wanted * 100) + '%' |
| 71 | + + '</div>'; |
| 72 | + } |
| 73 | + |
| 74 | + ratingItem = getNormalizedItemFromStock(Math.floor((Math.random() * itemsInStock.length - 1) + 1)); |
| 75 | + document.getElementById('result').innerHTML += |
| 76 | + '<div class="ratingItem">' + |
| 77 | + 'Rate this:<br/>' + |
| 78 | + getFormattedItem(ratingItem) + '<br/>' + |
| 79 | + '</div>' + |
| 80 | + 'Suggested items:<br/>' + |
| 81 | + '<div class="suggestedItems">' + |
| 82 | + suggestionItemsText + |
| 83 | + '</div>'; |
| 84 | + } |
| 85 | + |
| 86 | + function getNormalizedItemFromStock(index){ |
| 87 | + const item = itemsInStock[index]; |
| 88 | + trainingInformation = item.trainingInformation; |
| 89 | + return { |
| 90 | + trainingInformation : { |
| 91 | + color: trainingInformation.color / color_normalization_factor, |
| 92 | + hasPrinting: trainingInformation.hasPrinting, |
| 93 | + neckline: trainingInformation.neckline / neckline_normalization_factor, |
| 94 | + price: trainingInformation.price / price_normalization_factor |
| 95 | + }, |
| 96 | + displayingInformation : item.displayingInformation |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + function getFormattedItem(item){ |
| 101 | + const trainingInformation = item.trainingInformation; |
| 102 | + const formattedItem = |
| 103 | + '<img class="articleImage" src="images/articles/' + item.displayingInformation.imageFile + '"/><br/>' + |
| 104 | + 'color: ' + getColorName(trainingInformation.color) + '<br/>' + |
| 105 | + 'has printing?: ' + (1 === trainingInformation.hasPrinting ? 'yes' : 'no') + '<br/>' + |
| 106 | + 'neckline: ' + getNecklineName(trainingInformation.neckline) + '<br/>' + |
| 107 | + 'price: ' + ((Math.round((trainingInformation.price * price_normalization_factor) * 100)) / 100) + ' €'; |
| 108 | + return formattedItem; |
| 109 | + } |
| 110 | + |
| 111 | + function getColorName(color){ |
| 112 | + var id = color * color_normalization_factor; |
| 113 | + var name = |
| 114 | + color_black === id ? 'black' : |
| 115 | + color_blue === id ? 'blue' : |
| 116 | + color_darkblue === id ? 'dark blue' : |
| 117 | + color_gray === id ? 'gray' : |
| 118 | + color_green === id ? 'green' : |
| 119 | + color_lightblue === id ? 'light blue' : |
| 120 | + color_lightgreen === id ? 'light green' : |
| 121 | + color_skin === id ? 'skin' : |
| 122 | + color_turqoise === id ? 'turqoise' : |
| 123 | + color_white === id ? 'white' : |
| 124 | + ''; |
| 125 | + return name; |
| 126 | + } |
| 127 | + |
| 128 | + function getNecklineName(color){ |
| 129 | + var id = color * neckline_normalization_factor; |
| 130 | + var name = |
| 131 | + neckline_round === id ? 'round' : |
| 132 | + neckline_v === id ? 'v' : |
| 133 | + ''; |
| 134 | + return name; |
| 135 | + } |
| 136 | + </script> |
| 137 | + |
| 138 | + <center> |
| 139 | + <br/><br/><br/><br/><br/><br/><br/> |
| 140 | + <input type="button" value="1" onClick="nextTry(0);"/> |
| 141 | + <input type="button" value="2" onClick="nextTry(1);"/> |
| 142 | + <input type="button" value="3" onClick="nextTry(2);"/> |
| 143 | + <input type="button" value="4" onClick="nextTry(3);"/> |
| 144 | + <input type="button" value="5" onClick="nextTry(4);"/> |
| 145 | + <br/><br/> |
| 146 | + <div id="result"> |
| 147 | + </div> |
| 148 | + </center> |
| 149 | + </body> |
| 150 | +</html> |
0 commit comments