Suggest prefix via exhaustive search over word list.

This commit is contained in:
Eddie Antonio Santos 2019-01-17 14:42:59 -07:00
parent 198b6a1e3e
commit 4abbbc41a5
No known key found for this signature in database
GPG key ID: DD411EA4D9509D87
2 changed files with 41 additions and 8 deletions

View file

@ -25,7 +25,6 @@ describe('LMLayerWorker word list model', function() {
jsonFixture('wordlists/english-1000')
);
var suggestion;
var suggestions = model.predict({
insert: 't',
deleteLeft: 0,
@ -33,12 +32,12 @@ describe('LMLayerWorker word list model', function() {
assert.isAtLeast(suggestions.length, MIN_SUGGESTIONS);
// Ensure all of the suggestions actually start with 't'
var suggestion;
var suggestedWord;
for (var i = 0; i < MIN_SUGGESTIONS; i++) {
suggestions = suggestions[i];
assert.strictEqual(
suggestion.transform.insert.substr(0, 1),
't',
);
suggestion = suggestions[i];
suggestedWord = suggestion.transform.insert;
assert.strictEqual(suggestedWord.substr(0, 1), 't');
}
});
});

View file

@ -36,7 +36,41 @@
* at predicting the next word.
*/
LMLayerWorker.models.WordListModel = class WordListModel implements WorkerInternalModel {
predict(_transform: Transform, _context: Context): Suggestion[] {
return [];
private _wordlist: string[];
constructor(_capabilities: Capabilities, wordlist: string[]) {
this._wordlist = wordlist;
}
predict(transform: Transform, _context: Context): Suggestion[] {
const MAX_SUGGESTIONS = 3;
let prefix = transform.insert;
let suggestions: Suggestion[] = [];
// TODO: support astral code points (1 code point === length of 2)
if (prefix.length !== 1) {
throw new Error('Invalid prefix length')
}
// naïve O(n) exhaustive search through the entire word list.
for (let word of this._wordlist) {
let suggestionPrefix = word.substr(0, 1)
if (prefix === suggestionPrefix) {
suggestions.push({
transform: {
insert: word + ' ',
deleteLeft: 0,
},
displayAs: word,
});
}
// Do not exceed the limit on suggestions.
if (suggestions.length >= MAX_SUGGESTIONS) {
break;
}
}
return suggestions;
}
}