-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
94 lines (86 loc) · 2.35 KB
/
demo.js
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
function build(boundary, text) {
var dashdash = '--';
var crlf = '\r\n';
var res = '';
res += dashdash;
res += boundary;
res += crlf;
res += 'Content-Disposition: form-data; name="text"; filename="dummy"';
res += crlf;
res += 'Content-Type: text/plain';
res += crlf;
res += crlf;
res += text;
res += crlf;
res += dashdash;
res += boundary;
res += dashdash;
res += crlf;
return res;
}
// http://stackoverflow.com/a/11850486
function Interpolate(start, end, steps, count) {
var s = start,
e = end,
final = s + (((e - s) / steps) * count);
return Math.floor(final);
}
function Color(_r, _g, _b) {
var r, g, b;
var setColors = function(_r, _g, _b) {
r = _r;
g = _g;
b = _b;
};
setColors(_r, _g, _b);
this.getColors = function() {
var colors = {
r: r,
g: g,
b: b
};
return colors;
};
}
function color(value) {
value = parseFloat(value);
var start = new Color(255, 255, 255);
var end = new Color(6, 170, 60);
if (value < 0) {
end = new Color(232, 9, 26);
value = -1.0 * value;
}
var startColors = start.getColors();
var endColors = end.getColors();
var r = Interpolate(startColors.r, endColors.r, 100, value * 50);
var g = Interpolate(startColors.g, endColors.g, 100, value * 50);
var b = Interpolate(startColors.b, endColors.b, 100, value * 50);
return "rgb(" + r + "," + g + "," + b + ")";
}
function load() {
$('#result').html("<img src='./loading.gif' />");
}
function displayResult(data) {
console.log(data);
var res = "";
for (var i = 0; i < data.values.length; i++) {
res += "<span style='background-color: "+color(data.values[i])+"'>"+data.chars[i]+"</span>";
}
$('#result').html(res);
}
$(document).ready(function() {
$("#form").on("submit", function() {
load();
var text = JSON.stringify({"text": $("#input").val(), "method": "text"});
var boundary = '------multipartformboundary' + (new Date).getTime();
var xhr = new XMLHttpRequest();
xhr.open("POST", predict_url, true);
xhr.setRequestHeader('content-type', 'multipart/form-data; boundary='+boundary);
xhr.setRequestHeader('Authorization', predict_authkey);
xhr.send(build(boundary, text));
xhr.onload = function() {
displayResult(JSON.parse(xhr.response));
}
return false;
});
});