Reputation: 97
Lets say keywords1.txt contains the following:
Broken Screen
Then I write this program:
sentence = input("Input your sentence: ")
if open('keywords1.txt').read() in sentence:
print("hello there")
I wanted it to display 'hello there' whenever i say for example: my screen is broken
But it doesn't work. Putting those wods in a textfile as a list still doesn't work:
Broken
Screen
Upvotes: 1
Views: 139
Reputation: 692
You can use the set
method issubset
to do that:
sentence = input("Input your sentence: ")
A = open('keywords1.txt').read().split('\n') # or any other separator
B = sentence.split()
A, B = set(A), set(B)
if A.issubset(B) :
print("hello there")
Upvotes: 0
Reputation: 1397
Here is a basic algorithm for that. Maybe there is some function in python to make it easy, but this is the most basic code.
sentence = input("Input your sentence: ")
findCount = 0
lines = 0
fLines = open('keywords1.txt').readlines()
for line in fLines:
lines += 1
if line in sentence:
findCount += 1
if lines == findCount:
print("hello there")
Upvotes: 1