-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_student_wordcount.py
171 lines (147 loc) · 6.26 KB
/
test_student_wordcount.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
@@ CS 261 Assignment 5 - HashMap SpellChecker Tester
@@ Solution Test Script
@author: Martin Edmunds
"""
"""Change this import depending on spellchecker file name"""
from word_count import top_words
from gradescope_utils.autograder_utils.decorators import weight
import unittest
import re
rgx = re.compile("(\w[\w']*\w|\w)")
source = "alice.txt"
# Helper function that returns a (key, value) sorted list of (words, count)
# Found from a source file.
# Args:
# source - the text file to search
# number - number of 2-tuple results to return
# Return:
# (key, value) sorted (highest values first) list
def get_top_words_standard(source, number):
standard_map = {}
with open(source) as f:
for line in f:
words = rgx.findall(line)
for w in words:
lw = w.lower()
if lw in standard_map:
standard_map[lw] = standard_map[lw] + 1
else:
standard_map[lw] = 1
totals = []
keys = standard_map.keys()
for key in keys:
# get value, key pair
totals.append((standard_map[key], key))
# totals = (value, key) list
# sort by value
totals = sorted(totals, reverse=True)
for i in range(len(totals)):
totals[i] = (totals[i][1], totals[i][0])
# totals = (key, value) sorted by value
return totals[:number]
# Class for testing the student spell checker, checks against the standardized Python Dictionary
class TestStudentWords(unittest.TestCase):
@weight(15)
def test_top_words_1(self):
"""Tests that the top word returned matches the standard results"""
num_of_results = 1
standard_results = get_top_words_standard(source, num_of_results)
results = top_words(source, num_of_results)
words = []
standard_words = []
for key, value in standard_results:
standard_words.append(key)
for key, value in results:
words.append(key)
# check that the student hash map returns the exact list as the python dict
self.assertTrue(words == standard_words)
@weight(6)
def test_top_words_3(self):
"""Tests that the top three words returned matches the standard results"""
num_of_results = 3
standard_results = get_top_words_standard(source, num_of_results)
results = top_words(source, num_of_results)
words = []
standard_words = []
for key, value in standard_results:
standard_words.append(key)
for key, value in results:
words.append(key)
# check that the student hash map returns the exact list as the python dict
self.assertTrue(words == standard_words)
@weight(9)
def test_top_words_5(self):
"""Tests that the top five words returned matches the standard results"""
num_of_results = 5
standard_results = get_top_words_standard(source, num_of_results)
results = top_words(source, num_of_results)
words = []
standard_words = []
for key, value in standard_results:
standard_words.append(key)
for key, value in results:
words.append(key)
# check that the student hash map returns the exact list as the python dict
self.assertTrue(words == standard_words)
# Class for testing the student spell checker: Checks the counts of words returned
class TestStudentWordCount(unittest.TestCase):
@classmethod
def setUpClass(self):
"""Running the HashMap and dictionary functions once to build the results needed for grading"""
num_of_results = 5
self.standard_results = get_top_words_standard(source, num_of_results)
self.results = top_words(source, num_of_results)
self.counts = []
self.standard_counts = []
for key, value in self.standard_results:
self.standard_counts.append(value)
for key, value in self.results:
self.counts.append(value)
@weight(2)
def test_top_word_count_1(self):
"""Test that the top word returned matches the standard count"""
# check that the student hash map returns the exact list as the python dict
self.assertTrue(self.counts[0] == self.standard_counts[0])
@weight(1)
def test_top_word_count_3_a(self):
"""Test that the 1st place of the top 3 returned matches the standard count"""
self.assertTrue(len(self.counts) >= 3)
self.assertTrue(self.counts[0] == self.standard_counts[0])
@weight(1)
def test_top_word_count_3_b(self):
"""Test that the 2nd place of the top 3 returned matches the standard count"""
self.assertTrue(len(self.counts) >= 3)
self.assertTrue(self.counts[1] == self.standard_counts[1])
@weight(1)
def test_top_word_count_3_b(self):
"""Test that the 3rd place of the top 3 returned matches the standard count"""
self.assertTrue(len(self.counts) >= 3)
self.assertTrue(self.counts[2] == self.standard_counts[2])
@weight(1)
def test_top_word_count_5_a(self):
"""Test that the 1st place of the top 5 returned matches the standard count"""
self.assertTrue(len(self.counts) >= 5)
self.assertTrue(self.counts[0] == self.standard_counts[0])
@weight(1)
def test_top_word_count_5_b(self):
"""Test that the 2nd place of the top 5 returned matches the standard count"""
self.assertTrue(len(self.counts) >= 5)
self.assertTrue(self.counts[1] == self.standard_counts[1])
@weight(1)
def test_top_word_count_5_c(self):
"""Test that the 3rd place of the top 5 returned matches the standard count"""
self.assertTrue(len(self.counts) >= 5)
self.assertTrue(self.counts[2] == self.standard_counts[2])
@weight(1)
def test_top_word_count_5_d(self):
"""Test that the 4th place of the top 5 returned matches the standard count"""
self.assertTrue(len(self.counts) >= 5)
self.assertTrue(self.counts[3] == self.standard_counts[3])
@weight(1)
def test_top_word_count_5_e(self):
"""Test that the 5th place of the top 5 returned matches the standard count"""
self.assertTrue(len(self.counts) >= 5)
self.assertTrue(self.counts[4] == self.standard_counts[4])
if __name__ == '__main__':
unittest.main()