-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsimple-jieba-server.js
More file actions
39 lines (33 loc) · 867 Bytes
/
simple-jieba-server.js
File metadata and controls
39 lines (33 loc) · 867 Bytes
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
const { RPCConnection } = require('./stdio-jrpc')
const jieba = require('nodejieba')
// Entry
const main = () => {
let conn = RPCConnection.open()
conn.handle('hello', (_params) => {
jieba.load()
conn.sendResult(true)
})
conn.handle('split', (params) => {
// Using tag method will get the best result,
// even better than cut and cutHMM.
let result = jieba.tag(params).map((it, _idx, _arr) => it.word)
conn.sendResult(result)
})
conn.handle('loadDict', (params) => {
let success = 0
let failed = 0
for (dict of params) {
try {
jieba.load({
userDict: dict
})
success += 1
} catch (e) {
console.error(e)
failed += 1
}
}
conn.sendResult(`[JIEBA] Try to load ${params.length} Dict(s), ${success} successed, ${failed} failed.`)
})
}
main()