opencode-quill 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +96 -97
- package/package.json +5 -3
- package/src/ghost.ts +9 -3
- package/src/predictor.ts +131 -22
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,12 @@ All notable changes are documented in this file using
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and
|
|
5
5
|
[Semantic Versioning](https://semver.org/).
|
|
6
6
|
|
|
7
|
+
## [0.2.0] - 2026-10-13
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- **prediction:** Add safe typo correction and context repair
|
|
12
|
+
|
|
7
13
|
## [0.1.5] - 2026-10-10
|
|
8
14
|
|
|
9
15
|
### Fixed
|
|
@@ -50,6 +56,7 @@ All notable changes are documented in this file using
|
|
|
50
56
|
- `/quill` command and command palette action to toggle autocomplete.
|
|
51
57
|
- Local installer script, full test suite, CI workflows, and release tooling.
|
|
52
58
|
|
|
59
|
+
[0.2.0]: https://github.com/ozandogrultan/opencode-quill/compare/v0.1.5...v0.2.0
|
|
53
60
|
[0.1.5]: https://github.com/ozandogrultan/opencode-quill/compare/v0.1.4...v0.1.5
|
|
54
61
|
[0.1.4]: https://github.com/ozandogrultan/opencode-quill/compare/v0.1.3...v0.1.4
|
|
55
62
|
[0.1.3]: https://github.com/ozandogrultan/opencode-quill/compare/v0.1.2...v0.1.3
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# opencode-quill
|
|
2
2
|
|
|
3
|
-
[](https://github.com/ozandogrultan/opencode-quill/actions/workflows/ci.yml)
|
|
3
|
+
[](https://github.com/ozandogrultan/opencode-quill/actions/workflows/ci.yml) [](https://www.npmjs.com/package/opencode-quill)
|
|
4
4
|
|
|
5
5
|
Local word autocomplete and next-word prediction for the **OpenCode V2** prompt
|
|
6
6
|
composer — inspired by [oh-my-pi (OMP)](https://github.com/can1357/oh-my-pi)'s
|
|
@@ -10,38 +10,63 @@ As you type, Quill predicts the remainder of your current prose word or the next
|
|
|
10
10
|
word in sequence, shown as dimmed inline ghost text directly at your cursor.
|
|
11
11
|
Press <kbd>Tab</kbd> or <kbd>→</kbd> to accept it, or <kbd>Esc</kbd> to dismiss.
|
|
12
12
|
|
|
13
|
-
```text
|
|
14
|
-
> can you refactor the test
|
|
15
|
-
s ← dimmed ghost text, Tab or → to accept
|
|
16
|
-
```
|
|
17
|
-
|
|
18
13
|
## What it does
|
|
19
14
|
|
|
20
15
|
- **Zero AI, completely local:** Runs entirely on-device without sending prompt
|
|
21
|
-
text to any model, API, or external service.
|
|
22
|
-
- **Context-aware ranking:** Blends a
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
-
|
|
26
|
-
|
|
27
|
-
(`please run ` → `tests`) when prior evidence supports the sequence.
|
|
16
|
+
text to any model, API, or external service.
|
|
17
|
+
- **Context-aware ranking:** Blends a common-English vocabulary prior with an
|
|
18
|
+
online n-gram model (bigrams and trigrams) learned from your prompt history.
|
|
19
|
+
- **Completes words, fixes typos, and predicts next words:** Completes the active
|
|
20
|
+
prose prefix, offers single-edit corrections, and predicts following words
|
|
21
|
+
when learned evidence supports the sequence.
|
|
28
22
|
- **Ghost-text styling:** Matches [opencode-ghost](https://github.com/ozandogrultan/opencode-ghost)'s
|
|
29
|
-
dimmed appearance (
|
|
30
|
-
|
|
31
|
-
- **Safe acceptance:** Inserts
|
|
32
|
-
Supports native undo (<kbd>Ctrl+-</kbd> or <kbd>Cmd+Z</kbd>). Never
|
|
33
|
-
|
|
23
|
+
dimmed appearance (the theme's muted text color), rendered in the OpenTUI
|
|
24
|
+
buffer without displacing the cursor.
|
|
25
|
+
- **Safe acceptance:** Inserts or corrects the active word in the draft without
|
|
26
|
+
submitting. Supports native undo (<kbd>Ctrl+-</kbd> or <kbd>Cmd+Z</kbd>). Never
|
|
27
|
+
overwrites unrelated draft text and never touches the system clipboard.
|
|
34
28
|
- **Coexists with Ghost:** [Ghost](https://github.com/ozandogrultan/opencode-ghost)
|
|
35
29
|
suggests entire next prompts in an *empty* composer using a model; Quill
|
|
36
30
|
predicts words in an *active* draft locally. When both are installed, each
|
|
37
31
|
activates only in its respective domain without key conflicts.
|
|
38
32
|
- **Prose-aware gating:** Automatically suppresses suggestions in code blocks,
|
|
39
33
|
backticks, file paths, URLs, slash commands, shell executions, active selections,
|
|
40
|
-
and modal dialogs.
|
|
34
|
+
composers holding attachments or pasted-text chips, and modal dialogs.
|
|
35
|
+
- **End-of-draft only:** Suggestions appear when the cursor is at the end of the
|
|
36
|
+
draft, either on a partial word or after a space.
|
|
37
|
+
|
|
38
|
+
## What suggestions look like
|
|
39
|
+
|
|
40
|
+
The suggestion appears dimmed at the end of the draft. It is not part of the
|
|
41
|
+
editable text until you press <kbd>Tab</kbd> or <kbd>→</kbd>.
|
|
42
|
+
|
|
43
|
+
| Kind | Your draft | Dimmed suggestion | Draft after acceptance |
|
|
44
|
+
| --- | --- | --- | --- |
|
|
45
|
+
| Word completion | `please comp` | `lete` | `please complete` |
|
|
46
|
+
| Typo correction | `please cmplete` | ` → complete` | `please complete` |
|
|
47
|
+
| Next word | `please run ` | `tests` | `please run tests` |
|
|
48
|
+
|
|
49
|
+
A correction looks like this, with only ` → complete` dimmed:
|
|
50
|
+
|
|
51
|
+
```text
|
|
52
|
+
> please cmplete → complete
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
- **Completions append** the missing letters or next word.
|
|
56
|
+
- **Corrections replace only the active word**, preserving its initial-capital
|
|
57
|
+
or all-uppercase casing (`Cmplete` → `Complete`, `CMPLETE` → `COMPLETE`).
|
|
58
|
+
- **Normal completions take precedence.** For example, `whate` completes to
|
|
59
|
+
`whatever` rather than being corrected to `what`. Typo correction is a
|
|
60
|
+
fallback when no confident prefix completion is available; recognized words
|
|
61
|
+
are not corrected.
|
|
62
|
+
- **Earlier words are never rewritten.** With learned evidence for
|
|
63
|
+
`please run tests`, typing `please rnu ` can suggest `tests`. Accepting it
|
|
64
|
+
produces `please rnu tests`, leaving `rnu` unchanged.
|
|
41
65
|
|
|
42
66
|
## Requirements
|
|
43
67
|
|
|
44
|
-
- [OpenCode](https://opencode.ai)
|
|
68
|
+
- [OpenCode](https://opencode.ai) with the V2 TUI plugin runtime (the supported
|
|
69
|
+
range is `engines.opencode` in `package.json`).
|
|
45
70
|
- Node.js (for the installer script).
|
|
46
71
|
|
|
47
72
|
## Install
|
|
@@ -54,7 +79,7 @@ Add the package to `plugins` in `~/.config/opencode/cli.json`:
|
|
|
54
79
|
{
|
|
55
80
|
"$schema": "https://opencode.ai/v2/cli.json",
|
|
56
81
|
"plugins": [
|
|
57
|
-
"opencode-quill
|
|
82
|
+
"opencode-quill"
|
|
58
83
|
]
|
|
59
84
|
}
|
|
60
85
|
```
|
|
@@ -141,84 +166,58 @@ Custom keybindings can also be overridden using OpenCode's standard
|
|
|
141
166
|
- **Command Palette (`Ctrl+P`)** — Includes **Word autocomplete: on/off** and
|
|
142
167
|
**Clear word autocomplete history**.
|
|
143
168
|
|
|
144
|
-
##
|
|
145
|
-
|
|
146
|
-
###
|
|
147
|
-
|
|
148
|
-
Quill
|
|
149
|
-
|
|
150
|
-
- `session.execution.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
**
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
**Prefix-Constrained Posterior:**
|
|
191
|
-
When you type a prefix (e.g. `refac`), all candidates matching the prefix—including
|
|
192
|
-
the prefix itself if it is already a complete word—are ranked. A suggestion is only
|
|
193
|
-
displayed when its calibrated confidence exceeds a quality threshold (30% for prefix
|
|
194
|
-
completions, 55% for next-word predictions). If the typed prefix is already the most
|
|
195
|
-
probable word, no ghost text is shown.
|
|
196
|
-
|
|
197
|
-
**Bounded Capacity:**
|
|
198
|
-
To keep memory and storage footprint minimal, the personal model enforces strict caps:
|
|
199
|
-
- Words: Top 8,000 most frequent entries
|
|
200
|
-
- Bigrams: Top 30,000 transitions
|
|
201
|
-
- Trigrams: Top 30,000 transitions
|
|
202
|
-
- Message deduplication log: 8,000 IDs
|
|
203
|
-
|
|
204
|
-
### 3. Where State is Stored
|
|
205
|
-
|
|
206
|
-
Quill uses OpenCode V2's durable plugin storage API (`context.storage.store`), which
|
|
207
|
-
persists state as clean JSON files inside your standard XDG state directory:
|
|
208
|
-
|
|
209
|
-
| Setting / Data | File Path |
|
|
169
|
+
## How it works
|
|
170
|
+
|
|
171
|
+
### Learning
|
|
172
|
+
|
|
173
|
+
Quill learns from your own submitted prompts, never from assistant output:
|
|
174
|
+
|
|
175
|
+
- It listens to session events (`session.execution.started`,
|
|
176
|
+
`session.execution.succeeded`, `session.inbox.delivered`).
|
|
177
|
+
- On startup and when you open a session, it also backfills from the user
|
|
178
|
+
messages of the current and most recently updated sessions.
|
|
179
|
+
- Each message is ingested once, tracked by `<sessionID>:<messageID>`.
|
|
180
|
+
- Only prose is learned. Code blocks, inline code, URLs, file paths,
|
|
181
|
+
identifiers, slash commands, shell lines, indented blocks and very large
|
|
182
|
+
pastes are ignored.
|
|
183
|
+
- Punctuation, line breaks and code reset the context, so words separated by
|
|
184
|
+
syntax are never learned as adjacent.
|
|
185
|
+
- A word missing from the base vocabulary becomes suggestible only after it
|
|
186
|
+
has been seen more than once.
|
|
187
|
+
|
|
188
|
+
### Prediction
|
|
189
|
+
|
|
190
|
+
- **Baseline prior:** a bundled English word-frequency table (SUBTLEX) gives
|
|
191
|
+
useful completions from the first keystroke.
|
|
192
|
+
- **Personal model:** unigram, bigram and trigram counts from your prompts.
|
|
193
|
+
Context evidence falls back from trigram to bigram to unigram as it thins out.
|
|
194
|
+
- **Current draft:** words already in the draft get a small boost.
|
|
195
|
+
- **Confidence gating:** a suggestion is shown only when it clears a
|
|
196
|
+
confidence threshold, which is stricter for next-word predictions than for
|
|
197
|
+
prefix completions. If the typed prefix is already the most likely word,
|
|
198
|
+
nothing is shown.
|
|
199
|
+
- **Typo fallback:** when prefix completion is not confident enough, nearby
|
|
200
|
+
words are ranked with an edit penalty and a stricter confidence threshold.
|
|
201
|
+
Corrections require at least three typed letters and support single-edit
|
|
202
|
+
errors, including adjacent letter swaps; not every typo produces a suggestion.
|
|
203
|
+
Unknown words in the preceding context can also be matched to nearby words
|
|
204
|
+
for prediction.
|
|
205
|
+
- **Bounded size:** the personal model is pruned to its most frequent entries
|
|
206
|
+
when saved.
|
|
207
|
+
|
|
208
|
+
### Storage
|
|
209
|
+
|
|
210
|
+
Quill uses OpenCode's durable plugin storage, which writes JSON under the XDG
|
|
211
|
+
state directory (`$XDG_STATE_HOME` when set, otherwise `~/.local/state`):
|
|
212
|
+
|
|
213
|
+
| Data | File |
|
|
210
214
|
| --- | --- |
|
|
211
|
-
|
|
|
212
|
-
|
|
|
213
|
-
|
|
214
|
-
*(On systems with `$XDG_STATE_HOME` set, `$XDG_STATE_HOME/opencode/<channel>/tui/` is used).*
|
|
215
|
+
| Learned model | `opencode/<channel>/tui/plugin.quill.learning.json` |
|
|
216
|
+
| Settings | `opencode/<channel>/tui/plugin.quill.settings.json` |
|
|
215
217
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
- **Raw prompt text is never written to disk or stored in snapshots.**
|
|
220
|
-
- The storage files stay strictly on your local machine and are never tracked in Git
|
|
221
|
-
or included in npm packages.
|
|
218
|
+
The learned model holds only word, bigram and trigram counts plus processed
|
|
219
|
+
message IDs. Raw prompt text is never written to disk. These files stay on your
|
|
220
|
+
machine and are never tracked in Git or published to npm.
|
|
222
221
|
|
|
223
222
|
## Development
|
|
224
223
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-quill",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "OpenCode prompt autocomplete: local n-gram word and next-word prediction shown as inline ghost text in the TUI composer, accepted with Tab.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Ozan Dogrultan",
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"autocomplete",
|
|
21
21
|
"autosuggest",
|
|
22
22
|
"ngram",
|
|
23
|
-
"prompt"
|
|
23
|
+
"prompt",
|
|
24
|
+
"word-prediction",
|
|
25
|
+
"opencode-tui"
|
|
24
26
|
],
|
|
25
27
|
"exports": {
|
|
26
28
|
"./tui": "./src/tui.tsx"
|
package/src/ghost.ts
CHANGED
|
@@ -75,7 +75,7 @@ export class ComposerGhost {
|
|
|
75
75
|
const query = completionQuery(text, offset)
|
|
76
76
|
let prediction = query && this.predictor.complete(query)
|
|
77
77
|
const last = this.shown
|
|
78
|
-
if (query && !query.next && last && text.startsWith(last.text)) {
|
|
78
|
+
if (query && !query.next && last && !last.prediction.replace && text.startsWith(last.text)) {
|
|
79
79
|
const typed = text.slice(last.text.length)
|
|
80
80
|
if (typed && last.prediction.suffix.startsWith(typed) && typed.length < last.prediction.suffix.length) {
|
|
81
81
|
prediction = { ...last.prediction, suffix: last.prediction.suffix.slice(typed.length) }
|
|
@@ -99,9 +99,10 @@ export class ComposerGhost {
|
|
|
99
99
|
}
|
|
100
100
|
const x = this.editor.screenX + cursor.visualCol
|
|
101
101
|
const y = this.editor.screenY + cursor.visualRow
|
|
102
|
+
const ghost = prediction.replace ? ` → ${prediction.suffix}` : prediction.suffix
|
|
102
103
|
buffer.pushScissorRect(x, y, room, 1)
|
|
103
104
|
try {
|
|
104
|
-
buffer.drawText(
|
|
105
|
+
buffer.drawText(ghost, x, y, this.color(), this.editor.backgroundColor)
|
|
105
106
|
} finally {
|
|
106
107
|
buffer.popScissorRect()
|
|
107
108
|
}
|
|
@@ -114,7 +115,12 @@ export class ComposerGhost {
|
|
|
114
115
|
this.editor.plainText !== shown.text || this.offset() !== shown.offset) return false
|
|
115
116
|
if (!completionQuery(shown.text, shown.offset)) return false
|
|
116
117
|
this.shown = undefined
|
|
117
|
-
|
|
118
|
+
if (shown.prediction.replace) {
|
|
119
|
+
this.editor.replaceText(shown.text.slice(0, shown.text.length - shown.prediction.replace) + shown.prediction.suffix)
|
|
120
|
+
this.editor.gotoBufferEnd()
|
|
121
|
+
} else {
|
|
122
|
+
this.editor.insertText(shown.prediction.suffix)
|
|
123
|
+
}
|
|
118
124
|
this.editor.requestRender()
|
|
119
125
|
return true
|
|
120
126
|
}
|
package/src/predictor.ts
CHANGED
|
@@ -9,7 +9,7 @@ export type Snapshot = {
|
|
|
9
9
|
seen: string[]
|
|
10
10
|
}
|
|
11
11
|
export type Prompt = { id: string; text: string }
|
|
12
|
-
export type Prediction = { suffix: string; word: string; confidence: number }
|
|
12
|
+
export type Prediction = { suffix: string; word: string; confidence: number; replace?: number }
|
|
13
13
|
|
|
14
14
|
const DISCOUNT = 0.5
|
|
15
15
|
const PRIOR_LIMIT = 20_000
|
|
@@ -98,6 +98,58 @@ function lowerBound(words: readonly string[], prefix: string): number {
|
|
|
98
98
|
return low
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
function editKind(source: string, target: string, targetLength = target.length): "transpose" | "insert" | "delete" | "substitute" | undefined {
|
|
102
|
+
const sourceLength = source.length
|
|
103
|
+
if (Math.abs(sourceLength - targetLength) > 1) return
|
|
104
|
+
let i = 0
|
|
105
|
+
while (i < sourceLength && i < targetLength && source.charCodeAt(i) === target.charCodeAt(i)) i++
|
|
106
|
+
if (i === sourceLength && i === targetLength) return
|
|
107
|
+
if (sourceLength === targetLength) {
|
|
108
|
+
if (i + 1 < sourceLength && source.charCodeAt(i) === target.charCodeAt(i + 1) && source.charCodeAt(i + 1) === target.charCodeAt(i)) {
|
|
109
|
+
for (let j = i + 2; j < sourceLength; j++) if (source.charCodeAt(j) !== target.charCodeAt(j)) return
|
|
110
|
+
return "transpose"
|
|
111
|
+
}
|
|
112
|
+
for (let j = i + 1; j < sourceLength; j++) if (source.charCodeAt(j) !== target.charCodeAt(j)) return
|
|
113
|
+
return "substitute"
|
|
114
|
+
}
|
|
115
|
+
if (sourceLength < targetLength) {
|
|
116
|
+
for (let j = i; j < sourceLength; j++) if (source.charCodeAt(j) !== target.charCodeAt(j + 1)) return
|
|
117
|
+
return "insert"
|
|
118
|
+
}
|
|
119
|
+
for (let j = i; j < targetLength; j++) if (source.charCodeAt(j + 1) !== target.charCodeAt(j)) return
|
|
120
|
+
const doubled = (i > 0 && source.charCodeAt(i) === source.charCodeAt(i - 1)) || (i + 1 < sourceLength && source.charCodeAt(i) === source.charCodeAt(i + 1))
|
|
121
|
+
return doubled || sourceLength >= 5 ? "delete" : undefined
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function typoWeight(prefix: string, word: string): number {
|
|
125
|
+
const prefixLength = prefix.length
|
|
126
|
+
const wordLength = word.length
|
|
127
|
+
if (prefixLength < 3) return 0
|
|
128
|
+
const full = editKind(prefix, word)
|
|
129
|
+
if (full === "transpose") return 0.25
|
|
130
|
+
if (full === "insert" || full === "delete") return prefixLength === 3 && prefix.charCodeAt(0) !== word.charCodeAt(0) ? 0 : 0.06
|
|
131
|
+
if (full === "substitute") return prefixLength >= 4 && prefix.charCodeAt(0) === word.charCodeAt(0) ? 0.04 : 0
|
|
132
|
+
if (prefixLength < 4 || wordLength <= prefixLength) return 0
|
|
133
|
+
const head = prefix.charCodeAt(0) === word.charCodeAt(0) ||
|
|
134
|
+
(prefix.charCodeAt(0) === word.charCodeAt(1) && prefix.charCodeAt(1) === word.charCodeAt(0))
|
|
135
|
+
if (!head) return 0
|
|
136
|
+
const partial = editKind(prefix, word, prefixLength)
|
|
137
|
+
if (partial === "transpose") return 0.06
|
|
138
|
+
if (partial === "substitute") return 0.02
|
|
139
|
+
if (wordLength > prefixLength + 1 && editKind(prefix, word, prefixLength + 1) === "insert") return 0.03
|
|
140
|
+
if (prefixLength >= 5 && editKind(prefix, word, prefixLength - 1) === "delete") return 0.02
|
|
141
|
+
return 0
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function applyCase(source: string, word: string): string {
|
|
145
|
+
if (source.length > 1 && source === source.toUpperCase()) return word.toUpperCase()
|
|
146
|
+
const first = source[0]
|
|
147
|
+
if (first && first === first.toUpperCase() && first !== first.toLowerCase()) {
|
|
148
|
+
return word[0].toUpperCase() + word.slice(1)
|
|
149
|
+
}
|
|
150
|
+
return word === "i" ? "I" : word
|
|
151
|
+
}
|
|
152
|
+
|
|
101
153
|
export class WordPredictor {
|
|
102
154
|
readonly prior = new Map<string, number>()
|
|
103
155
|
private priorTotal = 0
|
|
@@ -106,6 +158,7 @@ export class WordPredictor {
|
|
|
106
158
|
private bigramTotals = new Map<string, { total: number; types: number }>()
|
|
107
159
|
private trigramTotals = new Map<string, { total: number; types: number }>()
|
|
108
160
|
private vocabulary: string[] = []
|
|
161
|
+
private vocabularyByLength = new Map<number, string[]>()
|
|
109
162
|
|
|
110
163
|
constructor(prior: readonly Frequency[]) {
|
|
111
164
|
for (const { word, count } of prior) {
|
|
@@ -124,6 +177,12 @@ export class WordPredictor {
|
|
|
124
177
|
this.bigramTotals = totals(this.personal.bigrams)
|
|
125
178
|
this.trigramTotals = totals(this.personal.trigrams)
|
|
126
179
|
this.vocabulary = [...new Set([...this.prior.keys(), ...[...this.personal.words].filter(([, count]) => count >= 2).map(([word]) => word)])].sort()
|
|
180
|
+
this.vocabularyByLength.clear()
|
|
181
|
+
for (const word of this.vocabulary) {
|
|
182
|
+
const words = this.vocabularyByLength.get(word.length) ?? []
|
|
183
|
+
words.push(word)
|
|
184
|
+
this.vocabularyByLength.set(word.length, words)
|
|
185
|
+
}
|
|
127
186
|
}
|
|
128
187
|
|
|
129
188
|
private score(word: string, context: readonly string[]): number {
|
|
@@ -146,37 +205,87 @@ export class WordPredictor {
|
|
|
146
205
|
return score
|
|
147
206
|
}
|
|
148
207
|
|
|
208
|
+
private resolveContext(raw: readonly string[], local: ReadonlyMap<string, number>): string[] {
|
|
209
|
+
const resolved: string[] = []
|
|
210
|
+
for (const word of raw) {
|
|
211
|
+
if (this.prior.has(word) || (this.personal.words.get(word) ?? 0) >= 2 || (local.get(word) ?? 0) >= 2 || word.length < 3) {
|
|
212
|
+
resolved.push(word)
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
let best = word
|
|
216
|
+
let bestScore = 0
|
|
217
|
+
for (let length = word.length - 1; length <= word.length + 1; length++) {
|
|
218
|
+
for (const candidate of this.vocabularyByLength.get(length) ?? []) {
|
|
219
|
+
const kind = editKind(word, candidate)
|
|
220
|
+
const weight = kind === "transpose" ? 0.25 : kind ? 0.05 : 0
|
|
221
|
+
if (!weight) continue
|
|
222
|
+
const score = this.score(candidate, resolved) * weight
|
|
223
|
+
if (score > bestScore) {
|
|
224
|
+
best = candidate
|
|
225
|
+
bestScore = score
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
resolved.push(best)
|
|
230
|
+
}
|
|
231
|
+
return resolved
|
|
232
|
+
}
|
|
233
|
+
|
|
149
234
|
complete(query: CompletionQuery): Prediction | undefined {
|
|
150
235
|
const prefix = normalizeWord(query.prefix)
|
|
151
|
-
const
|
|
236
|
+
const local = new Map<string, number>()
|
|
237
|
+
for (const word of proseWords(query.before)) increment(local, word.text)
|
|
238
|
+
const context = this.resolveContext(leftContext(query.before), local)
|
|
152
239
|
const evidence = this.bigramTotals.get(context.at(-1) ?? "")?.total ?? 0
|
|
153
240
|
if (query.next && evidence < 2) return
|
|
154
241
|
if (!query.next && [...prefix].length < 2 && evidence < 2) return
|
|
155
|
-
const
|
|
156
|
-
for (const word of proseWords(query.before)) increment(local, word.text)
|
|
157
|
-
const candidates = new Set<string>()
|
|
242
|
+
const candidates = new Map<string, number>()
|
|
158
243
|
for (let index = lowerBound(this.vocabulary, prefix); index < this.vocabulary.length; index++) {
|
|
159
244
|
const word = this.vocabulary[index]
|
|
160
245
|
if (!word.startsWith(prefix)) break
|
|
161
|
-
candidates.
|
|
246
|
+
candidates.set(word, 1)
|
|
247
|
+
}
|
|
248
|
+
for (const word of local.keys()) if (word.startsWith(prefix)) candidates.set(word, 1)
|
|
249
|
+
const suggest = (): Prediction | undefined => {
|
|
250
|
+
let best = ""
|
|
251
|
+
let bestScore = 0
|
|
252
|
+
let bestWeight = 1
|
|
253
|
+
let sum = 0
|
|
254
|
+
for (const [word, weight] of candidates) {
|
|
255
|
+
const base = this.score(word, context) + (query.next ? 0 : 0.003 * Math.min(local.get(word) ?? 0, 3))
|
|
256
|
+
const score = base * weight
|
|
257
|
+
sum += score
|
|
258
|
+
if (score > bestScore) {
|
|
259
|
+
best = word
|
|
260
|
+
bestScore = score
|
|
261
|
+
bestWeight = weight
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const confidence = query.next ? bestScore : bestScore / (sum || 1)
|
|
265
|
+
if (!best || best === prefix || confidence < (query.next ? 0.55 : bestWeight < 1 ? 0.45 : 0.3)) return
|
|
266
|
+
if (bestWeight < 1) {
|
|
267
|
+
return { suffix: applyCase(query.prefix, best), word: best, confidence, replace: query.prefix.length }
|
|
268
|
+
}
|
|
269
|
+
let suffix = best.slice(prefix.length)
|
|
270
|
+
if (query.prefix.length > 1 && query.prefix === query.prefix.toUpperCase()) suffix = suffix.toUpperCase()
|
|
271
|
+
if (query.next && best === "i") suffix = "I"
|
|
272
|
+
return { suffix, word: best, confidence }
|
|
162
273
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
best = word
|
|
172
|
-
bestScore = score
|
|
274
|
+
const completion = suggest()
|
|
275
|
+
if (completion || query.next || prefix.length < 3 || candidates.has(prefix)) return completion
|
|
276
|
+
for (const [length, words] of this.vocabularyByLength) {
|
|
277
|
+
if (length < prefix.length - 1) continue
|
|
278
|
+
for (const word of words) {
|
|
279
|
+
if (candidates.has(word)) continue
|
|
280
|
+
const weight = typoWeight(prefix, word)
|
|
281
|
+
if (weight > 0) candidates.set(word, weight)
|
|
173
282
|
}
|
|
174
283
|
}
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
return
|
|
284
|
+
for (const word of local.keys()) {
|
|
285
|
+
if (candidates.has(word)) continue
|
|
286
|
+
const weight = typoWeight(prefix, word)
|
|
287
|
+
if (weight > 0) candidates.set(word, weight)
|
|
288
|
+
}
|
|
289
|
+
return suggest()
|
|
181
290
|
}
|
|
182
291
|
}
|