diff --git a/common/predictive-text/worker/dummy-model.ts b/common/predictive-text/worker/dummy-model.ts index b6f89b582f..4ea134ab59 100644 --- a/common/predictive-text/worker/dummy-model.ts +++ b/common/predictive-text/worker/dummy-model.ts @@ -22,33 +22,35 @@ /// -/** - * @file dummy-model.ts - * - * Defines the Dummy model, which is used for testing the - * prediction API exclusively. - */ +namespace models { + /** + * @file dummy-model.ts + * + * Defines the Dummy model, which is used for testing the + * prediction API exclusively. + */ -/** - * The Dummy Model that returns nonsensical, but predictable results. - */ -LMLayerWorker.models.DummyModel = class DummyModel implements WorkerInternalModel { - configuration: Configuration; - private _futureSuggestions: Suggestion[][]; + /** + * The Dummy Model that returns nonsensical, but predictable results. + */ + LMLayerWorker.models.DummyModel = class DummyModel implements WorkerInternalModel { + configuration: Configuration; + private _futureSuggestions: Suggestion[][]; - constructor(capabilities: Capabilities, options?: any) { - options = options || {}; - this.configuration = options.configuration || {}; - // Create a shallow copy of the suggestions; - // this class mutates the array. - this._futureSuggestions = options.futureSuggestions - ? options.futureSuggestions.slice() : []; - } - - predict(transform: Transform, context: Context, injectedSuggestions?: Suggestion[]): Suggestion[] { - if (injectedSuggestions) { - return injectedSuggestions; + constructor(capabilities: Capabilities, options?: any) { + options = options || {}; + this.configuration = options.configuration || {}; + // Create a shallow copy of the suggestions; + // this class mutates the array. + this._futureSuggestions = options.futureSuggestions + ? options.futureSuggestions.slice() : []; } - return this._futureSuggestions.shift(); - } -}; + + predict(transform: Transform, context: Context, injectedSuggestions?: Suggestion[]): Suggestion[] { + if (injectedSuggestions) { + return injectedSuggestions; + } + return this._futureSuggestions.shift(); + } + }; +} \ No newline at end of file diff --git a/common/predictive-text/worker/index.ts b/common/predictive-text/worker/index.ts index fc4d00b830..f97d8c5c29 100644 --- a/common/predictive-text/worker/index.ts +++ b/common/predictive-text/worker/index.ts @@ -236,6 +236,9 @@ class LMLayerWorker { let worker = new LMLayerWorker({ postMessage: scope.postMessage }); scope.onmessage = worker.onMessage.bind(worker); + // Ensures that the worker instance is accessible for loaded model scripts. + scope['LMLayerWorker'] = worker; + return worker; } } diff --git a/common/predictive-text/worker/wordlist-model.ts b/common/predictive-text/worker/wordlist-model.ts index 518a304e58..f3b35bbd84 100644 --- a/common/predictive-text/worker/wordlist-model.ts +++ b/common/predictive-text/worker/wordlist-model.ts @@ -28,71 +28,73 @@ * Defines a simple word list (unigram) model. */ -/** - * @class WordListModel - * - * Defines the word list model, or the unigram model. - * Unigram models throw away all preceding words, and search - * for the next word exclusively. As such, they can perform simple - * prefix searches within words, however they are not very good - * at predicting the next word. - */ -LMLayerWorker.models.WordListModel = (function () { - /** Upper bound on the amount of suggestions to generate. */ - const MAX_SUGGESTIONS = 3; + namespace models { + /** + * @class WordListModel + * + * Defines the word list model, or the unigram model. + * Unigram models throw away all preceding words, and search + * for the next word exclusively. As such, they can perform simple + * prefix searches within words, however they are not very good + * at predicting the next word. + */ + LMLayerWorker.models.WordListModel = (function () { + /** Upper bound on the amount of suggestions to generate. */ + const MAX_SUGGESTIONS = 3; - return class WordListModel implements WorkerInternalModel { - private _wordlist: string[]; + return class WordListModel implements WorkerInternalModel { + private _wordlist: string[]; - constructor(_capabilities: Capabilities, wordlist: string[]) { - this._wordlist = wordlist; - } - - predict(transform: Transform, context: Context): Suggestion[] { - // EVERYTHING to the left of the cursor: - let fullLeftContext = context.left || ''; - // Stuff to the left of the cursor in the current word. - let leftContext = fullLeftContext.split(/\s+/).pop() || ''; - // All text to the left of the cursor INCLUDING anything that has - // just been typed. - let prefix = leftContext + (transform.insert || ''); - let suggestions: Suggestion[] = []; - - // Special-case the empty buffer/transform: return the top suggestions. - if (!transform.insert && context.startOfBuffer && context.endOfBuffer) { - return this._wordlist.slice(0, MAX_SUGGESTIONS).map(word => ({ - transform: { - insert: word + ' ', - deleteLeft: 0 - }, - displayAs: word - })); + constructor(_capabilities: Capabilities, wordlist: string[]) { + this._wordlist = wordlist; } - // Naïve O(n) exhaustive search through the entire word - // list, up to the suggestion limit. - for (let word of this._wordlist) { - let suggestionPrefix = word.substr(0, prefix.length); - if (prefix !== suggestionPrefix) { - continue; + predict(transform: Transform, context: Context): Suggestion[] { + // EVERYTHING to the left of the cursor: + let fullLeftContext = context.left || ''; + // Stuff to the left of the cursor in the current word. + let leftContext = fullLeftContext.split(/\s+/).pop() || ''; + // All text to the left of the cursor INCLUDING anything that has + // just been typed. + let prefix = leftContext + (transform.insert || ''); + let suggestions: Suggestion[] = []; + + // Special-case the empty buffer/transform: return the top suggestions. + if (!transform.insert && context.startOfBuffer && context.endOfBuffer) { + return this._wordlist.slice(0, MAX_SUGGESTIONS).map(word => ({ + transform: { + insert: word + ' ', + deleteLeft: 0 + }, + displayAs: word + })); } - suggestions.push({ - transform: { - // The left part of the word has already been entered. - insert: word.substr(leftContext.length) + ' ', - deleteLeft: 0, - }, - displayAs: word, - }); + // Naïve O(n) exhaustive search through the entire word + // list, up to the suggestion limit. + for (let word of this._wordlist) { + let suggestionPrefix = word.substr(0, prefix.length); + if (prefix !== suggestionPrefix) { + continue; + } - // Do not exceed the limit on suggestions. - if (suggestions.length >= MAX_SUGGESTIONS) { - break; + suggestions.push({ + transform: { + // The left part of the word has already been entered. + insert: word.substr(leftContext.length) + ' ', + deleteLeft: 0, + }, + displayAs: word, + }); + + // Do not exceed the limit on suggestions. + if (suggestions.length >= MAX_SUGGESTIONS) { + break; + } } + + return suggestions; } - - return suggestions; - } - }; -}()); + }; + }()); +} \ No newline at end of file