Identifying Misspelled Words in your Dataset with Hunspell
3 minute reading time
This article is based on one written by Markus Konrad at this link https://datascience.blog.wzb.eu/2016/07/13/autocorrecting-misspelled-words-in-python-using-hunspell/
I assume in this article that you have hunspell and it’s integration with python installed. If not, please refer to the article mention above and follow the prerequisite steps.
This article is inspired from the need to correct misspelled words in the Dress Attributes Dataset. I’ll share with you my initial pitfall, and what I ended up doing instead.
Misspelled words are common when dealing with survey data or data where humans type in the responses manually. In the Dress Attributes Dataset this is apparent when looking at the sleeve lengths of the different dresses.
Ouch, so many misspelled words. This is when my brain is racking up all the ways I can automate this problem away. Hence my stumbling upon Markus’ post.
Automagically Correcting Data
First, I decided to completely ignore what Markus warns in his post and automatically correct all the words in that column.
To begin the code, let’s import and create an instance of the spellchecker:
I modified his
correct_words function so that it only corrects one word and so I can
apply it along the
Now let’s apply the function over the
SleeveLength column of the dataset:
Doing so creates the following series:
As you might be able to tell, this process didn’t go as intended.
landownership isn’t even a length of a sleeve!
Reporting Misspelled Items and Allowing User Intervention
This is when I have to remember, technology isn’t perfect. Instead we should rely on ourselves to identify what the word should be correctly spelled as.
Keeping that in mind, I modified the function again to take in a list of the data, and return a dictionary that has the misspelled words as the keys and suggestions as the values represented as a list.
With that, I can use the function on my data. To do so, I convert the pandas values to a list and pass it to the function:
These are the suggestions it produces:
From here, you can analyze the output and do the replacements yourself:
What’s the Benefit?
This is where you ask “What’s the difference if it doesn’t automatically fix my data?”
When you have large datasets, it can be hard to individually identify which items are misspelled. Using this method will allow you to have a list of all the items that are misspelled which can let you deal with it in a systematic way.