-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatagram_test.js
141 lines (112 loc) · 4.4 KB
/
datagram_test.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
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
var datagram = require("./datagram").datagram;
var fs = require('fs');
// Setup basic express server
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io')(server),
port = 8082,
clients = [],
client_id,
queue_count = 0;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
// Routing
app.use(express.static(__dirname + '/public'));
var datagramDebug = function(data, addon){
if(addon !== null){
clients[addon.addon].debug_count += 1;
if(clients[addon.addon].debug_count < 100){
var sock = clients[addon.addon].socket;
sock.emit('console_output', { message: data });
}
}
console.log(data);
};
var datagramProcessed = function (error, result) {
datagramDebug('RESULTS:', null);
if (result.options.DEFAULT_MODE === 'classify') {
datagramDebug(JSON.stringify(result.classify.rating.basic, null, 2), result.options);
//datagramDebug(result.classify.guess);
datagramDebug('==> Your data probably belongs to: "' + result.classify.finalGuess + '" dataset!', result.options);
} else if (result.options.DEFAULT_MODE === 'profilize') {
datagramDebug(result.options.hash, null);
datagramDebug(result.profilize, null);
}
if (error) {
datagramDebug(error, null);
}
};
var datagramDone = function () {
queue_count -= 1;
return false;
};
var generateUUID = function() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x7|0x8)).toString(16);
});
return uuid;
};
var avaliable_datasets = ['English', 'German', 'French', 'Polish', 'Italian', 'Croatian'];
var options = {
DEFAULT_DATASETS: avaliable_datasets, //.,
DEFAULT_DATASETS_DIR: './datagram/LM/',
DEFAULT_DATASETS_EXT: '.lm',
DEFAULT_INPUT: 'string', // string, file
DEFAULT_MODE: 'classify', // classify, profilize
debug: false,
callback: function (error, result) {
return new datagramProcessed(error, result);
},
onDrain: function () {
return new datagramDone();
},
onDebug: function (data, addon) {
return new datagramDebug(data, addon);
}
};
var tc = new datagram(options);
// To make Dataset Profiles
//tc.queue(../samples/profilize/index.json);
//tc.queue(../datagram/samples/classify/cs.txt);
//tc.queue(../datagram/samples/classify/da.txt);
//tc.queue(../datagram/samples/classify/de.txt);
//tc.queue(../datagram/samples/classify/en.txt);
//tc.queue(../datagram/samples/classify/es.txt);
//tc.queue(../datagram/samples/classify/hu.txt);
//tc.queue(../datagram/samples/classify/it.txt);
//tc.queue(../datagram/samples/classify/sl.txt);
io.sockets.on('connection', function (socket) {
// Register for private session
socket.on("register", function(data) {
// Create new Client
client_id = generateUUID();
clients[client_id] = {};
clients[client_id].socket = socket;
clients[client_id].requests_count = 0;
clients[client_id].socket.emit('welcome', { message: 'Welcome to dataGram input analyzer! Please load some demo text and try to process it!', available_datasets: avaliable_datasets, guid: client_id});
clients[client_id].socket.emit('system', { message: 'You registered on system: ' + client_id});
socket.broadcast.emit('system', { message: 'Another user registered on system: ' + client_id});
});
socket.on('process', function (data) {
var actions = {};
actions.message = 'CLEAR';
clients[data.client_id].requests_count += 1;
clients[data.client_id].debug_count = 0;
queue_count += 1;
clients[data.client_id].socket.emit('system', { message: 'Your request "' + clients[data.client_id].requests_count + '" is ' + queue_count + ' in queue!'});
clients[data.client_id].socket.emit('actions', { message: actions });
// Add data to queue
tc.queue(data.value, data.client_id);
});
// When socket disconnects, remove it from the list:
//socket.on('disconnect', function() {
// console.log("DISCONNECTED");
// var index = clients.indexOf(socket);
// delete clients[index];
//});
});