Skip to content

word-count: Change separator from whitespace to non alphanumeric #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions word-count/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


def word_count(text):
"""Return a Counter object that maps from the words contained in
the phrase to their respective counts
"""
return Counter(text.lower().split())
replace_nonalpha = lambda c: c.lower() if c.isalnum() else ' '
text = ''.join(replace_nonalpha(c) for c in text)
return Counter(text.split())
8 changes: 7 additions & 1 deletion word-count/word_count_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_count_multiple_occurences(self):

def test_preserves_punctuation(self):
self.assertEqual(
{'car': 1, 'carpet': 1, 'as': 1, 'java': 1, ':': 2, 'javascript!!&@$%^&': 1},
{'car': 1, 'carpet': 1, 'as': 1, 'java': 1, 'javascript': 1},
word_count('car : carpet as java : javascript!!&@$%^&')
)

Expand Down Expand Up @@ -63,5 +63,11 @@ def test_tabs(self):
'want your bad romance')
)

def test_non_alphanumeric(self):
self.assertEqual(
{'hey': 1, 'my': 1, 'spacebar': 1, 'is': 1, 'broken': 1},
word_count('hey,my_spacebar_is_broken.')
)

if __name__ == '__main__':
unittest.main()