EDIT : If I had to match single worded phrases, I could first tokenize the text from the document and then calculate the cosine similarity of all the tokens with all the keywords from the keyword_list. But the issue is that I might have single worded or multi worded keyphrases present in the keyword_list. Even if I try to use ngrams, how would I know what length of ngrams to use?

I have searched and read many articles/questions regarding this but could not find a solution.

Problem Statement : I am trying to extract similar keywords/phrases from a document, based on a pre-curated list of keywords/phrases.

For example below is the list:

keyword_list = ['your work', 'ongoing operations', 'completed operations', 'your name', 'bodily injury', 'property damage', 
     'to the extent permitted by law', 'is required by a contract or agreement']

I also have the text I extracted from the documents using OCR. Let's say the text is as below:

text = "In light of your ongoing operations, your name is an approximation of your working models. The contract requires that the damage done to the property must be borne by both the parties, as permitted by the law." 

Now I want to extract all the keywords/phrases that occur in the keyword_list. In addition to that I also want to extract similar keyphrases (by similar I mean similar in context or meaning but worded differently). So the logic/model should be able to extract the following terms:

output = ["ongoing operations", "your name", "your working", "The contract requires", "damage done to the property", "as permitted by the law"]

We can see that ongoing operations and your name are present in the keyword_list and hence are extracted.

But your working, The contract requires, damage done to the property, as permitted by the law are also extracted because they have the same meaning/context to your work, is required by a contract or agreement, property damage, to the extent permitted by law.

For the phrases matching completely (ongoing operations and your name), I have written a logic which uses regex to match the phrases. But for the phrases which have the same meaning/context but worded differently, I am unsure how to proceed. I think a Machine learning or Deep learning approach would be suitable here but I don't know which exact approach!

Any help is appreciated!


1 Answer 1


You could use sentence transformer library to calculate the similarity between different phrases. It also works for multi worded tokens.

from sentence_transformers import SentenceTransformer, util
import compress_fasttext
import numpy as np

mpnet_v2 = SentenceTransformer('all-mpnet-base-v2')

sentence1 = "property damage"
sentence2 = "damage done to the property"

# encode sentences to get their embeddings
embeb_r_large1 = mpnet_v2.encode(sentence1, convert_to_tensor=True)

# compute similarity scores of two embeddings
mpnetv2_score = util.pytorch_cos_sim(embeb_r_large1, embeb_r_large2)

print(f'similarity score is : {mpnetv2_score}')

#similarity score is : 0.8635872602462769
  • 1
    $\begingroup$ But how would I decide what ngram_range to select from the document? The document has multiple sentences. $\endgroup$
    – spectre
    May 27, 2023 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.