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

[Core] Add getreuer's Autocorrect feature to core #15699

Merged
merged 2 commits into from
Sep 17, 2022

Conversation

drashna
Copy link
Member

@drashna drashna commented Jan 2, 2022

Description

As title.
It's a super cool feature, works well, and would be a great addition to QMK.

Types of Changes

  • Core
  • New feature
  • Enhancement/optimization
  • Documentation

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Copy link
Contributor

@getreuer getreuer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Drashna! This is awesome. Here are a few nits and suggestions.

quantum/process_keycode/process_autocorrect.c Outdated Show resolved Hide resolved
lib/python/qmk/cli/generate/autocorrect_data.py Outdated Show resolved Hide resolved
quantum/quantum_keycodes.h Outdated Show resolved Hide resolved
quantum/process_keycode/process_autocorrect.c Outdated Show resolved Hide resolved
lib/python/qmk/cli/generate/autocorrect_data.py Outdated Show resolved Hide resolved
lib/python/qmk/cli/generate/autocorrect_data.py Outdated Show resolved Hide resolved
lib/python/qmk/cli/generate/autocorrect_data.py Outdated Show resolved Hide resolved
@drashna
Copy link
Member Author

drashna commented Jan 8, 2022

lib/python/qmk/cli/generate/autocorrect_data.py:55:1: C901 'parse_file' is too complex (20)

I'm not sure how to fix this.

Nor these:

lib/python/qmk/cli/generate/autocorrect_data.py:193:9: E731 do not assign a lambda expression, use a def
lib/python/qmk/cli/generate/autocorrect_data.py:222:5: E731 do not assign a lambda expression, use a def

