-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sequence.js
47 lines (43 loc) · 1.49 KB
/
Sequence.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 Ngram = require('./Ngram.js');
const Event = require('./Event.js');
class Sequence {
constructor(eventList, name) {
this.name = name;
if (eventList === null || eventList === undefined) {
this.eventList = [];
this.length = 0;
} else {
if (! Array.isArray(eventList)) {
throw 'Cannot create sequence with Array.isArray(eventList) === false';
}
eventList.forEach(event => {
if (!(event instanceof Event)) {
throw 'Cannot create sequence with array containing a not Event'
}
})
this.eventList = eventList;
this.length = eventList.length;
}
}
append(event) {
if (event == null || event == undefined) {
throw 'cannot append null or undefined';
}
if (!(event instanceof Event)) {
throw 'cannot append event not Event';
}
this.eventList.push(event);
this.length++;
}
getNgram(beforeIndex, ngramSize) {
let ngrameventList = [];
if (beforeIndex > 0) {
let from = Math.max(0,beforeIndex - ngramSize);
for (let previousIndex = from; previousIndex < beforeIndex; previousIndex++) {
ngrameventList.push(this.eventList[previousIndex]);
}
}
return new Ngram(ngrameventList);
}
}
module.exports = Sequence;