python - Extract topic keywords from text -


i'm trying extract list of ingredients cooking recipe. made list of many ingredients in file, check these ingredients against recipe. code looks this:

ingredients = ['sugar', 'flour', 'apple'] found = [] recipe = ''' 1 teaspoon of sugar 2 tablespoons of flour. 3 apples ''' ingredient in ingredients:     if ingredient in recipe:          found.append(ingredient) 

i'm looking more efficient way because list of possible ingredients can big. ideas?

you split input , use sets:

ingredients = set(['sugar', 'flour', 'apple'])     recipe_elements = set([i.strip() in recipe.split(' ')]) used_ingredients = ingredients & recipe_elements    # intersection 

you may need various clean ups on input depending on though. you'd need benchmark see whether better though, , wouldn't match 'apple' user entered 'apples' in example without work (make singular example).


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -