How to Find the Levenshtein Distance using Enchant in Python?

Enchant Module in Python:

Enchant is a Python module that checks a word’s spelling and provides suggestions for correcting it. The antonyms and synonyms of the words are also provided. It determines whether or not a word is in the dictionary.

What is Levenshtein distance?

The Levenshtein distance between two strings is the minimum number of characters required to insert, delete or replace in a given string str_1 to transform it to another string str_2.

enchant.utils.levenshtein() method:

The enchant.utils.levenshtein() method of the enchant module is used to calculate the Levenshtein distance between two strings.

Syntax:

enchant.utils.levenshtein(str_1, str_2)

Examples:

Example1:

Input:

Given First String = pqr 
Given Second String = pst

Output:

The Levenshtein distance to convert { pqr } to { pst }:
2

Explanation:

To convert given first string { pqr } to { pst } we we should change 
q,r to s, t respectively i.e, 2 characters

Example2:

Input:

Given First String = helloPythonprograms 
Given Second String = hiPythonprograms

Output:

The Levenshtein distance to convert { helloPythonprograms } to { hiPythonprograms }:
4

Finding the Levenshtein Distance using Enchant in Python

Below are the ways to find the Levenshtein distance using enchant in Python:

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import enchant module using the import keyword
  • Give the first string as static input and store it in a variable.
  • Give the second string as static input and store it in another variable.
  • Calculate the Levenshtein distance (minimum characters required to convert the first string to the second string) between the given two strings using the levenshtein() function by passing the given first and second strings as arguments to it and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import enchant module using the import keyword
import enchant

# Give the first string as static input and store it in a variable.
gvn_str1 = "pqr"
# Give the second string as static input and store it in another variable.
gvn_str2 = "pst"

# Calculate the Levenshtein distance (minimum characters required to convert the first string 
# to second string) between the given two strings using the levenshtein() function
# by passing given first and second strings as arguments to it and print it.
print("The Levenshtein distance to convert {",gvn_str1,"} to {",gvn_str2,"}:")
print(enchant.utils.levenshtein(gvn_str1, gvn_str2))

Output:

The Levenshtein distance to convert { pqr } to { pst }:
2

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import enchant module using the import keyword
  • Give the first string as user input using the input() function and store it in a variable.
  • Give the second string as static input and store it in another variable.
  • Calculate the Levenshtein distance (minimum characters required to convert the first string to the second string) between the given two strings using the levenshtein() function by passing the given first and second strings as arguments to it and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import enchant module using the import keyword
import enchant

# Give the first string as user input using the input() function and store it in a variable.
gvn_str1 = input("Enter some random first string = ")
# Give the second string as user input using the input() function and store it in another variable.
gvn_str2 = input("Enter some random second string = ")

# Calculate the Levenshtein distance (minimum characters required to convert the first string 
# to the second string) between the given two strings using the levenshtein() function
# by passing given first and second strings as arguments to it and print it.
print("The Levenshtein distance to convert {",gvn_str1,"} to {",gvn_str2,"}:")
print(enchant.utils.levenshtein(gvn_str1, gvn_str2))

Output:

Enter some random first string = helloPythonprograms
Enter some random second string = hiPythonprograms
The Levenshtein distance to convert { helloPythonprograms } to { hiPythonprograms }:
4