Skip to content
Advertisement

How to write a text parser logic which identifies keywords from a dictionary?

How to make a simple text parser that finds keywords and categorizes them accordingly.

Example: I have two dictionaries

JavaScript

Now I want to pick out if the keywords from A are present in B and the result should be something like this.

JavaScript

I’m a beginner and the logic to get this is very confusing.

Advertisement

Answer

You can do this with a dict comprehension:

JavaScript

This makes every value in the dict a list rather than some being collections and others strings. That’s almost certainly going to be easier to work with than a mixed type dictionary.

If your dicts are going to be large, you might pick up some performance by inverting the B dictionary so you don’t need to scan through each value every time.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement