mirror of
https://github.com/keymanapp/keyman.git
synced 2026-09-10 01:27:43 +00:00
Suggest prefix via exhaustive search over word list.
This commit is contained in:
parent
198b6a1e3e
commit
4abbbc41a5
2 changed files with 41 additions and 8 deletions
|
|
@ -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');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue