Remove the 'autocorrection' library

Although very useful, every application I care about already offers
spell suggestions, so having `autocorrection` on top of that seems
redundant.
This commit is contained in:
Chris Zervakis 2024-10-15 09:09:49 +03:00
parent 7bef010bd9
commit 0fe3467a64
5 changed files with 0 additions and 496 deletions

View file

@ -1,208 +0,0 @@
// Copyright 2021-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file autocorrection.c
* @brief Autocorrection implementation
*
* For full documentation, see
* <https://getreuer.info/posts/keyboards/autocorrection>
*/
#include "autocorrection.h"
#include <string.h>
#include "autocorrection_data.h"
#pragma message \
"Autocorrect is now a core QMK feature! To use it, update your QMK set up and see https://docs.qmk.fm/features/autocorrect"
#if AUTOCORRECTION_MIN_LENGTH < 4
// Odd output or hard locks on the board have been observed when the min typo
// length is 3 or lower (https://github.com/getreuer/qmk-keymap/issues/2).
// Additionally, autocorrection entries for short typos are more likely to false
// trigger, so it is suggested that typos be at least 5 characters.
#error "Min typo length is less than 4. Autocorrection may behave poorly."
#endif
bool process_autocorrection(uint16_t keycode, keyrecord_t* record) {
static uint8_t typo_buffer[AUTOCORRECTION_MAX_LENGTH] = {0};
static uint8_t typo_buffer_size = 0;
// Ignore key release; we only process key presses.
if (!record->event.pressed) {
return true;
}
#ifndef NO_ACTION_ONESHOT
const uint8_t mods = get_mods() | get_oneshot_mods();
#else
const uint8_t mods = get_mods();
#endif // NO_ACTION_ONESHOT
// Disable autocorrection while a mod other than shift is active.
if ((mods & ~MOD_MASK_SHIFT) != 0) {
typo_buffer_size = 0;
return true;
}
// The following switch cases address various kinds of keycodes. This logic is
// split over two switches rather than merged into one. The first switch may
// extract a basic keycode which is then further handled by the second switch,
// e.g. a layer-tap key with Caps Lock `LT(layer, KC_CAPS)`.
switch (keycode) {
#ifndef NO_ACTION_TAPPING
case QK_MOD_TAP ... QK_MOD_TAP_MAX: // Tap-hold keys.
#ifndef NO_ACTION_LAYER
case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
#endif // NO_ACTION_LAYER
// Ignore when tap-hold keys are held.
if (record->tap.count == 0) {
return true;
}
// Otherwise when tapped, get the basic keycode.
// Fallthrough intended.
#endif // NO_ACTION_TAPPING
// Handle shifted keys, e.g. symbols like KC_EXLM = S(KC_1).
case QK_LSFT ... QK_LSFT + 255:
case QK_RSFT ... QK_RSFT + 255:
keycode = QK_MODS_GET_BASIC_KEYCODE(keycode);
break;
// NOTE: Space Cadet keys expose no info to check whether they are being
// tapped vs. held. This makes autocorrection ambiguous, e.g. KC_LCPO
// might be '(', which we would treat as a word break, or it might be
// shift, which we would treat as having no effect. To behave cautiously,
// we allow Space Cadet keycodes to fall to the logic below and clear
// autocorrection state.
}
switch (keycode) {
// Ignore shifts, Caps Lock, one-shot mods, and layer switch keys.
case KC_NO:
case KC_LSFT:
case KC_RSFT:
case KC_CAPS:
case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
case QK_TO ... QK_TO_MAX:
case QK_MOMENTARY ... QK_MOMENTARY_MAX:
case QK_DEF_LAYER ... QK_DEF_LAYER_MAX:
case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
return true; // Ignore these keys.
}
if (keycode == KC_QUOT) {
// Treat " (shifted ') as a word boundary.
if ((mods & MOD_MASK_SHIFT) != 0) {
keycode = KC_SPC;
}
} else if (!(KC_A <= keycode && keycode <= KC_Z)) {
if (keycode == KC_BSPC) {
// Remove last character from the buffer.
if (typo_buffer_size > 0) {
--typo_buffer_size;
}
return true;
} else if (KC_1 <= keycode && keycode <= KC_SLSH && keycode != KC_ESC) {
// Set a word boundary if space, period, digit, etc. is pressed.
// Behave more conservatively for the enter key. Reset, so that enter
// can't be used on a word ending.
if (keycode == KC_ENT) {
typo_buffer_size = 0;
}
keycode = KC_SPC;
} else {
// Clear state if some other non-alpha key is pressed.
typo_buffer_size = 0;
return true;
}
}
// If the buffer is full, rotate it to discard the oldest character.
if (typo_buffer_size >= AUTOCORRECTION_MAX_LENGTH) {
memmove(typo_buffer, typo_buffer + 1, AUTOCORRECTION_MAX_LENGTH - 1);
typo_buffer_size = AUTOCORRECTION_MAX_LENGTH - 1;
}
// Append `keycode` to the buffer.
// NOTE: `keycode` must be a basic keycode (0-255) by this point.
typo_buffer[typo_buffer_size++] = (uint8_t)keycode;
// Early return if not many characters have been buffered so far.
if (typo_buffer_size < AUTOCORRECTION_MIN_LENGTH) {
return true;
}
// Check whether the buffer ends in a typo. This is done using a trie
// stored in `autocorrection_data`.
uint16_t state = 0;
uint8_t code = pgm_read_byte(autocorrection_data + state);
for (int i = typo_buffer_size - 1; i >= 0; --i) {
const uint8_t key_i = typo_buffer[i];
if (code & 64) { // Check for match in node with multiple children.
code &= 63;
for (; code != key_i;
code = pgm_read_byte(autocorrection_data + (state += 3))) {
if (!code) {
return true;
}
}
// Follow link to child node.
state = (uint16_t)((uint_fast16_t)pgm_read_byte(autocorrection_data +
state + 1) |
(uint_fast16_t)pgm_read_byte(autocorrection_data +
state + 2)
<< 8);
// Otherwise check for match in node with a single child.
} else if (code != key_i) {
return true;
} else if (!(code = pgm_read_byte(autocorrection_data + (++state)))) {
++state;
}
// Stop if `state` becomes an invalid index. This should not normally
// happen, it is a safeguard in case of a bug, data corruption, etc.
if (state >= sizeof(autocorrection_data)) {
return true;
}
// Read first byte of the next node.
code = pgm_read_byte(autocorrection_data + state);
if (code & 128) { // A typo was found! Apply autocorrection.
const int backspaces = code & 63;
for (int i = 0; i < backspaces; ++i) {
tap_code(KC_BSPC);
}
send_string_P((char const*)(autocorrection_data + state + 1));
if (keycode == KC_SPC) {
typo_buffer[0] = KC_SPC;
typo_buffer_size = 1;
return true;
} else {
typo_buffer_size = 0;
return false;
}
}
}
return true;
}

