Reputation: 61
I want to find word in sentence to give a category to sentence. For that I have created a function listed below:
def theme(x):
category = ()
for i in x:
if 'AC' in i:
category = 'AC problem'
elif 'insects' in i:
category = 'Cleanliness'
elif 'clean' in i:
category = 'Cleanliness'
elif 'food' in i:
category = 'Food Problem'
elif 'delay' in i:
category = 'Train Delayed'
else:
category = 'None'
print(category)
the output is :
None
None
AC problem
None
AC problem
How should I save this output to a variable
Upvotes: 0
Views: 105
Reputation: 132
def theme(x):
category = []
for i in x:
if 'AC' in i:
category.append('AC problem')
elif 'insects' in i:
category.append('Cleanliness')
elif 'clean' in i:
category.append('Cleanliness')
elif 'food' in i:
category.append('Food Problem')
elif 'delay' in i:
category.append('Train Delayed')
else:
category.append('None')
return category
categories = theme(['bla bla AC bla','bla insects bla'])
print(categories)
Upvotes: 0
Reputation: 404
def theme(x):
output =[]
category = ()
for i in x:
if 'AC' in i:
category = 'AC problem'
elif 'insects' in i:
category = 'Cleanliness'
elif 'clean' in i:
category = 'Cleanliness'
elif 'food' in i:
category = 'Food Problem'
elif 'delay' in i:
category = 'Train Delayed'
else:
category = 'None'
output.append(category)
return output
Upvotes: 2