Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] *Multi*dict #1

Merged
merged 6 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions data/constants/keycodes/keycodes_0.0.4_quantum.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"keycodes": {
"0x7C7B": {
"group": "quantum",
"key": "QK_AUTOCORRECT_BANK_TOGGLE",
"key": "QK_AUTOCORRECT_DICT_CYCLE",
"aliases": [
"AC_BANK"
"AC_DICT"
]
}
}
Expand Down
47 changes: 26 additions & 21 deletions docs/feature_autocorrect.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ This file will look like this:
// ouput -> output
// widht -> width

#define AUTOCORRECT_MIN_LENGTH 5 // "ouput"
#define AUTOCORRECT_MAX_LENGTH 6 // ":thier"
#define N_DICTS 1

static const uint16_t autocorrect_offsets[N_DICTS] PROGMEM = {0};
static const uint16_t autocorrect_min_lengths[N_DICTS] PROGMEM = {5};
static const uint16_t autocorrect_max_lengths[N_DICTS] PROGMEM = {6};
static const uint16_t autocorrect_sizes[N_DICTS] PROGMEM = {74};

#define DICTIONARY_SIZE 74
#define TYPO_BUFFER_SIZE 6

static const uint8_t autocorrect_data[DICTIONARY_SIZE] PROGMEM = {85, 7, 0, 23, 35, 0, 0, 8, 0, 76, 16, 0, 15, 25, 0, 0,
11, 23, 44, 0, 130, 101, 105, 114, 0, 23, 12, 9, 0, 131, 108, 116, 101, 114, 0, 75, 42, 0, 24, 64, 0, 0, 71, 49, 0,
Expand Down Expand Up @@ -86,11 +91,13 @@ The `qmk generate-autocorrect-data` commands can make an effort to check for ent

### Using multiple dictionaries

Including an additional dictionary under the file `autocorrect_data_alt.h` allows for on the fly switching between two sets of autocorrection rules,
useful for bilingual users or for running context-specific rulesets.
`QK_AUTOCORRECT_BANK_TOGGLE` can then be used to toggle the active dictionary and persist the selection to eeprom.
Including additional dictionaries allows for on-the-fly switching between sets of autocorrection rules, useful for bilingual users or for running context-specific rulesets.
`QK_AUTOCORRECT_DICT_CYCLE` can then be used to cycle the active dictionary and persist the selection to eeprom.

To use this feature, `autocorrect_data_alt.h` should be generated using the flag `-a`, which constructs an alternate dictionary with appropriately named constants
To use this feature, you can input several files into `qmk generate-autocorrect-data`.
```sh
qmk generate-autocorrect-data dict1.txt dict2.txt
```

## Overriding Autocorrect

Expand All @@ -106,12 +113,12 @@ Additionally, you can use the `AC_TOGG` keycode to toggle the on/off status for

### Keycodes :id=keycodes

|Keycode |Aliases |Description |
|----------------------------|---------|--------------------------------------------------------------------------------------|
|`QK_AUTOCORRECT_ON` |`AC_ON` |Turns on the Autocorrect feature. |
|`QK_AUTOCORRECT_OFF` |`AC_OFF` |Turns off the Autocorrect feature. |
|`QK_AUTOCORRECT_TOGGLE` |`AC_TOGG`|Toggles the status of the Autocorrect feature. |
|`QK_AUTOCORRECT_BANK_TOGGLE`|`AC_BANK`|Toggles the dictionary in use by the Autocorrect feature (if an alternate is present).|
|Keycode |Aliases |Description |
|----------------------------|---------|-----------------------------------------------------------------|
|`QK_AUTOCORRECT_ON` |`AC_ON` |Turns on the Autocorrect feature. |
|`QK_AUTOCORRECT_OFF` |`AC_OFF` |Turns off the Autocorrect feature. |
|`QK_AUTOCORRECT_TOGGLE` |`AC_TOGG`|Toggles the status of the Autocorrect feature. |
|`QK_AUTOCORRECT_DICT_CYCLE` |`AC_DICT`|Cycle through dictionaries, reverse direction when Shift is held.|

## User Callback Functions

Expand Down Expand Up @@ -254,15 +261,13 @@ bool apply_autocorrect(uint8_t backspaces, const char *str, char *typo, char *co

Additional user callback functions to manipulate Autocorrect:

| Function | Description |
|----------------------------|----------------------------------------------------------------------------------|
| `autocorrect_enable()` | Turns Autocorrect on. |
| `autocorrect_disable()` | Turns Autocorrect off. |
| `autocorrect_toggle()` | Toggles Autocorrect. |
| `autocorrect_is_enabled()` | Returns true if Autocorrect is currently on. |
| `autocorrect_bank_toggle()`| Toggles and initializes Autocorrect dictionary (if multiple present) |
| `autocorrect_init_bank()` | Initializes current dictionary according so selected bank (if multiple present) |

| Function | Description |
|---------------------------------------|----------------------------------------------------------------|
| `autocorrect_enable()` | Turns Autocorrect on. |
| `autocorrect_disable()` | Turns Autocorrect off. |
| `autocorrect_toggle()` | Toggles Autocorrect. |
| `autocorrect_is_enabled()` | Returns true if Autocorrect is currently on. |
| `autocorrect_dict_cycle(bool forward)`| Cycles through dictionaries (parameter controls the direction).|

## Appendix: Trie binary data format :id=appendix

Expand Down
13 changes: 6 additions & 7 deletions docs/keycodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,12 @@ See also: [Auto Shift](feature_auto_shift.md)

See also: [Autocorrect](feature_autocorrect.md)

|Keycode |Aliases |Description |
|----------------------------|---------|--------------------------------------------------------------------------------------|
|`QK_AUTOCORRECT_ON` |`AC_ON` |Turns on the Autocorrect feature. |
|`QK_AUTOCORRECT_OFF` |`AC_OFF` |Turns off the Autocorrect feature. |
|`QK_AUTOCORRECT_TOGGLE` |`AC_TOGG`|Toggles the status of the Autocorrect feature. |
|`QK_AUTOCORRECT_BANK_TOGGLE`|`AC_BANK`|Toggles the dictionary in use by the Autocorrect feature (if an alternate is present).|

|Keycode |Aliases |Description |
|----------------------------|---------|-----------------------------------------------------------------|
|`QK_AUTOCORRECT_ON` |`AC_ON` |Turns on the Autocorrect feature. |
|`QK_AUTOCORRECT_OFF` |`AC_OFF` |Turns off the Autocorrect feature. |
|`QK_AUTOCORRECT_TOGGLE` |`AC_TOGG`|Toggles the status of the Autocorrect feature. |
|`QK_AUTOCORRECT_DICT_CYCLE` |`AC_DICT`|Cycle through dictionaries, reverse direction when Shift is held.|

## Backlighting :id=backlighting

Expand Down
78 changes: 63 additions & 15 deletions lib/python/qmk/cli/generate/autocorrect_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Google LLC

Check failure on line 1 in lib/python/qmk/cli/generate/autocorrect_data.py

View workflow job for this annotation

GitHub Actions / lint

Requires Formatting
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@

import sys
import textwrap
from pathlib import Path
from typing import Any, Dict, Iterator, List, Tuple

from milc import cli
Expand Down Expand Up @@ -249,17 +250,33 @@
return f'0x{b:02X}'


@cli.argument('filename', type=normpath, help='The autocorrection database file')
class AutocorrectDict:
"""Class to generate autocorrect data from a file."""
def __init__(self, path: Path):
self.path = path
self.autocorrections = parse_file(self.path)

self.trie = make_trie(self.autocorrections)
self.data = serialize_trie(self.autocorrections, self.trie)
assert all(0 <= b <= 255 for b in self.data)

self.min_typo = min(self.autocorrections, key=typo_len)[0]
self.max_typo = max(self.autocorrections, key=typo_len)[0]


@cli.argument('filenames', type=normpath, help='The autocorrection database file(s)', nargs='+')
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
@cli.argument('-a', '--alternate', arg_only=True, action='store_true', help="Create an alternate dictionary file")
@cli.subcommand('Generate the autocorrection data file from a dictionary file.')
def generate_autocorrect_data(cli):
autocorrections = parse_file(cli.args.filename)
trie = make_trie(autocorrections)
data = serialize_trie(autocorrections, trie)
if len(cli.args.filenames) > 8:
cli.log.error("Current EEPROM settings can only index up to 8 dicts")
sys.exit(1)

autocorrect_dicts = [AutocorrectDict(path) for path in cli.args.filenames]

current_keyboard = cli.args.keyboard or cli.config.user.keyboard or cli.config.generate_autocorrect_data.keyboard
current_keymap = cli.args.keymap or cli.config.user.keymap or cli.config.generate_autocorrect_data.keymap
Expand All @@ -271,22 +288,53 @@
if current_keyboard and current_keymap:
cli.args.output = locate_keymap(current_keyboard, current_keymap).parent / file_name

assert all(0 <= b <= 255 for b in data)

min_typo = min(autocorrections, key=typo_len)[0]
max_typo = max(autocorrections, key=typo_len)[0]

# Build the autocorrect_data.h file.
autocorrect_data_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '']

autocorrect_data_h_lines.append(f'// Autocorrection dictionary ({len(autocorrections)} entries):')
for typo, correction in autocorrections:
autocorrect_data_h_lines.append(f'// {typo:<{len(max_typo)}} -> {correction}')
n_entries = sum(len(dict_.autocorrections) for dict_ in autocorrect_dicts)
autocorrect_data_h_lines.append(f'// Autocorrection dictionary ({n_entries} entries):')

# collect all information, and write the corrections as comments
data = []
maxs = []
mins = []
sizes = []
for dict_ in autocorrect_dicts:
autocorrect_data_h_lines.append(f'// From {dict_.path}')
for typo, correction in dict_.autocorrections:
autocorrect_data_h_lines.append(f'// {typo:<{len(dict_.max_typo)}} -> {correction}')
autocorrect_data_h_lines.append('// ' + '-' * 15)

data.extend(dict_.data)
maxs.append(len(dict_.max_typo))
mins.append(len(dict_.min_typo))
sizes.append(len(dict_.data))

assert(sum(sizes) == len(data))

offsets = [0]
for size in sizes[:-1]:
offsets.append(offsets[-1] + size)

def list_to_str(x: list) -> str:
"""Helper to stringify the lists"""
return ', '.join(map(str, x))

offsets_str = list_to_str(offsets)
maxs_str = list_to_str(maxs)
mins_str = list_to_str(mins)
sizes_str = list_to_str(sizes)

autocorrect_data_h_lines.append('')
autocorrect_data_h_lines.append(f'#define N_DICTS {len(autocorrect_dicts)}')
autocorrect_data_h_lines.append('')
autocorrect_data_h_lines.append(f'static const uint16_t autocorrect_offsets[N_DICTS] PROGMEM = {{{offsets_str}}};')
autocorrect_data_h_lines.append(f'static const uint16_t autocorrect_min_lengths[N_DICTS] PROGMEM = {{{mins_str}}};')
autocorrect_data_h_lines.append(f'static const uint16_t autocorrect_max_lengths[N_DICTS] PROGMEM = {{{maxs_str}}};')
autocorrect_data_h_lines.append(f'static const uint16_t autocorrect_sizes[N_DICTS] PROGMEM = {{{sizes_str}}};')
autocorrect_data_h_lines.append('')
autocorrect_data_h_lines.append(f'#define AUTOCORRECT_MIN_LENGTH{defines_suffix} {len(min_typo)} // "{min_typo}"')
autocorrect_data_h_lines.append(f'#define AUTOCORRECT_MAX_LENGTH{defines_suffix} {len(max_typo)} // "{max_typo}"')
autocorrect_data_h_lines.append(f'#define DICTIONARY_SIZE{defines_suffix} {len(data)}')
autocorrect_data_h_lines.append(f'#define DICTIONARY_SIZE {sum(sizes)}')
autocorrect_data_h_lines.append(f'#define TYPO_BUFFER_SIZE {max(maxs)}')
autocorrect_data_h_lines.append('')
autocorrect_data_h_lines.append(f'static const uint8_t autocorrect_data{static_suffix}[DICTIONARY_SIZE{defines_suffix}] PROGMEM = {{')
autocorrect_data_h_lines.append(textwrap.fill(' %s' % (', '.join(map(to_hex, data))), width=100, subsequent_indent=' '))
Expand Down
2 changes: 1 addition & 1 deletion quantum/keycode_config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2016 Jack Humbert

Check failure on line 1 in quantum/keycode_config.h

View workflow job for this annotation

GitHub Actions / lint

Requires Formatting
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -44,7 +44,7 @@
bool oneshot_enable : 1;
bool swap_escape_capslock : 1;
bool autocorrect_enable : 1;
bool autocorrect_bank : 1;
uint8_t autocorrect_curr_dict : 3;
};
} keymap_config_t;

