简体   繁体   中英

lookup/map multiple keys in a dictionary and add to a list

My current code:

from nltk.tag import pos_tag, map_tag
search_term = 'quaker lemon banana oatmeal'
lst = []
search_term_words = search_term.split()
for w in search_term_words:
  if w in flavor_grocer_mapping:
      for flavor in flavor_grocer_mapping[w]:
          if flavor in search_term:
              lst.append((flavor, 'FLAVOR'))
              for x in search_term.replace(flavor, '').split(): 
                  if x in brand_grocer_mapping:
                      for brand in brand_grocer_mapping[x]:
                          if brand in search_term.replace(flavor, '').split():
                              lst.append((brand, 'BRAND'))
                              for word, tag in pos_tag(word_tokenize(search_term.replace(flavor, '').replace(brand, '').strip())):
                                  lst.append((word, map_tag('en-ptb', 'universal', tag)))

The result I got:

[('lemon', 'FLAVOR'), 
 ('quaker', 'BRAND'),
 ('banana', 'NOUN'), 
 ('oatmeal', 'NOUN'),
 ('banana', 'FLAVOR'),
 ('quaker', 'BRAND'),
 ('lemon', 'ADJ'),
 ('oatmeal', 'NOUN')]

My expected result is:

[('lemon', 'FLAVOR'),
 ('banana', 'FLAVOR'),
 ('quaker', 'BRAND'),
 ('oatmeal', 'NOUN')]

I know the problem is that the splitted words in search terms run recursively in the for loops. How can lookup/map a whole string that contains multiple keys in a dictionary? (For example, lemon and banana are in the search term string and they are the keys in the flavor_grocer_mapping dictionary.)

Well, I tried to recreate flavor_grocer_mapping based on your results, your additional code to lookup is highly unnecessary. Instead, since you have a dictionary that corresponds to search_term_words as keys, do a proper dictionary lookup in a list comprehension.

search_term = 'quaker lemon banana oatmeal'
search_term_words = search_term.split()

#recreating your dictionary
flavor_grocer_mapping = [('lemon', 'FLAVOR'),
                         ('banana', 'FLAVOR'),
                         ('quaker', 'BRAND'),
                         ('oatmeal', 'NOUN')]
flavor_grocer_mapping = {k:v for (k,v) in flavor_grocer_mapping} 

#solution
results = [(word,flavor_grocer_mapping[word]) for word in search_term_words]
results
>>[('quaker', 'BRAND'),
 ('lemon', 'FLAVOR'),
 ('banana', 'FLAVOR'),
 ('oatmeal', 'NOUN')]

In the case of your provided example, I suggest your merge all the dictionaries to 1 instead of repeatedly looping over them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM