diff --git a/common/predictive-text/unit_tests/headless/worker-predict-wordlist.js b/common/predictive-text/unit_tests/headless/worker-predict-wordlist.js index e50ef74246..82edefb0f5 100644 --- a/common/predictive-text/unit_tests/headless/worker-predict-wordlist.js +++ b/common/predictive-text/unit_tests/headless/worker-predict-wordlist.js @@ -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'); } }); }); diff --git a/common/predictive-text/worker/wordlist-model.ts b/common/predictive-text/worker/wordlist-model.ts index 288bf18804..4c745284a0 100644 --- a/common/predictive-text/worker/wordlist-model.ts +++ b/common/predictive-text/worker/wordlist-model.ts @@ -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; } }