Skip to content

Commit cf98227

Browse files
committed
Merge pull request #251 from behrtam/change-separator-word-count
word-count: Change separator from whitespace to non alphanumeric
2 parents ec0fa19 + 4771b97 commit cf98227

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

word-count/example.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
def word_count(text):
5-
"""Return a Counter object that maps from the words contained in
6-
the phrase to their respective counts
7-
"""
8-
return Counter(text.lower().split())
5+
replace_nonalpha = lambda c: c.lower() if c.isalnum() else ' '
6+
text = ''.join(replace_nonalpha(c) for c in text)
7+
return Counter(text.split())

word-count/word_count_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_count_multiple_occurences(self):
2525

2626
def test_preserves_punctuation(self):
2727
self.assertEqual(
28-
{'car': 1, 'carpet': 1, 'as': 1, 'java': 1, ':': 2, 'javascript!!&@$%^&': 1},
28+
{'car': 1, 'carpet': 1, 'as': 1, 'java': 1, 'javascript': 1},
2929
word_count('car : carpet as java : javascript!!&@$%^&')
3030
)
3131

@@ -63,5 +63,11 @@ def test_tabs(self):
6363
'want your bad romance')
6464
)
6565

66+
def test_non_alphanumeric(self):
67+
self.assertEqual(
68+
{'hey': 1, 'my': 1, 'spacebar': 1, 'is': 1, 'broken': 1},
69+
word_count('hey,my_spacebar_is_broken.')
70+
)
71+
6672
if __name__ == '__main__':
6773
unittest.main()

0 commit comments

Comments
 (0)