Skip to content

Instantly share code, notes, and snippets.

@dat-boris
Last active January 31, 2017 04:24
Show Gist options
  • Select an option

  • Save dat-boris/baaca9686fc3573f222b7e0a2987d47d to your computer and use it in GitHub Desktop.

Select an option

Save dat-boris/baaca9686fc3573f222b7e0a2987d47d to your computer and use it in GitHub Desktop.
A brief demo of a word counter
import re
from collections import Counter
RE_CHAR = re.compile('\w')
def count_better_parser(stream):
counter = Counter()
word = ''
for i,char in enumerate(iter(lambda: stream.read(1), '')):
if char is None:
break
if not RE_CHAR.match(char):
if word:
counter[word] += 1
word = ''
else:
word += char
if word:
counter[word] += 1
return dict(counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment