-
Notifications
You must be signed in to change notification settings - Fork 1
/
trigram.js
56 lines (48 loc) · 1.66 KB
/
trigram.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
(function () {
TrigramIndex = function (inputPhrases) {
function asTrigrams( phrase, callback ) {
var rawData = " ".concat( phrase , " " );
for( var i = rawData.length - 3; i >= 0; i = i - 1 )
callback.call( this, rawData.slice( i, i + 3 ) );
};
var instance = {
phrases : [],
trigramIndex : [],
index : function ( phrase ) {
if( !phrase || phrase === "" || this.phrases.indexOf( phrase ) >= 0 ) return;
var phraseIndex = this.phrases.push( phrase ) - 1;
asTrigrams.call( this, phrase, function( trigram ) {
var phrasesForTrigram = this.trigramIndex[trigram];
if( !phrasesForTrigram ) phrasesForTrigram = [];
if( phrasesForTrigram.indexOf( phraseIndex ) < 0 ) phrasesForTrigram.push( phraseIndex );
this.trigramIndex[trigram] = phrasesForTrigram;
});
},
find : function( phrase ) {
var phraseMatches = [];
var trigramsInPhrase = 0;
asTrigrams.call( this, phrase, function( trigram ) {
var phrasesForTrigram = this.trigramIndex[trigram];
trigramsInPhrase += 1;
if( phrasesForTrigram )
for( var j in phrasesForTrigram ) {
phraseIndex = phrasesForTrigram[j];
if( !phraseMatches[phraseIndex] ) phraseMatches[phraseIndex] = 0;
phraseMatches[phraseIndex] += 1;
}
});
var result = [];
for( var i in phraseMatches )
result.push( { phrase: this.phrases[i], matches: phraseMatches[i] } );
result.sort( function ( a, b ) {
var diff = b.matches - a.matches;
return diff;// == 0 ? a.phrase.localeCompare(b.phrase) : diff;
});
return result;
}
};
for( var i in inputPhrases )
instance.index( input[i] );
return instance;
};
})();