Also, if "english_words" is added to requirements.txt, the unit tests fail (presumably because it's asking to install dependencies), and if it's not, cli ci fails

getreuer added a commit to getreuer/qmk-keymap that referenced this pull request Jan 9, 2022
Some style changes to hopefully address lint errors that came up in
qmk/qmk_firmware#15699.

* Break up `parse_file()` into several functions.
* Avoid using `lambda`.
* Better error message if node link exceeds 64KB limit.
@getreuer
Copy link
Contributor

getreuer commented Jan 9, 2022

Drashna, thank you for all your work on getting this together! Here are some suggestions that hopefully fix these lint errors. I also just made these style fixes in my repo in getreuer/qmk-keymap@c28e526 to show concretely what I suggest below.

lib/python/qmk/cli/generate/autocorrect_data.py:55:1: C901 'parse_file' is too complex (20)

I'm not sure how to fix this.

This is saying there are too many branches and loops in parse_file. We should pull some bits out as separate functions, which parse_file calls. For instance define

def check_typo_against_dictionary(typo: str) -> None:
  """Checks `typo` against English dictionary words."""

  if typo.startswith(':') and typo.endswith(':'):
    if typo[1:-1] in CORRECT_WORDS:
      print(f'Warning:{line_number}: Typo "{typo}" is a correctly spelled '
            'dictionary word.')
  elif typo.startswith(':') and not typo.endswith(':'):
    for word in CORRECT_WORDS:
      if word.startswith(typo[1:]):
        print(f'Warning:{line_number}: Typo "{typo}" would falsely trigger '
              f'on correctly spelled word "{word}".')
  elif not typo.startswith(':') and typo.endswith(':'):
    for word in CORRECT_WORDS:
      if word.endswith(typo[:-1]):
        print(f'Warning:{line_number}: Typo "{typo}" would falsely trigger '
              f'on correctly spelled word "{word}".')
  elif not typo.startswith(':') and not typo.endswith(':'):
    for word in CORRECT_WORDS:
      if typo in word:
        print(f'Warning:{line_number}: Typo "{typo}" would falsely trigger '
              f'on correctly spelled word "{word}".')

and then call this function in the loop in parse_file().

Nor these:

lib/python/qmk/cli/generate/autocorrect_data.py:193:9: E731 do not assign a lambda expression, use a def
lib/python/qmk/cli/generate/autocorrect_data.py:222:5: E731 do not assign a lambda expression, use a def

The lint message is (tersely) saying that we should rewrite an assigned lambda like

typo_len = lambda e: len(e[0])

as

def typo_len(e: Tuple[str, str]) -> int:
    return len(e[0])

Also, if "english_words" is added to requirements.txt, the unit tests fail (presumably because it's asking to install dependencies), and if it's not, cli ci fails

english_words is an optional dependency: if it is present, it is used to warn if the typos would create false triggers on correctly-spelled English words, but the program still produces the trie data either way. On the one hand, we could simply remove the english_words import if adding new dependencies is a big obstacle. On the other hand, it is useful to have this checking, so it would make sense if qmk could take on english_words as a dependency. Or, if there is an easier way to get an English dictionary, we could change the code to use that instead. I'm open to moving forward with whatever path you prefer.

Thanks again! =)

@drashna
Copy link
Member Author

drashna commented Jan 9, 2022

Thanks! I think i got most of it working. Though, the try part is causing the pytest stuff to error out, because it's being called in every python file, I think (due to how the cli stuff is configured).

I think it would work if the try import was moved into parse_file function instead. But I'm not exactly sure how to handle that, and properly set the correct_words stuff.

(i'm definitely not a python person)

@drashna
Copy link
Member Author

drashna commented Jan 9, 2022

got it mostly working, but still got pytest errors.

Mostly, just running into this:

lib/python/qmk/cli/generate/autocorrect_data.py:68:9: F841 local variable 'correct_words' is assigned to but never used

And I'm not sure how to correct it.

@drashna
Copy link
Member Author

drashna commented Jan 9, 2022

Well, got it passing all the tests, and it does appear to be working correctly. So looks like it's good to go.

@getreuer
Copy link
Contributor

getreuer commented Jan 9, 2022

Awesome! Great work, Drashna! 🥳

@drashna
Copy link
Member Author

drashna commented Jan 10, 2022

Awesome! Great work, Drashna! 🥳

Thanks!

And documents added.

@ZenghaoWang
Copy link

Hi Drashna,

I wanted to try this out, but after locally checking out your PR and enabling the feature in my rules.mk, nothing is being autocorrected. I'm a bit lost on how to debug this, any ideas on where to start?

Apologies if this is the wrong place to ask.

@drashna
Copy link
Member Author

drashna commented Jan 13, 2022

keymap config is zero'ed by default, so that would cause this feature to be disabled by default. You'd need to hit the AUTOCRT/AUTOCORRECT_TOGGLE keycode once to enable it, initially.

@ZenghaoWang
Copy link

No change, even after I add AUTOCRT to my keymap and tap it

@drashna
Copy link
Member Author

drashna commented Jan 13, 2022

No change, even after I add AUTOCRT to my keymap and tap it

Odd. I can confirm that it does work, even from this branch directly.

quantum/process_keycode/process_autocorrect.c Show resolved Hide resolved
quantum/process_keycode/process_autocorrect.c Outdated Show resolved Hide resolved
quantum/process_keycode/process_autocorrect.c Outdated Show resolved Hide resolved
quantum/process_keycode/process_autocorrect.c Outdated Show resolved Hide resolved
quantum/process_keycode/process_autocorrect.c Outdated Show resolved Hide resolved
@drashna drashna force-pushed the core/autocorrect branch 2 times, most recently from 3de96bb to 8a572f5 Compare January 29, 2022 02:22
@drashna
Copy link
Member Author

drashna commented Apr 18, 2022

and json support added.

Copy link
Member

@zvecr zvecr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info.json having context of headers bleeds implementation details into something that should be fairly generic. Its also questionable if the feature should be enabled at the keyboard level, which makes the schema/mappings changes not required. My view would be that it should never be.

Providing a default header to fall back on, and then warning seems a tad strange. I'm not really sure what use case that is trying to cover (especially if you take the viewpoint above).

data/mappings/info_config.json Outdated Show resolved Hide resolved
data/schemas/keyboard.jsonschema Outdated Show resolved Hide resolved
@drashna drashna force-pushed the core/autocorrect branch 2 times, most recently from fca16b1 to c65ef0a Compare May 20, 2022 16:53
@drashna drashna force-pushed the core/autocorrect branch 2 times, most recently from e717f19 to e22731e Compare June 18, 2022 03:42
@drashna drashna force-pushed the core/autocorrect branch from e22731e to dca76c8 Compare July 3, 2022 17:05
@drashna drashna force-pushed the core/autocorrect branch from dca76c8 to 9c746c7 Compare July 19, 2022 16:18
@drashna drashna requested a review from a team August 7, 2022 21:20
data/schemas/keyboard.jsonschema Outdated Show resolved Hide resolved
data/mappings/info_rules.json Outdated Show resolved Hide resolved
Fix compilation issues

Clean up names

Reformat python

cleanup

add support for swap hands

Add support for -km and -kb

Ugly hack to get working

Make output pretty

Add support for --output

Fix lint issue

hopefully address cli ci errors

Fix double space

additional tweaks

Apply suggestions from code review

Update lib/python/qmk/cli/generate/autocorrect_data.py

Apply suggestions from code review

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

adddep

Apply cli ci suggestion'

Attempt to fix linter

remove  english_words dep

Brute force hack to pass CI

Fix issue with autocorrect

leave english words enabled

Fix pytest issues?

Get cli python code working

pass on line_number

get tests passed

Add documentation

minor tweaks

Fixes based on feedback

Add fixes based on feedback

Fix rebase conflicts

add one more backspace if it's needed

Add fixes

move pressed processing

improvements based on feedback

"Fix" formatting

fix error output

fix linting

fix linting, more

brute force python formatting fix

test this

fix compilation issues with mods

Add user callback for which keycodes to handle

Allow configurable data file

fix pr linting

better handle keycodes

fix some compiler issues

Add doxygen comment

Fix some edge cases

Remove special case - not sure why I added it

Make buffer check a switch

Add comments

Additional improvements (mods+)

Update quantum/process_keycode/process_autocorrect.c

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

fix oneshot check

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

Add fixes and commenting from filterpaper

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

Fix up switch case

Ignore "dead" keys when features are disabled

fix check

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

Apply suggestions from code review

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

apply documentation suggetion

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

Expand handling for autocorrect triggering

Fix functions and add more docs

Apply suggestions from code review

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

Rename apply function

clarify pointier parameter

Apply suggestions from code review

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

fix docs

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

Enable autocorrect by default

Add on/off keycodes for autocorrect

Move autocorrect to be after existing keycodes

Update generate-autocorrect-data file

Remove empty line

Move svg to be local

It\'s not imgur compatible so host it locally

Switch to imgur hosted png

Fix overflow issue on AVR

Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>

Attempt to add tests?

Add config.h for test

fix formatting

Prevent autocorrect test warning

remove tests

Add tests for autocorrect (wooooo!!!)

Thanks karlk90!!

Re-add autocorrect define to hide no default warning

Add functions for state behavior

Add enable/disable checks

Fix lint issues

Add additional test cases

Clarify names

Revert changes to drashna userspace

for a cleaner commit history

Add info.json support

Add state check

Don't ignore autocorrect h file

Update docs

Changes based on feedback

Remove changes to json stuff
Comment on lines 20 to 21

#define AUTOCORRECT_DATA_H "quantum/process_keycode/autocorrect_data_default.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define AUTOCORRECT_DATA_H "quantum/process_keycode/autocorrect_data_default.h"

@@ -0,0 +1,21 @@
/* Copyright 2021 Stefan Kerkmann
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be yourself.

@@ -0,0 +1,20 @@
# Copyright 2021 Stefan Kerkmann
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be yourself.

@@ -0,0 +1,217 @@
/* Copyright 2017 Fred Sundvik
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be yourself.

@@ -0,0 +1,17 @@
// Copyright 2021 Google LLC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the Google thing because it matches their OSS policy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because that's what the original code is.
https://github.com/getreuer/qmk-keymap/blob/a1f62991a99b205b7d493dcc1fdea3946cbb575c/features/autocorrection.c#L1-L13

And yeah, because of their OSS policies, IIRC>

@@ -0,0 +1,287 @@
// Copyright 2021 Google LLC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the Google thing because it matches their OSS policy?

@@ -0,0 +1,289 @@
# Copyright 2021 Google LLC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the Google thing because it matches their OSS policy?

@tzarc tzarc merged commit fb29c0a into qmk:develop Sep 17, 2022
@drashna drashna deleted the core/autocorrect branch September 17, 2022 15:10
ramonimbao pushed a commit to ramonimbao/qmk_firmware that referenced this pull request Nov 28, 2022
Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants