Extracting sentences containing a keyword using set()

Viewed 42

I'm trying to extract sentences that contain selected keywords using set.intersection().

So far I'm only getting sentences that have the word 'van'. I can't get sentences with the words 'blue tinge' or 'off the road' because the code below can only handle single keywords.

Why is this happening, and what can I do to solve the problem? Thank you.

from textblob import TextBlob
import nltk
nltk.download('punkt')

search_words = set(["off the road", "blue tinge" ,"van"])

blob = TextBlob("That is the off the road vehicle I had in mind for my adventure. 
Which one? The one with the blue tinge. Oh, I'd use the money for a van.")

matches = []

for sentence in blob.sentences:
    blobwords = set(sentence.words) 
    if search_words.intersection(blobwords):  
        matches.append(str(sentence))

print(matches)

Output: ["Oh, I'd use the money for a van."]
1 Answers

If you want to check for exact match of the search keywords this can be accomplished using:

from nltk.tokenize import sent_tokenize
text = "That is the off the road vehicle I had in mind for my adventure. Which one? The one with the blue tinge. Oh, I'd use the money for a van."
search_words = ["off the road", "blue tinge" ,"van"]
matches = []
sentances = sent_tokenize(text)
for word in search_words:
   for sentance in sentances:
       if word in sentance:
           matches.append(sentance)
print(matches)

The output is:

['That is the off the road vehicle I had in mind for my adventure.',
 "Oh, I'd use the money for a van.",
 'The one with the blue tinge.']

If you want partial matching then use fuzzywuzzy for percentage matching.

Related