/
test_text_stats.py
104 lines (83 loc) · 3.22 KB
/
test_text_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import unittest
import text_stats
class simple_unit_tests(unittest.TestCase):
'''
Here go the unit test for the text_stats class
'''
def test_empty_string(self):
'''
When an empty string is passed, an empty dictionary should
be returned
'''
word_stats = text_stats.text_stats()
word_stats.extract_stats("")
self.assertEqual(len(word_stats.freq_table), 0)
def test_one_word_string(self):
'''
Test with input of 1 word
Please note that all of the input words are turned to lowercase
'''
word_stats = text_stats.text_stats()
word_stats.extract_stats("Hello")
self.assertEqual(word_stats.freq_table, dict(hello = 1))
def test_removal_of_dots(self):
'''
Test whether dots are removed
'''
word_stats = text_stats.text_stats()
word_stats.extract_stats('Hello. There.')
self.assertEqual(word_stats.freq_table, dict(hello = 1, there = 1))
def test_append_to_existing(self):
'''
Test whether more results can be appended after processing a string
'''
word_stats = text_stats.text_stats()
word_stats.extract_stats('Hello')
word_stats.extract_stats('hello')
self.assertEqual(word_stats.freq_table, dict(hello = 2))
def test_add_excluded_words(self):
'''
Test whether adding a new excluded word works internally
'''
word_stats = text_stats.text_stats()
word_stats.add_excluded_words(['one','two','three'])
self.assertEqual(word_stats.excluded_words, set(['one','two','three']) )
def test_append_excluded_words(self):
'''
Test whether extending the excluded word set works appropriately
'''
word_stats = text_stats.text_stats()
word_stats.add_excluded_words(['one','two','three'])
word_stats.add_excluded_words(['four','five','six'])
self.assertEqual(word_stats.excluded_words,
set(['one','two','three','four','five','six']) )
def test_excluded_word_case(self):
'''
Test whether the excluded words are converted into lowercase
'''
word_stats = text_stats.text_stats()
word_stats.add_excluded_words(['One','Two','ThrEe'])
self.assertEqual(word_stats.excluded_words, set(['one','two','three']) )
def test_exclusion(self):
'''
Test whether excluding a word works
'''
word_stats = text_stats.text_stats()
word_stats.extract_stats('Hello there', ['there'])
self.assertEqual(word_stats.freq_table, dict(hello=1))
def test_word_count(self):
'''
Test whether the word count is correct
'''
word_stats = text_stats.text_stats()
word_stats.extract_stats('Hello there this is a long sentence')
self.assertEqual(word_stats.get_total_words(), 7)
def test_unique_word_count(self):
'''
Test whether the unique word count is correct
'''
word_stats = text_stats.text_stats()
word_stats.extract_stats('Hello hello hello')
self.assertEqual(word_stats.get_unique_word_count(), 1)
if __name__ == '__main__':
unittest.main()