How to Build an Autocorrect in Python

Example of how to build an Autocorrect in Python by taking the vocabulary from a corpus

George Pipis
The Startup

--

Image by Author

Description

We assume that you are familiar with the concepts of String Distance and String Similarities. You can also have a look at the Spelling Recommender. We will show how you can easily build a simple Autocorrect tool in Python with a few lines of code. What you will need is a corpus to build your vocabulary and the word frequencies. The idea is the following:

  • You enter a word, if this is word exists in the vocabulary then we assume that is correct.
  • If this word does not exist in the vocabulary we try to find the most similar words ordered by their frequency probability.

Build the Vocabulary

We will work with the Moby Dick book. Let’s start.

import pandas as pd
import numpy as np
import textdistance
import re
from collections import Counter
words = []

--

--

George Pipis
The Startup

Sr. Director, Data Scientist @ Persado | Co-founder of the Data Science blog: https://predictivehacks.com/