Namespaces the existing model files in prep for move, to assist Git

This commit is contained in:
Joshua A. Horton 2019-02-28 11:52:03 +07:00
parent 9b1ad25537
commit b39dcd5588
3 changed files with 93 additions and 86 deletions

View file

@ -22,33 +22,35 @@
/// <reference path="./index.ts" />
/**
* @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();
}
};
}

View file

@ -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;
}
}

View file

@ -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;
}
};
}());
};
}());
}