neg=['no','not',"don't",'have no interest in']
verb=['like','love','want']
senten=['I love apples.', 'I do not like grapes', 'I have no interest in wanting bananas.','No love for cars']
I would like to know if my senten is positive or negative. If the string in senten contains only the verb, then print "1". but if neg comes before verb, I will give it a "-1".
Example:
like +1
love +1
want +1
no like -1
not want -1
don't love -1
Code:
for j in senten:
j=j.lower()
if any(x in j for x in verb):
first_match = list(filter(lambda x: x in j, verb))[0]
loca=j.find(first_match) #location of the first word
#the sentence might be too short.
if int(loca)<=3:
if j[int(loca)-3:int(loca)-1]== "no":
print(j)
print(-1)
continue
elif int(loca)<=4:
if j[int(loca)-3:int(loca)-1]== "no" or j[int(loca)-4:int(loca)-1]== "not":
print(j)
print(-1)
continue
elif int(loca)<=6:
if j[int(loca)-3:int(loca)-1]== "no" or j[int(loca)-4:int(loca)-1]== "not" or j[int(loca)-6:int(loca)-1]== "don't":
print(j)
print(-1)
continue
elif int(loca)<=22:
if j[int(loca)-3:int(loca)-1]== "no" or j[int(loca)-4:int(loca)-1]== "not" or j[int(loca)-6:int(loca)-1]== "don't" or j[int(loca)-20:int(loca)-1]== "have no interest in":
print(j)
print(-1)
continue
print(j)
print(1)
My code has no error and it prints:
i love apples.
1
i do not like grapes
-1
i have no interest in wanting bananas.
-1
no love for cars
-1
But I would like to ask for an advice if there's a better way to write it? Cause I will add more words in neg, I need to do lots of if or statement and it might not be efficient. Thanks :)
Aucun commentaire:
Enregistrer un commentaire