Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

python - How do I find a list of words in a sentence where each word might be separated by n other words

I have a list of words i.e. "please find cat"
and I have a sentence "can you please find my pretty cat"
and I have variable indicating the number of words that can be between the "list" of words that determine if this is a match.

For this example, if the fuzziness value between the words was 2 or more, the answer would be true. If it was 1 or less then the answer would be false.

The 'fuzziness' variable can be used between any word. With a fuzzy value of 2 "please hello bob find my pretty cat". This would be true

The order matters, if the sentence was "find please my pretty cat". That would be false.

I'm making the presumption that there is a library in Python that I can uses. If not, then I was looking at regular expressions. Neither of which I've been able to find. Everything I've seen returns true if all the words are in the sentence, regardless of the gap between them.

question from:https://stackoverflow.com/questions/65952551/how-do-i-find-a-list-of-words-in-a-sentence-where-each-word-might-be-separated-b

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I made a small script, I hope I understood your problem:

import numpy as np

def check(text, sentence, n):
    index = []
    for word in sentence.split(" "):
        if word in text.split(" "):
            i = sentence.split(" ").index(word)
            print("Word '"+word+"' found in sentence in position "+str(i+1)+".")
            index.append(i+1)
            #i+1 to make it more understandable
    #print(index)
    #print(np.diff(index))
    if any(t>(n+1) for t in np.diff(index)):
        # If there are n words between the words we are looking for, then the difference between words position should be n+1
        # E.g. First word is in position 2 and second is in position 6, there are 3 words between and 6-3=4=(3+1)
        print("No match found")
        return False
    else:
        print("Match found")
        return True

Examples:

text = "please find cat"
sentence = "can you please find my pretty cat"
n = 2
check(text,sentence,n) #True

n = 1
check(text,sentence,n) #False

n = 3 
check(text,sentence,n) #True
text = "please find cat"
sentence = "can you please find my pretty little cat"
n = 2
check(text,sentence,n) #False

n = 3
check(text,sentence,n) #True

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...