开发者

Algorithm for multiple word matching in a text, count the number of every matched word

开发者 https://www.devze.com 2023-01-02 05:10 出处:网络
I have noticed that it has solutions for matching multiple words in a given text, such as below: Algorithm for multiple word matching in text

I have noticed that it has solutions for matching multiple words in a given text, such as below: Algorithm for multiple word matching in text

If I want to开发者_运维百科 know exactly the number of appearances of each matched word in the text, my solution is like this:

step 1: using ac-algorithm to obtain the maching words;

step 2: count the number of each word obtained in step 1

is there a faster way?

Thx~


  1. Put the words you want to search for in a hashtable, with the words as keys and the values initialized to 0.
  2. Iterate over the words of the text, each time checking to see if the word is a key in the hashtable, if so, then increment the value for that key.
  3. Iterate over the hashtable finding the values which are non-zero, the keys for these are your matched words, the values are the counts.

Runs in O(N+M) where N is the number of words you're searching for and M is the number of words you're searching through.

0

精彩评论

暂无评论...
验证码 换一张
取 消