View file

@ -1,115 +0,0 @@
// Copyright 2021-2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file autocorrection.h
* @brief Autocorrection on your keyboard.
*
* Overview
* --------
*
* @note Autocorrect is now a core QMK feature! See
* <https://docs.qmk.fm/features/autocorrect>
*
* Some words are more prone to typos than others. This userspace QMK library
* implements rudimentary autocorrection, automatically detecting and fixing
* some misspellings.
*
* Features:
*
* * It runs on your keyboard, so it is always active no matter what software.
* * Low resource cost.
* * It is case insensitive.
* * It works within words, useful for programming to catch typos within longer
* identifiers.
*
* Limitations:
*
* * It is limited to alphabet characters az, apostrophes ', and word breaks.
* I'm sorry this probably isn't useful for languages besides English.
* * It does not follow mouse or hotkey driven cursor movement.
*
* Changing the autocorrection dictionary
* --------------------------------------
*
* The file autocorrection_data.h encodes the typos to correct. While you could
* simply use the version of this file provided above for a practical
* configuration, you can make your own to personalize the autocorrection to
* your most troublesome typos:
*
* Step 1: First, create an autocorrection dictionary autocorrection_dict.txt,
* in a form like
*
* :thier -> their
* dosen't -> doesn't
* fitler -> filter
* ouput -> output
* widht -> width
*
* For a practical 71-entry example, see autocorrection_dict.txt. And for a yet
* larger 400-entry example, see autocorrection_dict_extra.txt.
*
* The syntax is `typo -> correction`. Typos and corrections are case
* insensitive, and any whitespace before or after the typo and correction is
* ignored. The typo must be only the characters az, ', or the special
* character : representing a word break. The correction may have just about any
* printable ASCII characters.
*
* Step 2: Use the make_autocorrection_data.py Python script to process the
* dictionary. Put autocorrection_dict.txt in the same directory as the Python
* script and run
*
* $ python3 make_autocorrection_data.py
* Processed 71 autocorrection entries to table with 1120 bytes.
*
* The script arranges the entries in autocorrection_dict.txt into a trie and
* generates autocorrection_data.h with the serialized trie embedded as an
* array. The .h file will be written in the same directory.
*
* Step 3: Finally, recompile and flash your keymap.
*
* For full documentation, see
* <https://getreuer.info/posts/keyboards/autocorrection>
*
* @author Pascal Getreuer
*/
#pragma once
#include "quantum.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Handler function for autocorrection.
*
* Call this function in keymap.c from `process_record_user()` like
*
* #include "features/autocorrection.h"
*
* bool process_record_user(uint16_t keycode, keyrecord_t* record) {
* if (!process_autocorrection(keycode, record)) { return false; }
* // Your macros...
*
* return true;
* }
*/
bool process_autocorrection(uint16_t keycode, keyrecord_t* record);
#ifdef __cplusplus
}
#endif

