Word Similarity Calculation with Lemmatization

  • Share this:

Code introduction


This function calculates the similarity between two words. First, it uses the WordNetLemmatizer from the Lingua library to lemmatize the words, and then it uses the word_similarity function to calculate the similarity between the lemmatized words.


Technology Stack : Lingua, WordNetLemmatizer, word_similarity

Code Type : Function

Code Difficulty : Intermediate


                
                    
def word_similarity(word1, word2):
    from lingua import WordNetLemmatizer
    from lingua.similarity import word_similarity

    # Initialize the lemmatizer
    lemmatizer = WordNetLemmatizer()

    # Lemmatize the words
    lemma1 = lemmatizer.lemmatize(word1)
    lemma2 = lemmatizer.lemmatize(word2)

    # Calculate the similarity between the lemmatized words
    similarity = word_similarity(lemma1, lemma2)

    return similarity