File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change 1
1
from collections import Counter
2
2
3
3
4
+ # to be backwards compatible with the old Python 2.X
5
+ def decode_if_needed (string ):
6
+ try :
7
+ return string .decode ('utf-8' )
8
+ except AttributeError :
9
+ return string
10
+
11
+
4
12
def word_count (text ):
5
13
replace_nonalpha = lambda c : c .lower () if c .isalnum () else ' '
6
- text = '' .join (replace_nonalpha (c ) for c in text )
14
+ text = '' .join (replace_nonalpha (c ) for c in decode_if_needed ( text ) )
7
15
return Counter (text .split ())
Original file line number Diff line number Diff line change
1
+ # -*- coding: utf-8 -*-
1
2
import unittest
2
3
3
4
from wordcount import word_count
4
5
5
6
7
+ # to be backwards compatible with the old Python 2.X
8
+ def decode_if_needed (string ):
9
+ try :
10
+ return string .decode ('utf-8' )
11
+ except AttributeError :
12
+ return string
13
+
14
+
6
15
class WordCountTests (unittest .TestCase ):
7
16
8
17
def test_count_one_word (self ):
@@ -69,5 +78,11 @@ def test_non_alphanumeric(self):
69
78
word_count ('hey,my_spacebar_is_broken.' )
70
79
)
71
80
81
+ def test_unicode (self ):
82
+ self .assertEqual (
83
+ {decode_if_needed ('до' ): 1 , decode_if_needed ('свидания' ): 1 },
84
+ word_count ('до🖖свидания!' )
85
+ )
86
+
72
87
if __name__ == '__main__' :
73
88
unittest .main ()
You can’t perform that action at this time.
0 commit comments