efficient merging on substrings (long lists of short terms) : I have a list of terms that need to be categorized according to a dictionary table.  The term list and the dictionary are both hundreds of thousands of lines long.  

The terms will typically not match a keyword in the dictionary exactly; the keywords will typically be a **substring** of the terms.

*I* ***could*** *loop through the dictionary keywords and then grep for the presence of the current keyword in all list terms.*

**What approaches would be more efficient than that?  My preferred language is R, followed by Python.**

For the terms and dictionary below, I would want a result like this:

    bean soup		legume
    cheese omelette	dairy
    cheese omelette	eggs
    turkey sandwich	meat
    turkey sandwich	sandwich

Input terms:

    bean soup
    cheese omelette
    turkey sandwich


Dictionary columns:  keyword, category

    bean		legume
    beef		meat
    carrot		vegetable
    cheese		dairy
    ice cream	dairy
    milk		dairy
    omelette	eggs
    sandwich	bread
    turkey		meat


I have seen many good SO posts about effienct substring searching, but they seem more focused on searchign for short needles in long haystacks.