{"id":27151,"date":"2022-04-06T22:06:30","date_gmt":"2022-04-06T16:36:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=27151"},"modified":"2022-04-06T22:06:30","modified_gmt":"2022-04-06T16:36:30","slug":"how-to-find-the-levenshtein-distance-using-enchant-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-find-the-levenshtein-distance-using-enchant-in-python\/","title":{"rendered":"How to Find the Levenshtein Distance using Enchant in Python?"},"content":{"rendered":"
Enchant Module in Python:<\/strong><\/p>\n Enchant is a Python module that checks a word\u2019s 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.<\/p>\n What is Levenshtein distance?<\/strong><\/p>\n The Levenshtein distance between two strings is the minimum number of characters required to insert, delete or replace in a given string str_1<\/strong> to transform it to another string str_2<\/strong>.<\/p>\n enchant.utils.levenshtein() method:<\/strong><\/p>\n The enchant.utils.levenshtein() method of the enchant module is used to calculate the Levenshtein distance between two strings.<\/p>\n Syntax:<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the Levenshtein distance using enchant in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Enchant Module in Python: Enchant is a Python module that checks a word\u2019s 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 …<\/p>\nenchant.utils.levenshtein(str_1, str_2)<\/pre>\n
Given First String = pqr\u00a0\r\nGiven Second String = pst<\/pre>\n
The Levenshtein distance to convert { pqr } to { pst }:\r\n2<\/pre>\n
To convert given first string { pqr } to { pst } we we should change \r\nq,r to s, t respectively i.e, 2 characters<\/pre>\n
Given First String = helloPythonprograms\u00a0\r\nGiven Second String = hiPythonprograms<\/pre>\n
The Levenshtein distance to convert { helloPythonprograms } to { hiPythonprograms }:\r\n4<\/pre>\n
Finding the Levenshtein Distance using Enchant in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import enchant module using the import keyword\r\nimport enchant\r\n\r\n# Give the first string as static input and store it in a variable.\r\ngvn_str1 = \"pqr\"\r\n# Give the second string as static input and store it in another variable.\r\ngvn_str2 = \"pst\"\r\n\r\n# Calculate the Levenshtein distance (minimum characters required to convert the first string \r\n# to second string) between the given two strings using the levenshtein() function\r\n# by passing given first and second strings as arguments to it and print it.\r\nprint(\"The Levenshtein distance to convert {\",gvn_str1,\"} to {\",gvn_str2,\"}:\")\r\nprint(enchant.utils.levenshtein(gvn_str1, gvn_str2))<\/pre>\n
The Levenshtein distance to convert { pqr } to { pst }:\r\n2<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import enchant module using the import keyword\r\nimport enchant\r\n\r\n# Give the first string as user input using the input() function and store it in a variable.\r\ngvn_str1 = input(\"Enter some random first string = \")\r\n# Give the second string as user input using the input() function and store it in another variable.\r\ngvn_str2 = input(\"Enter some random second string = \")\r\n\r\n# Calculate the Levenshtein distance (minimum characters required to convert the first string \r\n# to the second string) between the given two strings using the levenshtein() function\r\n# by passing given first and second strings as arguments to it and print it.\r\nprint(\"The Levenshtein distance to convert {\",gvn_str1,\"} to {\",gvn_str2,\"}:\")\r\nprint(enchant.utils.levenshtein(gvn_str1, gvn_str2))<\/pre>\n
Enter some random first string = helloPythonprograms\r\nEnter some random second string = hiPythonprograms\r\nThe Levenshtein distance to convert { helloPythonprograms } to { hiPythonprograms }:\r\n4<\/pre>\n