Search Engine in 100 Lines of Code

Let's build on the previous example and create an actual search engine. We can use parse actions to compile the search string into an intermediate data structure, and then use that structure to generate and evaluate Python sets to extract matching items by keyword.

The example data set this time will not be all the wooden, steel, or iron things in the universe that are red, blue, or lime green, but a collection of recipes with their respective ingredients.

These will be defined using simple Python lists:

recipes = "Tuna casserole/Hawaiian pizza/Chicken a la King/"\
    "Pepperoni pizza/Baked ham/Tuna melt/Eggs Benedict"\
ingredients = "eggs/pineapple/pizza crust/pepperoni/ham/bacon/"\
    "English muffins/noodles/tuna/cream of mushroom soup/chicken/"\
    "mixed vegetables/cheese/tomato sauce/mayonnaise/Hollandaise sauce"\

The contents of each recipe will be defined using a list of tuples, each tuple mapping the index of a recipe to the index of one of its ingredients:

recipe_ingredients_map = [

So, recipe 0 ("Tuna casserole") contains ingredients 8, 9, and 7 ("tuna," "cream of mushroom soup," and "noodles"). Not exactly the Cordon Bleu, but enough to get us started.

Our search engine will focus on finding recipes that contain given ingredients, so we'll define a Python dict named ...

Get Getting Started with Pyparsing now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.