Skip to content

Commit 0834619

Browse files
Merge pull request #196 from LegendOfGIT/master
added example of recommendation engine (favorite t-shirts)
2 parents 293905d + 1d74270 commit 0834619

18 files changed

+230
-0
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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) + ' &euro;';
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>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const color_black = 1;
2+
const color_blue = 2;
3+
const color_darkblue = 3;
4+
const color_gray = 4;
5+
const color_white = 5;
6+
const color_green = 6;
7+
const color_turqoise = 7;
8+
const color_skin = 8;
9+
const color_lightblue = 9;
10+
const color_lightgreen = 10;
11+
12+
const neckline_round = 1;
13+
const neckline_v = 2;
14+
15+
let itemsInStock = [
16+
{
17+
trainingInformation: { color: color_black, hasPrinting: 0, neckline: neckline_round, price: 19.99 },
18+
displayingInformation: { imageFile: 'cl_black_nl_circle_hp_false_prc_1999.jpg' }
19+
},
20+
{
21+
trainingInformation: { color: color_blue, hasPrinting: 0, neckline: neckline_round, price: 19.99 },
22+
displayingInformation: { imageFile: 'cl_blue_nl_circle_hp_false_prc_1999.jpg' }
23+
},
24+
{
25+
trainingInformation: { color: color_darkblue, hasPrinting: 1, neckline: neckline_round, price: 29.99 },
26+
displayingInformation: { imageFile: 'cl_darkblue_nl_circle_hp_true_prc_2999.jpg' }
27+
},
28+
{
29+
trainingInformation: { color: color_gray, hasPrinting: 0, neckline: neckline_v, price: 9.99 },
30+
displayingInformation: { imageFile: 'cl_gray_nl_v_hp_false_prc_999.jpg' }
31+
},
32+
{
33+
trainingInformation: { color: color_white, hasPrinting: 0, neckline: neckline_v, price: 9.99 },
34+
displayingInformation: { imageFile: 'cl_white_nl_v_hp_false_prc_999.jpg' }
35+
},
36+
{
37+
trainingInformation: { color: color_green, hasPrinting: 0, neckline: neckline_round, price: 17.99 },
38+
displayingInformation: { imageFile: 'cl_green_nl_circle_hp_false_prc_1799.jpg' }
39+
},
40+
{
41+
trainingInformation: { color: color_blue, hasPrinting: 0, neckline: neckline_round, price: 17.99 },
42+
displayingInformation: { imageFile: 'cl_blue_nl_circle_hp_false_prc_1799.jpg' }
43+
},
44+
{
45+
trainingInformation: { color: color_turqoise, hasPrinting: 1, neckline: neckline_round, price: 15.99 },
46+
displayingInformation: { imageFile: 'cl_turqoise_nl_circle_hp_true_prc_1599.jpg' }
47+
},
48+
{
49+
trainingInformation: { color: color_skin, hasPrinting: 1, neckline: neckline_round, price: 15.99 },
50+
displayingInformation: { imageFile: 'cl_skin_nl_circle_hp_true_prc_1599.jpg' }
51+
},
52+
{
53+
trainingInformation: { color: color_darkblue, hasPrinting: 1, neckline: neckline_round, price: 15.99 },
54+
displayingInformation: { imageFile: 'cl_darkblue_nl_circle_hp_true_prc_1599.jpg' }
55+
},
56+
{
57+
trainingInformation: { color: color_turqoise, hasPrinting: 1, neckline: neckline_round, price: 15.99 },
58+
displayingInformation: { imageFile: 'cl_turqoise_nl_circle_hp_true_prc_1599_1.jpg' }
59+
},
60+
{
61+
trainingInformation: { color: color_darkblue, hasPrinting: 1, neckline: neckline_round, price: 15.99 },
62+
displayingInformation: { imageFile: 'cl_darkblue_nl_circle_hp_true_prc_1599_1.jpg' }
63+
},
64+
{
65+
trainingInformation: { color: color_lightblue, hasPrinting: 0, neckline: neckline_round, price: 19.99 },
66+
displayingInformation: { imageFile: 'cl_lightblue_nl_circle_hp_false_prc_1999.jpg' }
67+
},
68+
{
69+
trainingInformation: { color: color_lightgreen, hasPrinting: 0, neckline: neckline_round, price: 19.99 },
70+
displayingInformation: { imageFile: 'cl_lightgreen_nl_circle_hp_false_prc_1999.jpg' }
71+
},
72+
{
73+
trainingInformation: { color: color_skin, hasPrinting: 0, neckline: neckline_round, price: 19.99 },
74+
displayingInformation: { imageFile: 'cl_skin_nl_circle_hp_false_prc_1999.jpg' }
75+
},
76+
{
77+
trainingInformation: { color: color_gray, hasPrinting: 0, neckline: neckline_round, price: 19.99 },
78+
displayingInformation: { imageFile: 'cl_gray_nl_circle_hp_false_prc_1999.jpg' }
79+
},
80+
];

0 commit comments

Comments
 (0)