-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (65 loc) · 2.48 KB
/
Copy pathapp.js
File metadata and controls
83 lines (65 loc) · 2.48 KB
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
import nodejieba from 'nodejieba';
import path from 'path';
import express from 'express';
import fs from 'fs';
const app = express();
nodejieba.load({
userDict: path.join(path.dirname(''), '/dict.txt')
});
app.set('view engine', 'ejs');
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb', extended: true}));
app.use('/assets', express.static(path.join(path.dirname(''), ('/assets'))));
app.get('/', (req, res) => res.render('index'));
app.post("/jieba", (req, res) => {
let templateName = 'jieba';
let moodDict = [...fs.readFileSync('mood.txt','utf-8').matchAll(/([^\t]+?)\t(.+)/gm)].map(x => {
return {
word: x[1].replace(/\r|\n/g,''),
score: x[2].replace(/\t|\s/g, '')
}
});
let moodWords = moodDict.map(x => x.word);
let requestDictMode = parseInt(req.body.dictMode);
let requestText = nodejieba.tag(req.body.text);
let data = {};
if(requestDictMode <= 4){
let preData = requestText;
let partOfSpeech = requestDictMode == 2 ? 'n' : 'v';
if(requestDictMode >= 2 && requestDictMode <= 3)
preData = preData.filter(x => x.tag.charAt(0) === partOfSpeech);
else if (requestDictMode == 4)
preData = preData.filter(x => moodWords.indexOf(x.word) !== -1);
preData.forEach(item => {
let moodData = requestDictMode == 4 ? moodDict[moodWords.indexOf(item.word)].score : item.tag;
if(Object.keys(data).indexOf(moodData) === -1)
data[moodData] = [];
if(Object.values(data[moodData]).indexOf(item.word) === -1)
data[moodData].push(item.word);
})
} else {
let datasets = {
positive: [],
neutral: [],
negative: [],
}
templateName = requestDictMode == 5 ? 'draw' : 'wordcloud';
requestText.forEach(x => {
if (moodWords.indexOf(x.word) !== -1) {
let index = moodWords.indexOf(x.word);
if (moodDict[index].score == 0) {
datasets.neutral.push(x.word);
} else if (moodDict[index].score < 0) {
datasets.negative.push(x.word);
} else {
datasets.positive.push(x.word);
}
}
})
data = JSON.stringify(datasets);
}
res.render(templateName, { data });
});
app.listen(8080, () => {
console.log('Server Info: Listening Port 8080');
});