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

Add suggestions when misspelling a record field #1710

Merged
merged 12 commits into from
Nov 13, 2023

Conversation

yannham
Copy link
Member

@yannham yannham commented Nov 9, 2023

Closes #1708.

This pull request shows suggestions of similar field names when trying to access a non-existent field of a record.

This first implementation uses the Damerau-Levensthein distance. The choice of the algorithm is inspired from the Rust compiler, although they use a more involved and hand-written variant with corrections around substrings: we just take one from the seemingly most popular string similarity library on crates.io, strsim.

More specifically, we try to find either a perfect match ignoring cases, or the field with the highest similarity with the user input, as long as it's above some threshold (to avoid suggesting things that have nothing to do with the input). The threshold was chosen rather arbitrarily, by trial and error: it seems that choosing a higher threshold didn't work that well for very small words (4 letters word), which can have low similarity even with an edit distance of 1 or 2.

@yannham yannham marked this pull request as ready for review November 9, 2023 15:50
@github-actions github-actions bot temporarily deployed to pull request November 9, 2023 15:52 Inactive
Copy link
Contributor

@thufschmitt thufschmitt left a comment

Choose a reason for hiding this comment

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

Thanks!

core/src/error/suggest.rs Show resolved Hide resolved
core/src/error/suggest.rs Show resolved Hide resolved
id: ident,
field_names: record
.field_names(RecordOpKind::IgnoreEmptyOpt),
operator: String::from("(.$)"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to this PR, but where is that .$ coming from?

Copy link
Member Author

@yannham yannham Nov 10, 2023

Choose a reason for hiding this comment

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

Oh, good catch. In fact the operator wasn't shown before this PR (basically the notes were created but never used - it's unrelated to this PR, it's just that I needed to add the suggestion to the notes, so I realized, and it was in fact surprising that the rust compiler haven't yelled at me for unused value until now). So we might have let old names slipped through.

I believe this was the original syntax for dynamic access field, like foo.$x was the current foo."%{x}", because we used to have $ as the interpolation character and we didn't have quoted fields yet, such as foo."bar^baz". When we added the later syntax, it felt better to avoid keeping the strange .$ which could just be subsumed by quoted field access + interpolation.

@github-actions github-actions bot temporarily deployed to pull request November 10, 2023 17:45 Inactive
@github-actions github-actions bot temporarily deployed to pull request November 13, 2023 08:58 Inactive
@yannham yannham added this pull request to the merge queue Nov 13, 2023
Merged via the queue into master with commit c072941 Nov 13, 2023
5 checks passed
@yannham yannham deleted the feat/mispelling-suggestion branch November 13, 2023 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest alternatives when trying to access a non-existent record field
3 participants