-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNgramSuccessorModel.js
47 lines (38 loc) · 1.08 KB
/
NgramSuccessorModel.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
const Event = require('./Event.js');
class NgramSuccessorModel {
constructor() {
this.successorMap = new Map();
this.occurence = 0;
}
getProbability(event) {
checkType(event);
let successor = this.successorMap.get(event.key)
if (successor === undefined) {
return 0;
}
return successor.occurence / this.occurence;
}
learn(event) {
checkType(event);
let successor = this.successorMap.get(event.key);
if (successor == undefined) {
successor = {
event : event,
occurence : 1
}
} else {
successor.occurence++;
}
this.occurence++;
this.successorMap.set(event.key, successor);
}
}
function checkType(event) {
if (event == null || event == undefined) {
throw 'cannot learn null or undefined';
}
if (!(event instanceof Event)) {
throw 'cannot learn event not Event';
}
}
module.exports = NgramSuccessorModel;