-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsentiment.js
70 lines (66 loc) · 1.85 KB
/
testsentiment.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
let data = require('./data.json')
let results = []
let test =
'Khăn thương nhớ ai,'+
'Khăn rơi xuống đất.'+
'Khăn thương nhớ ai,'+
'Khăn vắt lên vai.'+
'Khăn thương nhớ ai,'+
'Khăn chùi nước mắt.'+
'Đèn thương nhớ ai,'+
'Mà đèn không tắt.'+
'Mắt thương nhớ ai,'+
'Mắt ngủ không yên.'+
'Đêm qua em những lo phiền,'+
'Lo vì một nỗi không yên một bề...'
test = test.replace(/[.*+?!${},]/g, " ")
console.log(test)
let split_test = test.split(" ")
let countelement = {}
function count(arraydata) {
array_elements = arraydata
array_elements.sort();
let current = null;
let cnt = 0;
for (let i = 0; i < array_elements.length; i++) {
if (array_elements[i] != current) {
if (cnt > 0) {
countelement[current] = cnt
}
current = array_elements[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt > 0) {
countelement[current] = cnt
}
}
for (let i in data) {
if(i.split(" ").length > 1){
if (test.toLocaleLowerCase().indexOf(i.toLocaleLowerCase()) != -1) {
results.push(data[i])
}
}
else{
for(let word in split_test){
if(i.toLocaleLowerCase() === split_test[word].toLocaleLowerCase()){
results.push(data[i])
}
}
}
}
if (results != [] && results.length != 0) {
count(results)
console.log(results)
let sortcount = Object.values(countelement).sort(function (a, b) { return a - b })
let maxInNumbers = Math.max.apply(Math, sortcount);
for (let i in countelement) {
if (countelement[i] == maxInNumbers) {
console.log(i + " : " + String(Math.round((maxInNumbers / results.length) * 100)) + "%")
}
}
}else{
console.log("Không xác định")
}