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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…