title: Count number of occurrences of a given list of strings in a data stream
body: <p>I have a stream of data something like, </p>
  
  <pre><code>stream = "carracecowtenhihellocohiwcar ......"
  </code></pre>
  
  <p>and i have to get no. of occurrence of all words in the list from the stream </p>
  
  <pre><code>words = ["car", "cow", "hi", ....]
  </code></pre>
  
  <p>So, result would be something like </p>
  
  <pre><code>result = {
    "car": 2,
    "cow": 1,
    "hi": 2,
    ....
    ....
  }
  </code></pre>
  
  <p>with my current implementation, I am iterating through the list of words and add them to dict as below,</p>
  
  <p>I am looking for some better way to do it, as list of words keep on increasing and data from stream in continuous.</p>
  
  <p>This is what I have currently,</p>
  
  <pre><code>import re
  def word_count(stream_obj):
  
      mydict = {}
      words = ["car", "cow", "hi", "hello"]
      max_word_len = len(max(words, key=len))
      regex = re.compile("|".join(words))
      last_chunk_remainder = ""
  
      while(stream_obj.getchunk() is not None):
          stream_data = last_chunk_remainder + stream_obj.getchunk()
          for word in words:
              mydict[word] = stream_data.count(word)
  
          # to handle the corner case like if the stream chunk ends with
          # “ca” and first letter of next is "r", so that make the word
          # match words in the list, which is "car"
          if not regex.findall(stream_data[-max_word_len:]):
              last_chunk_remainder = stream_data[-max_word_len:]
  </code></pre>
  
  <p>Thanks</p>