-
Notifications
You must be signed in to change notification settings - Fork 1
/
crf_test_min2.html
157 lines (142 loc) · 5.22 KB
/
crf_test_min2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>crf_test_min</title>
<style>
body {
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
}
.circle {
display: inline-block;
border-radius: 150%;
width: 40px;
height: 40px;
border: 0px;
text-align: center;
line-height: 15px;
font-size: 80%;
margin: 5px;
padding: 3px;
}
.nutrient_value {
font-size: 120%;
}
.carbs_circle {
background-color: #00CC66;
color: white;
}
.cals_circle {
background-color: #00CCFF;
color: white;
}
.other_circle {
background-color: white;
border: 1px solid black;
color: black;
}
</style>
</head>
<body>
<div class="circle carbs_circle">
<span class="nutrient_value" id="carbohydrate_content"></span><br><span class="nutrient_unit">Carbs</span>
</div>
<br/>
<textarea id="ingredients_box" rows="20" cols="40">10g (1/202) butter
50g (202) onion, very finely chopped
450g (11b) beef (flank, chump or shin would be perfect), freshly minced
1/2 teaspoon fresh thyme leaves
1/2 teaspoon finely chopped parsley
1 small organic egg, beaten
salt and freshly ground pepper
pork caul fat (optional)
oil or dripping</textarea>
<div id="breakdown"></div>
<script type='text/javascript'>
const textArea = document.getElementById('ingredients_box');
textArea.addEventListener('input', (e) => textChanged(e));
function textChanged(e) {
updateNutrients();
}
function updateNutrients() {
const text = document.getElementById('ingredients_box').value;
const lines = text.split(/\n/);
const foods = parseIngredients(lines);
console.log(foods);
const carbs = calculateCarbs(foods);
console.log(carbs);
document.getElementById('carbohydrate_content').innerHTML = Math.round(carbs) + "g";
let breakdown = '';
for (let i = 0; i < foods.length; i++) {
if ('food' in foods[i] && foods[i]['food'] != null) {
// TODO: use function from measures.js
let carbs = 0;
if (foods[i]['food']['carbohydrate_content'] != 0) {
carbs = foods[i]['food']['carbohydrate_content'] * foods[i]['weight'] / 100.0;
}
breakdown += Math.round(carbs) + 'g ' + foods[i]['food']['name'] + '<br/>';
}
}
document.getElementById('breakdown').innerHTML = breakdown;
}
function parseIngredients(lines) {
return import_data(crfTest(export_data(lines)));
}
function crfTest(input) {
FS.writeFile('input.txt', input);
args = ['-v', '1', '-m', 'model_file', '-o', 'output.txt', 'input.txt'];
Module['callMain'](args);
return FS.readFile('output.txt', { encoding: 'utf8' }).split(/\n/);
}
function whenReady() {
// can start the program here
updateNutrients();
}
var Module = {
arguments: ["-h"], // run help when it first loads (this is ignored)
preRun: [],
postRun: [whenReady],
print: (function() {
var element = document.getElementById('output');
if (element) element.value = ''; // clear browser cache
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
// These replacements are necessary if you render to raw HTML
//text = text.replace(/&/g, "&");
//text = text.replace(/</g, "<");
//text = text.replace(/>/g, ">");
//text = text.replace('\n', '<br>', 'g');
console.log(text);
if (element) {
element.value += text + "\n";
element.scrollTop = element.scrollHeight; // focus on bottom
}
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
},
setStatus: function(text) {
console.log("status:" + text);
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
window.onerror = function(event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
Module.setStatus('Exception thrown, see JavaScript console');
Module.setStatus = function(text) {
if (text) Module.printErr('[post-exception status] ' + text);
};
};
</script>
<script async type="text/javascript" src="ingreedy.js"></script>
<script async type="text/javascript" src="crf_test.js"></script>
</body>
</html>