2

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?

semiflex
  • 1,176
  • 3
  • 25
  • 44
  • Probably not related to your problem, but does `for row in rows: terms = row[1]` really do what you intended? Shouldn't that be `terms+= row` or `terms.append(row[1])`? – Aran-Fey Jul 13 '16 at 21:07
  • Yep. It pulls everything from the column "terms" – semiflex Jul 13 '16 at 21:09

4 Answers4

2

Set operations are the way to go here:

def matcher(keywords, searcher):
    for sentence in searcher:
        sentence= set(sentence.split()) # split sentence into words
        for key,words in keywords.iteritems():
            if sentence & words: # if these two sets of words intersect, output the key
                print key


keyword_dictionary = {
    'Animals' : {'animal', 'dog', 'cat'},
    'Art' : {'art', 'sculpture', 'fearns','graphic','display','lights'},
    'Fruit' : {'yellow','fruit'},
}

matcher(keyword_dictionary, ["wendy bought a dog","that's a red fruit"])
# output: Animals
#         Fruit
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
1

I don't know what the pyodbc or cursor part of this code is, but I have tried running the following and it works. I'm not sure if it's exactly what you want though as it's returning the answers in a list and I might have miss interpreted the form of the "terms" input.

import re

keyword_dictionary = {
    'Animals' : {'animal', 'dog', 'cat'},
    'Art' : {'art', 'sculpture', 'fearns','graphic','display','lights'},
    'Fruit' : {'yellow','fruit'},
}
def matcher(keywords, terms):
    final_results = []
    for term in terms:
        for word in term.split(" "):
            for key, words in keywords.items():
                if word in words:
                    result = []
                    result.append(key)
                    result1 = str(result).replace("['", "")
                    final_results.append(str(result1).replace("']", ""))
    return final_results

terms = ["wendy bought a dog","that's a red fruit"]
results = matcher(keyword_dictionary, terms)
print results

yielding:

['Animals', 'Fruit']
mitoRibo
  • 4,468
  • 1
  • 13
  • 22
1

Your code splits the searcher into single letters, you need to split by spaces. The rest seems OK.

Example:

searcher = 'wendy bought a dog'

# result: single letters
for word in searcher:
    print word

# result: words
for word in searcher.split(' '):
    print word
576i
  • 7,579
  • 12
  • 55
  • 92
1

I'd think the easiest way to solve this is to reverse the keys and values of keyword_dictionary and perform a search on every word.

I put a simple example of this on repl.it.

keyword_dictionary = {
    'Animals' : {'animal', 'dog', 'cat'},
    'Art' : {'art', 'sculpture', 'fearns','graphic','display','lights'},
    'Fruit' : {'yellow','fruit'},
}
#Swap the keys and values
keywords = dict(pair for key in keyword_dictionary.keys() for pair in dict.fromkeys(keyword_dictionary[key], key).items())

def keyword(sentence):
    for word in set(map(str.lower, sentence.split())):
        if word in keywords:
            return keywords[word]
    return None

print keyword("Wendy bought a dog")   #Returns "Animal"
print keyword("that's a red fruit")   #Returns "Fruit"
Community
  • 1
  • 1
Sunny Patel
  • 7,830
  • 2
  • 31
  • 46