2016-07-13 7 views
2

条件付きの辞書があり、その条件のいずれかが基準と一致する場合、1つのキーを返したいと思います。ここに私のコードは、これまでのようになります。用語と戻りキーを使用して辞書条件を検索する

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) 

用語は

キー Animals

または

印刷する必要があります
"that's a red fruit" 

を印刷する必要があります

"wendy bought a dog" 

のようなものかもしれキーFruit

誰かが私のコードを編集する方法を知っていますか?

+0

おそらくあなたの問題には関係ありませんが、 '行内の行に対して:terms = row [1]'はあなたが意図したとおりの動作をしますか?それは 'terms + = row'か' terms.append(row [1]) 'ではないでしょうか? –

+0

それは列 "用語"からすべてを引き出します – semiflex

答えて

2

Set操作は、ここに行くための方法です:

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 
+0

これは素晴らしいです!私は完全に交差を忘れてしまった。 –

+0

恐ろしいアポウム。それはうまくいった! – semiflex

1

私はこのコードのpyodbcまたはカーソル部分が何であるかはわかりませんが、私は以下を実行しようとしています。私はそれがあなたが望むものであるかどうか確信が持てません。なぜなら、答えをリストに返すので、「用語」入力の形を解釈していないかもしれません。

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 

得:あなたは、スペースで分割する必要があり

['Animals', 'Fruit'] 
1

あなたのコードは、単一の文字に探索を分割します。 残りはOKと思われます。

例:

searcher = 'wendy bought a dog' 

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

# result: words 
for word in searcher.split(' '): 
    print word 
1

私はこの問題を解決する最も簡単な方法は、keyword_dictionaryreverse the keys and valuesにあると思い、すべての検索を実行したいですワード。

この簡単な例は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" 
関連する問題