-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
61 lines (52 loc) · 1.23 KB
/
index.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
'use strict';
const net = require('net');
class NER {
/**
* @param {Object}
* @property {String} host Host of NER server
* @property {Number} port Port of NER server
*/
constructor(opts) {
this.opts = opts;
}
/**
* @param {String} text to be processed
* @param {Function} callback
* @return {Object}
* @property {Array} entities List of entitiy object
*/
get(text, callback) {
this.socket = new net.Socket();
this.socket.connect(this.opts.port, this.opts.host, () => {
this.socket.setNoDelay(true);
this.socket.write(text.replace(/\r?\n|\r|\t/g, ' ') + '\n');
});
this.socket.on('data', data => {
const re = /<([A-Za-z-]+?)>(.+?)<\/\1>/g;
const str = data.toString();
const res = {};
res.raw = str;
let m;
const _entities = {};
const _parsed = [];
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
_parsed.push(m);
if (!_entities[m[1]]) {
_entities[m[1]] = [];
}
_entities[m[1]].push(m[2]);
}
res._parsed = _parsed;
res.entities = _entities;
this.socket.destroy();
callback(undefined, res);
});
this.socket.on('error', err => {
callback(err, undefined);
});
}
}
module.exports = NER;