-
Notifications
You must be signed in to change notification settings - Fork 0
/
gram-cracker.js
78 lines (59 loc) · 1.65 KB
/
gram-cracker.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
76
77
78
var q = require("q");
var _ = require("underscore");
var document = require("./models/document");
var GramCracker = function(docs) {
this.docs = [];
this.addDocuments(docs);
};
GramCracker.prototype.addDocument = function(doc) {
var that = this;
var def = q.defer();
if(doc) {
if(doc instanceof document) {
that.docs.push(doc);
}
else {
that.docs.push(new document(doc));
}
}
def.resolve(that.docs);;
return def.promise;
};
GramCracker.prototype.addDocuments = function(docs) {
var that = this;
var def = q.defer();
if(docs) {
_.chain([docs]).flatten().each(function(doc) {
that.addDocument(doc);
});
}
def.resolve(that.docs);
return def.promise;
};
GramCracker.prototype.clearDocuments = function() {
var that = this;
var def = q.defer();
that.docs = [];
def.resolve(that.docs);
return def.promise;
};
GramCracker.prototype.getDocuments = function() {
var that = this;
var def = q.defer();
def.resolve(that.docs);
return def.promise;
};
// extracts n-grams
// max: max number/level of n-grams to acquire
// stopWords: array of words to ignore when extracting n-grams
GramCracker.prototype.extract = function(max, stopWords) {
var that = this;
var def = q.defer();
var ngrams = [];
_.each(that.docs, function(doc) {
ngrams.push(doc.ngram(max, stopWords));
});
def.resolve(_.flatten(ngrams));
return def.promise;
};
module.exports = GramCracker;