I have a dictionary with a criteria and I want to return one key if any one of the terms matches with the criteria. Here's what my code looks like so far:
import re
import pyodbc
keyword_dictionary = {
'Animals' : {'animal', 'dog', 'cat'},
'Art' : {'art', 'sculpture', 'fearns','graphic','display','lights'},
'Fruit' : {'yellow','fruit'},
}
def matcher(keywords, searcher):
for word in searcher:
for key, words in keywords.items():
if word in words:
result = []
result.append(key)
result1 = str(result).replace("['", "")
final_result = str(result1).replace("']", "")
print final_result
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=INSERT SERVER NAME;DATABASE=INSERT DATABASE NAME;UID=INSERT USERNAME;PWD=INSERT PASSWORD')
cursor = cnxn.cursor()
cursor.execute("SELECT TOP 50 [unique_id] \
,[terms] \
,[url] \
FROM [INSERT DATABASE NAME].[dbo].[INSERT TABLE NAME]")
rows = cursor.fetchall()
for row in rows:
terms = row[1]
matcher(keyword_dictionary, terms)
A term may be something like
"wendy bought a dog"
which should print the key Animals
or
"that's a red fruit"
which should print the key Fruit
Does anyone know how I would go about editing my code so it does this?