Expand Down
8 changes: 4 additions & 4 deletions quantum/keycodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ enum qk_keycode_defines {
QK_TRI_LAYER_UPPER = 0x7C78,
QK_REPEAT_KEY = 0x7C79,
QK_ALT_REPEAT_KEY = 0x7C7A,
QK_AUTOCORRECT_BANK_TOGGLE = 0x7C7B,
QK_AUTOCORRECT_DICT_CYCLE = 0x7C7B,
QK_KB_0 = 0x7E00,
QK_KB_1 = 0x7E01,
QK_KB_2 = 0x7E02,
Expand Down Expand Up @@ -1367,7 +1367,7 @@ enum qk_keycode_defines {
TL_UPPR = QK_TRI_LAYER_UPPER,
QK_REP = QK_REPEAT_KEY,
QK_AREP = QK_ALT_REPEAT_KEY,
AC_BANK = QK_AUTOCORRECT_BANK_TOGGLE,
AC_DICT = QK_AUTOCORRECT_DICT_CYCLE,
};

// Range Helpers
Expand Down Expand Up @@ -1419,7 +1419,7 @@ enum qk_keycode_defines {
#define IS_MACRO_KEYCODE(code) ((code) >= QK_MACRO_0 && (code) <= QK_MACRO_31)
#define IS_BACKLIGHT_KEYCODE(code) ((code) >= QK_BACKLIGHT_ON && (code) <= QK_BACKLIGHT_TOGGLE_BREATHING)
#define IS_RGB_KEYCODE(code) ((code) >= RGB_TOG && (code) <= RGB_MODE_TWINKLE)
#define IS_QUANTUM_KEYCODE(code) ((code) >= QK_BOOTLOADER && (code) <= QK_AUTOCORRECT_BANK_TOGGLE)
#define IS_QUANTUM_KEYCODE(code) ((code) >= QK_BOOTLOADER && (code) <= QK_AUTOCORRECT_DICT_CYCLE)
#define IS_KB_KEYCODE(code) ((code) >= QK_KB_0 && (code) <= QK_KB_31)
#define IS_USER_KEYCODE(code) ((code) >= QK_USER_0 && (code) <= QK_USER_31)

Expand All @@ -1441,6 +1441,6 @@ enum qk_keycode_defines {
#define MACRO_KEYCODE_RANGE QK_MACRO_0 ... QK_MACRO_31
#define BACKLIGHT_KEYCODE_RANGE QK_BACKLIGHT_ON ... QK_BACKLIGHT_TOGGLE_BREATHING
#define RGB_KEYCODE_RANGE RGB_TOG ... RGB_MODE_TWINKLE
#define QUANTUM_KEYCODE_RANGE QK_BOOTLOADER ... QK_AUTOCORRECT_BANK_TOGGLE
#define QUANTUM_KEYCODE_RANGE QK_BOOTLOADER ... QK_AUTOCORRECT_DICT_CYCLE
#define KB_KEYCODE_RANGE QK_KB_0 ... QK_KB_31
#define USER_KEYCODE_RANGE QK_USER_0 ... QK_USER_31
9 changes: 7 additions & 2 deletions quantum/process_keycode/autocorrect_data_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@
// udpate -> update
// widht -> width

#define AUTOCORRECT_MIN_LENGTH 5 // ":ture"
#define AUTOCORRECT_MAX_LENGTH 10 // "accomodate"
#define N_DICTS 1

static const uint16_t autocorrect_offsets[N_DICTS] PROGMEM = {0};
static const uint16_t autocorrect_min_lengths[N_DICTS] PROGMEM = {5};
static const uint16_t autocorrect_max_lengths[N_DICTS] PROGMEM = {10};
static const uint16_t autocorrect_sizes[N_DICTS] PROGMEM = {1104};

#define DICTIONARY_SIZE 1104
#define TYPO_BUFFER_SIZE 10

static const uint8_t autocorrect_data[DICTIONARY_SIZE] PROGMEM = {108, 43, 0, 6, 71, 0, 7, 81, 0, 8, 199, 0, 9, 240, 1, 10, 250, 1, 11, 26, 2, 17, 53, 2, 18, 190, 2, 19, 202, 2, 21, 212, 2, 22, 20, 3, 23, 67, 3, 28, 16, 4, 0, 72, 50, 0, 22, 60, 0, 0, 11, 23, 44, 8, 11, 23, 44, 0, 132, 0, 8, 22, 18, 18, 15, 0, 132, 115, 101, 115, 0, 11, 23, 12, 26, 22, 0, 129, 99, 104, 0, 68, 94, 0, 8, 106, 0, 15, 174, 0, 21, 187, 0, 0, 12, 15, 25, 17, 12, 0, 131, 97, 108, 105, 100, 0, 74, 119, 0, 12, 129, 0, 21, 140, 0, 24, 165, 0, 0, 17, 12, 22, 0, 131, 103, 110, 101, 100, 0, 25, 21, 8, 7, 0, 131, 105, 118, 101, 100, 0, 72, 147, 0, 24, 156, 0, 0, 9, 8, 21, 0, 129, 114, 101, 100, 0, 6, 6, 18, 0, 129, 114, 101, 100, 0, 15, 6, 17, 12, 0, 129, 100, 101, 0, 18, 22, 8, 21, 11, 23, 0, 130, 104, 111,
108, 100, 0, 4, 26, 18, 9, 0, 131, 114, 119, 97, 114, 100, 0, 68, 233, 0, 6, 246, 0, 7, 4, 1, 8, 16, 1, 10, 52, 1, 15, 81, 1, 21, 90, 1, 22, 117, 1, 23, 144, 1, 24, 215, 1, 25, 228, 1, 0, 6, 19, 22, 8, 16, 4, 17, 0, 130, 97, 99, 101, 0, 19, 4, 22, 8, 16, 4, 17, 0, 131, 112, 97, 99, 101, 0, 12, 21, 8, 25, 18, 0, 130, 114, 105, 100, 101, 0, 23, 0, 68, 25, 1, 17, 36, 1, 0, 21, 4, 24, 10, 0, 130, 110, 116, 101, 101, 0, 4, 21, 24, 4, 10, 0, 135, 117, 97, 114, 97, 110, 116, 101, 101, 0, 68, 59, 1, 7, 69, 1, 0, 24, 10, 44, 0, 131, 97, 117, 103, 101, 0, 8, 15, 12, 25, 12, 21, 19, 0, 130, 103, 101, 0, 22, 4, 9, 0, 130, 108, 115, 101, 0, 76, 97, 1, 24, 109, 1, 0, 24, 20, 4, 0, 132, 99, 113, 117, 105, 114, 101, 0, 23, 44, 0,
Expand Down
Loading
Loading