I have a list of keywords and another longer string(2 or 3 pages). I want to figure out keywords which are present in list of keywords. e.g
Keywords = [k1, k2, k3 k4, k5, k6 k7 k8]
paragraphs = "This will be 2 to4 page article"
one simple way will be
present_keywords = [x for x in keywords if x in paragraphs]
Time complexity of above algorithm will be O(m*n) =~ O(n^2)
Another way
I can create Heap of keywords list, time complexity: O(n log n)
Then search for each word from paragraph in the Heap, time complexity will be O(n).
Note: The keywords are bi-grams, tri-grams as well so second approach will not work.
What will be an efficient way to achieve this?
Some of the keywords are n-grams
Many People have given solution without considering this constrain. e.g New York is one keyword. Splitting paragraph will split New and York as different word. Have Mentioned this in Note above as well.