Last active
January 31, 2017 04:24
-
-
Save dat-boris/baaca9686fc3573f222b7e0a2987d47d to your computer and use it in GitHub Desktop.
A brief demo of a word counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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