-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scorer.js
75 lines (57 loc) · 1.79 KB
/
Scorer.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
const elasticlunr = require('./elasticlunr.loader');
module.exports = class Scorer {
constructor(classification = [], options = {}) {
this.options = Object.assign({
activity: 1e10
}, options);
this.classification = [];
(classification || []).forEach(([keyword, weight]) => this.add(keyword, weight));
}
add(keyword, weight) {
weight = +weight;
this.classification.push([keyword, weight ? weight : 0]);
}
score(text) {
const result = {
positive: 0,
negative: 0,
};
const index = elasticlunr(function () {
this.addField('text');
this.setRef('id');
this.saveDocument(false);
});
index.addDoc({text, id: '1'});
this.classification.forEach(([keyword, weight]) => {
const results = index.search(('' + keyword), {fields: {text: {boost: 1}}});
if (results.length) {
const {score} = results[0];
const value = score * weight;
if (weight >= 0) {
result.positive += value;
} else {
result.negative -= value;
}
}
});
return this.calculate(result);
}
calculate({positive, negative}) {
const result = {
positive: positive,
negative: negative,
weight: 0,
expertise: 0,
positivity: 0,
negativity: 0,
activity: 0
};
result.weight = result.positive - result.negative;
result.expertise = result.positive + result.negative;
const default_activity = this.options.activity;
result.positivity = result.negative ? result.positive / result.negative : default_activity * result.positive;
result.negativity = result.positive ? result.negative / result.positive : default_activity * result.negative;
result.activity = result.positivity - result.negativity;
return result;
}
};