View file

@ -1,169 +0,0 @@
// Copyright 2021-2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Generated code.
// Autocorrection dictionary (71 entries):
// :guage -> gauge
// :the:the: -> the
// :thier -> their
// :ture -> true
// accomodate -> accommodate
// acommodate -> accommodate
// aparent -> apparent
// aparrent -> apparent
// apparant -> apparent
// apparrent -> apparent
// aquire -> acquire
// becuase -> because
// cauhgt -> caught
// cheif -> chief
// choosen -> chosen
// cieling -> ceiling
// collegue -> colleague
// concensus -> consensus
// contians -> contains
// cosnt -> const
// dervied -> derived
// dosen't -> doesn't
// fales -> false
// fasle -> false
// fitler -> filter
// flase -> false
// foward -> forward
// frequecy -> frequency
// gaurantee -> guarantee
// guaratee -> guarantee
// heigth -> height
// heirarchy -> hierarchy
// inclued -> include
// interator -> iterator
// intput -> input
// invliad -> invalid
// lenght -> length
// liasion -> liaison
// libary -> library
// listner -> listener
// looses: -> loses
// looup -> lookup
// manefist -> manifest
// namesapce -> namespace
// namespcae -> namespace
// occassion -> occasion
// occured -> occurred
// ouptut -> output
// ouput -> output
// overide -> override
// postion -> position
// priviledge -> privilege
// psuedo -> pseudo
// recieve -> receive
// refered -> referred
// relevent -> relevant
// repitition -> repetition
// retrun -> return
// retun -> return
// reuslt -> result
// reutrn -> return
// saftey -> safety
// seperate -> separate
// singed -> signed
// stirng -> string
// strign -> string
// swithc -> switch
// swtich -> switch
// thresold -> threshold
// udpate -> update
// widht -> width
#define AUTOCORRECTION_MIN_LENGTH 5 // ":ture"
#define AUTOCORRECTION_MAX_LENGTH 10 // "accomodate"
static const uint8_t autocorrection_data[1120] 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, 32, 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, 130, 114, 117, 101, 0, 4, 0,
79, 126, 1, 24, 134, 1, 0, 9, 0, 131, 97, 108, 115, 101, 0,
6, 8, 5, 0, 131, 97, 117, 115, 101, 0, 4, 0, 71, 156, 1,
19, 193, 1, 21, 203, 1, 0, 18, 16, 0, 80, 166, 1, 18, 181,
1, 0, 18, 6, 4, 0, 135, 99, 111, 109, 109, 111, 100, 97, 116,
101, 0, 6, 6, 4, 0, 132, 109, 111, 100, 97, 116, 101, 0, 7,
24, 0, 132, 112, 100, 97, 116, 101, 0, 8, 19, 8, 22, 0, 132,
97, 114, 97, 116, 101, 0, 10, 8, 15, 15, 18, 6, 0, 130, 97,
103, 117, 101, 0, 8, 12, 6, 8, 21, 0, 131, 101, 105, 118, 101,
0, 12, 8, 11, 6, 0, 130, 105, 101, 102, 0, 17, 0, 76, 3,
2, 21, 16, 2, 0, 15, 8, 12, 6, 0, 133, 101, 105, 108, 105,
110, 103, 0, 12, 23, 22, 0, 131, 114, 105, 110, 103, 0, 70, 33,
2, 23, 44, 2, 0, 12, 23, 26, 22, 0, 131, 105, 116, 99, 104,
0, 10, 12, 8, 11, 0, 129, 104, 116, 0, 72, 69, 2, 10, 80,
2, 18, 89, 2, 21, 156, 2, 24, 167, 2, 0, 22, 18, 18, 11,
6, 0, 131, 115, 101, 110, 0, 12, 21, 23, 22, 0, 129, 110, 103,
0, 12, 0, 86, 98, 2, 23, 124, 2, 0, 68, 105, 2, 22, 114,
2, 0, 12, 15, 0, 131, 105, 115, 111, 110, 0, 4, 6, 6, 18,
0, 131, 105, 111, 110, 0, 76, 131, 2, 22, 146, 2, 0, 23, 12,
19, 8, 21, 0, 134, 101, 116, 105, 116, 105, 111, 110, 0, 18, 19,
0, 131, 105, 116, 105, 111, 110, 0, 23, 24, 8, 21, 0, 131, 116,
117, 114, 110, 0, 85, 174, 2, 23, 183, 2, 0, 23, 8, 21, 0,
130, 117, 114, 110, 0, 8, 21, 0, 128, 114, 110, 0, 7, 8, 24,
22, 19, 0, 131, 101, 117, 100, 111, 0, 24, 18, 18, 15, 0, 129,
107, 117, 112, 0, 72, 219, 2, 18, 3, 3, 0, 76, 229, 2, 15,
238, 2, 17, 248, 2, 0, 11, 23, 44, 0, 130, 101, 105, 114, 0,
23, 12, 9, 0, 131, 108, 116, 101, 114, 0, 23, 22, 12, 15, 0,
130, 101, 110, 101, 114, 0, 23, 4, 21, 8, 23, 17, 12, 0, 135,
116, 101, 114, 97, 116, 111, 114, 0, 72, 30, 3, 17, 38, 3, 24,
51, 3, 0, 15, 4, 9, 0, 129, 115, 101, 0, 4, 12, 23, 17,
18, 6, 0, 131, 97, 105, 110, 115, 0, 22, 17, 8, 6, 17, 18,
6, 0, 133, 115, 101, 110, 115, 117, 115, 0, 116, 89, 3, 10, 102,
3, 11, 112, 3, 15, 134, 3, 17, 145, 3, 22, 234, 3, 24, 248,
3, 0, 17, 8, 22, 18, 7, 0, 132, 101, 115, 110, 39, 116, 0,
11, 24, 4, 6, 0, 130, 103, 104, 116, 0, 71, 119, 3, 10, 126,
3, 0, 12, 26, 0, 129, 116, 104, 0, 17, 8, 15, 0, 129, 116,
104, 0, 22, 24, 8, 21, 0, 131, 115, 117, 108, 116, 0, 68, 155,
3, 8, 166, 3, 22, 226, 3, 0, 21, 4, 19, 19, 4, 0, 130,
101, 110, 116, 0, 85, 173, 3, 25, 216, 3, 0, 68, 180, 3, 21,
191, 3, 0, 19, 4, 0, 132, 112, 97, 114, 101, 110, 116, 0, 4,
19, 0, 68, 201, 3, 19, 209, 3, 0, 133, 112, 97, 114, 101, 110,
116, 0, 4, 0, 131, 101, 110, 116, 0, 8, 15, 8, 21, 0, 130,
97, 110, 116, 0, 18, 6, 0, 130, 110, 115, 116, 0, 12, 9, 8,
17, 4, 16, 0, 132, 105, 102, 101, 115, 116, 0, 83, 255, 3, 23,
22, 4, 0, 87, 6, 4, 24, 14, 4, 0, 17, 12, 0, 131, 112,
117, 116, 0, 18, 0, 130, 116, 112, 117, 116, 0, 19, 24, 18, 0,
131, 116, 112, 117, 116, 0, 70, 45, 4, 8, 57, 4, 11, 67, 4,
21, 85, 4, 0, 8, 24, 20, 8, 21, 9, 0, 129, 110, 99, 121,
0, 23, 9, 4, 22, 0, 130, 101, 116, 121, 0, 6, 21, 4, 21,
12, 8, 11, 0, 135, 105, 101, 114, 97, 114, 99, 104, 121, 0, 4,
5, 12, 15, 0, 130, 114, 97, 114, 121, 0};

View file

@ -1,6 +1,5 @@
#include QMK_KEYBOARD_H
#include "features/select_word.h"
#include "features/autocorrection.h"
#include "features/achordion.h"
// Select word (https://getreuer.info/posts/keyboards/select-word/index.html)
@ -160,8 +159,6 @@ bool process_record_user(uint16_t keycode, keyrecord_t* record) {
// Select word
if (!process_select_word(keycode, record, SELWORD)) { return false; }
if (!process_autocorrection(keycode, record)) { return false; }
return true;
}

View file

@ -7,7 +7,6 @@ CAPS_WORD_ENABLE = yes
DYNAMIC_TAPPING_TERM_ENABLE = yes
SRC += features/select_word.c
SRC += features/autocorrection.c
SRC += features/achordion.c
# Diasble features we don't use to reduce firmware size