NLTK edit_distance : How to Implement in Python ?

Nltk Edit_Distance featured image

NLTK edit_distance is a function which computes the distance between strings. It returns the minimum number of operation to match the source string to the target string.

NLTK edit_distance Python Implementation –

Let’s see the syntax then we will follow some examples with detail explanation.

distance=nltk.edit_distance(source_string, target_string)

Here we have seen that it returns the distance between two strings. The distance is the minimum number of operation to convert the source string to the target string.

(NLTK edit_distance) Example 1:

Let’s define two strings –

source = ‘Data Science Learner’
target = ‘Data Science Learners’

Here both the string source and target are almost same. The only difference is the last extra character “s”. Now lets run the below code and check the output.

import nltk
# string decleration
source = 'Data Science Learner'
target = 'Data Science Learners'
#distance calculation
distance=nltk.edit_distance(source , target )
print(distance)
edit_distance python
edit_distance python

NLTK edit_distance ( Example 2) :

In this section, We will try to find the distance between two string which are has difference in the mid. In the earlier example,  string was only different in the last.

Now calculate the edit_distance between two string using python.

 

import nltk
#string decleration
source='Data Science Learner'
target='Data Learner'
#distance calculation
distance=nltk.edit_distance(source,target)
print(distance)
edit_distance example
edit_distance example

 

Here we are getting the distance as 9. Because 7 letters ( Science) are different in the mid of target string ( Data Learner).  Also two spaces are missing. Which again adds two operations. Hence we got 9 as final distance between these two strings in using edit_distance NLTK.

edit_distance() Usecases-

Edit_distance solves so many NLP usecases  . Specially the similarity analysis of documents with queries. It also helps in making clusters of similar documents. We can also use the edit_distance function in text Recommender like news Recommender etc. But make sure it does not capture semantic meaning of words. It represents the symbolic similarity.

End Notes-

I hope this article must have cleared your concepts on edit_distance() function. If you have doubt over this topic. Please comment below in comment box.

 

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner