Skip to content

Add Trie-based Spell Checker with edit distance suggestions#14677

Open
fauzan171 wants to merge 2 commits into
TheAlgorithms:masterfrom
fauzan171:add-trie-spell-checker
Open

Add Trie-based Spell Checker with edit distance suggestions#14677
fauzan171 wants to merge 2 commits into
TheAlgorithms:masterfrom
fauzan171:add-trie-spell-checker

Conversation

@fauzan171
Copy link
Copy Markdown

Description

Added a new trie_spell_checker.py module in data_structures/trie/ that implements a Trie-based Spell Checker with Levenshtein edit distance-based suggestions.

What's New:

  • SpellChecker class - Uses a Trie for efficient dictionary storage
  • is_correct(word) - Check if a word exists in the dictionary
  • suggest(word, max_suggestions=5) - Return closest matches based on edit distance
  • add_word(word) - Dynamically add words to the dictionary
  • Case-insensitive matching - All words are stored and searched in lowercase
  • _edit_distance() static method - Levenshtein distance implementation

Why:

The existing trie.py only provides basic Trie operations (insert, search, delete). A spell checker is a classic and practical application of Trie data structures, commonly taught in data structures courses. This addition demonstrates a real-world use case of Tries.

Testing:

  • All doctests pass
  • Comprehensive test_spell_checker() function included
  • Tested with python3 data_structures/trie/trie_spell_checker.py

Checklist

  • Code follows project conventions
  • Includes doctests
  • Includes test function
  • Placed in appropriate directory (data_structures/trie/)
  • No external dependencies

Implemented a SpellChecker class that uses a Trie data structure for
efficient dictionary storage and provides spell-checking with
Levenshtein edit distance-based suggestions.

Features:
- Trie-based dictionary storage for efficient prefix lookups
- is_correct() - check if a word exists in dictionary
- suggest() - return closest matches based on edit distance
- add_word() - dynamically add words to dictionary
- Case-insensitive word matching
- Comprehensive test suite included
@algorithms-keeper algorithms-keeper Bot added the require descriptive names This PR needs descriptive function and/or variable names label May 13, 2026
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.


all_words = self.trie.get_all_words()
distances = [(w, self._edit_distance(word, w)) for w in all_words]
distances.sort(key=lambda x: x[1])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: x

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label May 13, 2026
The word 'worl' in the test case was flagged by codespell as a
misspelling of 'world'. Replaced with 'helo' (misspelling of 'hello')
which serves the same purpose without triggering the linter.
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.


all_words = self.trie.get_all_words()
distances = [(w, self._edit_distance(word, w)) for w in all_words]
distances.sort(key=lambda x: x[1])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants