mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-08 01:45:32 +00:00
change(common/models): adds optional 'testMode' param to LMLayer config
This commit is contained in:
parent
30747f5600
commit
43bf49d1cd
7 changed files with 30 additions and 10 deletions
|
|
@ -65,7 +65,7 @@ namespace com.keyman.text.prediction {
|
|||
* @param uri URI of the underlying LMLayer worker code. This will usually be a blob:
|
||||
* or file: URI. If uri is not provided, this will start the default Worker.
|
||||
*/
|
||||
constructor(capabilities: Capabilities, worker?: Worker) {
|
||||
constructor(capabilities: Capabilities, worker?: Worker, testMode?: boolean) {
|
||||
// Either use the given worker, or instantiate the default worker.
|
||||
this._worker = worker || DefaultWorker.constructInstance();
|
||||
this._worker.onmessage = this.onMessage.bind(this)
|
||||
|
|
@ -76,7 +76,7 @@ namespace com.keyman.text.prediction {
|
|||
this._revertPromises = new PromiseStore();
|
||||
this._nextToken = Number.MIN_SAFE_INTEGER;
|
||||
|
||||
this.sendConfig(capabilities);
|
||||
this.sendConfig(capabilities, !!testMode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -85,10 +85,11 @@ namespace com.keyman.text.prediction {
|
|||
* @param capabilities The host platform's capability spec - a model cannot assume access to more context
|
||||
* than specified by this parameter.
|
||||
*/
|
||||
private sendConfig(capabilities: Capabilities) {
|
||||
private sendConfig(capabilities: Capabilities, testMode: boolean) {
|
||||
this._worker.postMessage({
|
||||
message: 'config',
|
||||
capabilities: capabilities
|
||||
capabilities: capabilities,
|
||||
testMode: testMode
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ describe('LMLayer', function () {
|
|||
|
||||
describe('[[constructor]]', function () {
|
||||
it('should construct with a single argument', function () {
|
||||
let lmLayer = new LMLayer(helpers.defaultCapabilities);
|
||||
let lmLayer = new LMLayer(helpers.defaultCapabilities, null, true);
|
||||
assert.instanceOf(lmLayer, LMLayer);
|
||||
lmLayer.shutdown();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@ describe('LMLayer using dummy model', function () {
|
|||
it('will predict future suggestions', function () {
|
||||
this.timeout(testconfig.timeouts.standard * 3); // This one makes multiple subsequent calls across
|
||||
// the WebWorker boundary, so we should be generous here.
|
||||
var lmLayer = new LMLayer(helpers.defaultCapabilities);
|
||||
|
||||
var lmLayer = new LMLayer(helpers.defaultCapabilities, null, true);
|
||||
|
||||
var stripIDs = function(suggestions) {
|
||||
suggestions.forEach(function(suggestion) {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,11 @@ describe('LMLayer using the trie model', function () {
|
|||
it('will predict an empty buffer', function () {
|
||||
this.timeout(testconfig.timeouts.standard * 3); // This one makes multiple subsequent calls across
|
||||
// the WebWorker boundary, so we should be generous here.
|
||||
var lmLayer = new LMLayer(helpers.defaultCapabilities);
|
||||
|
||||
// Parameter 3 = true: enables 'test mode', disables correction-search timeout.
|
||||
// This helps prevent the correction-search timeout from flaking out periodically during unit tests in
|
||||
// CI, since remote servers / devices are involved.
|
||||
var lmLayer = new LMLayer(helpers.defaultCapabilities, null, true);
|
||||
|
||||
// We're testing many as asynchronous messages in a row.
|
||||
// this would be cleaner using async/await syntax, but
|
||||
|
|
|
|||
|
|
@ -84,6 +84,8 @@ class LMLayerWorker {
|
|||
|
||||
private _platformCapabilities: Capabilities;
|
||||
|
||||
private _testMode: boolean = false;
|
||||
|
||||
private _hostURL: string;
|
||||
|
||||
private _currentModelSource: ModelSourceSpec;
|
||||
|
|
@ -259,6 +261,7 @@ class LMLayerWorker {
|
|||
}
|
||||
|
||||
this._platformCapabilities = payload.capabilities;
|
||||
this._testMode = !!payload.testMode;
|
||||
|
||||
this.transitionToLoadingState();
|
||||
}
|
||||
|
|
@ -308,7 +311,7 @@ class LMLayerWorker {
|
|||
* @param model The loaded language model.
|
||||
*/
|
||||
private transitionToReadyState(model: LexicalModel): ModelCompositor {
|
||||
let compositor = new ModelCompositor(model);
|
||||
let compositor = new ModelCompositor(model, this._testMode);
|
||||
this.state = {
|
||||
name: 'ready',
|
||||
handleMessage: (payload) => {
|
||||
|
|
|
|||
|
|
@ -25,12 +25,15 @@ class ModelCompositor {
|
|||
|
||||
private SUGGESTION_ID_SEED = 0;
|
||||
|
||||
constructor(lexicalModel: LexicalModel) {
|
||||
private testMode: boolean = false
|
||||
|
||||
constructor(lexicalModel: LexicalModel, testMode?: boolean) {
|
||||
this.lexicalModel = lexicalModel;
|
||||
if(lexicalModel.traverseFromRoot) {
|
||||
this.contextTracker = new correction.ContextTracker();
|
||||
}
|
||||
this.punctuation = ModelCompositor.determinePunctuationFromModel(lexicalModel);
|
||||
this.testMode = !!testMode;
|
||||
}
|
||||
|
||||
private predictFromCorrections(corrections: ProbabilityMass<Transform>[], context: Context): Distribution<Suggestion> {
|
||||
|
|
@ -233,7 +236,8 @@ class ModelCompositor {
|
|||
// Whitespace is probably fine, actually. Less sure about backspace.
|
||||
|
||||
let bestCorrectionCost: number;
|
||||
for(let matches of searchSpace.getBestMatches()) {
|
||||
const SEARCH_TIMEOUT = this.testMode ? 0 : correction.SearchSpace.DEFAULT_ALLOTTED_CORRECTION_TIME_INTERVAL;
|
||||
for(let matches of searchSpace.getBestMatches(SEARCH_TIMEOUT)) {
|
||||
// Corrections obtained: now to predict from them!
|
||||
let predictionRoots = matches.map(function(match) {
|
||||
let correction = match.matchString;
|
||||
|
|
|
|||
|
|
@ -52,6 +52,13 @@ interface ConfigMessage {
|
|||
* The platform's supported capabilities.
|
||||
*/
|
||||
capabilities: Capabilities;
|
||||
|
||||
/**
|
||||
* An optional flag to enable 'test mode'.
|
||||
*
|
||||
* At present, this just disables the correction algorithm's timeout when set to `true`.
|
||||
*/
|
||||
testMode?: boolean
|
||||
}
|
||||
|
||||
interface ModelFile {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue