-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.py
executable file
·452 lines (402 loc) · 19.8 KB
/
tests.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python3
##### PYTHON IMPORTS ###################################################################################################
import unittest, sys
##### SPLAT IMPORTS ####################################################################################################
from splat.SPLAT import SPLAT
whitman_splat = SPLAT("tests/whitman_test.txt")
frankenstein_splat = SPLAT("tests/frankenstein_test.txt")
flesch_splat = SPLAT("tests/flesch_kincaid_test.txt")
roark_splat = SPLAT("tests/roark_sample.txt")
class TestLogistics(unittest.TestCase):
def test_bad_input(self):
try:
dummy_splat = SPLAT([])
except ValueError as e:
self.assertEqual(e.args[0], "WARNING: SPLAT must be of type str or file.")
except TypeError as e:
self.assertEqual(e.args[0], "argument should be string, bytes or integer, not list")
class TestLexicalFeatures(unittest.TestCase):
global whitman_splat, frankenstein_splat
def test_tokens(self):
expected = ['i', 'celebrate', 'myself', 'and', 'sing', 'myself', 'and', 'what', 'i', 'assume', 'you', 'shall', 'assume', 'for', 'every', 'atom', 'belonging', 'to', 'me', 'as', 'good', 'belongs', 'to', 'you']
output = whitman_splat.tokens()
unexpected = frankenstein_splat.tokens()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_rawtokens(self):
expected = ['I', 'celebrate', 'myself,', 'and', 'sing', 'myself,', 'And', 'what', 'I', 'assume', 'you', 'shall', 'assume,', 'For', 'every', 'atom', 'belonging', 'to', 'me', 'as', 'good', 'belongs', 'to', 'you.']
output = whitman_splat.rawtokens()
unexpected = whitman_splat.tokens()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_types(self):
expected = [('and', 2), ('as', 1), ('assume', 2), ('atom', 1), ('belonging', 1), ('belongs', 1), ('celebrate', 1), ('every', 1), ('for', 1), ('good', 1), ('i', 2), ('me', 1), ('myself', 2), ('shall', 1), ('sing', 1), ('to', 2), ('what', 1), ('you', 2)]
output = whitman_splat.types()
unexpected = frankenstein_splat.types()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_rawtypes(self):
expected = [('And', 1), ('For', 1), ('I', 2), ('and', 1), ('as', 1), ('assume', 1), ('assume,', 1), ('atom', 1), ('belonging', 1), ('belongs', 1), ('celebrate', 1), ('every', 1), ('good', 1), ('me', 1), ('myself,', 2), ('shall', 1), ('sing', 1), ('to', 2), ('what', 1), ('you', 1), ('you.', 1)]
output = whitman_splat.rawtypes()
unexpected = whitman_splat.types()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_type_token_ratio(self):
expected = 0.8163
output = frankenstein_splat.type_token_ratio()
unexpected = whitman_splat.type_token_ratio()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_wordcount(self):
expected = 49
output = frankenstein_splat.wordcount()
unexpected = whitman_splat.wordcount()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_unique_wordcount(self):
expected = 18
output = whitman_splat.unique_wordcount()
unexpected = frankenstein_splat.unique_wordcount()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
class TestSyntacticFeatures(unittest.TestCase):
global whitman_splat, frankenstein_splat, flesch_splat
def test_sents(self):
expected = ['I celebrate myself, and sing myself, And what I assume you shall assume, For every atom belonging to me as good belongs to you.']
output = whitman_splat.sents()
unexpected = frankenstein_splat.sents()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_sentcount(self):
expected = 2
output = frankenstein_splat.sentcount()
unexpected = whitman_splat.sentcount()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_utts(self):
expected = ['I celebrate myself, and sing myself,', 'And what I assume you shall assume,',
'For every atom belonging to me as good belongs to you.']
output = whitman_splat.utts()
unexpected = frankenstein_splat.utts()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_uttcount(self):
expected = 3
output = frankenstein_splat.uttcount()
unexpected = flesch_splat.uttcount()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
class TestGrammaticalFeatures(unittest.TestCase):
global whitman_splat, frankenstein_splat
def test_content_words(self):
expected = ['rejoice', 'hear', 'disaster', 'accompanied', 'commencement', 'enterprise', 'regarded', 'evil',
'forebodings', 'arrived', 'yesterday', 'first', 'task', 'assure', 'dear', 'sister', 'welfare',
'increasing', 'confidence', 'success', 'undertaking']
output = frankenstein_splat.content_words()
unexpected = whitman_splat.content_words()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_function_words(self):
expected = ['you', 'will', 'to', 'that', 'no', 'has', 'the', 'of', 'an', 'which', 'you', 'have', 'with', 'such',
'i', 'here', 'and', 'my', 'is', 'to', 'my', 'of', 'my', 'and', 'in', 'the', 'of', 'my']
output = frankenstein_splat.function_words()
unexpected = whitman_splat.function_words()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_content_function_ratio(self):
expected = 0.75
output = frankenstein_splat.content_function_ratio()
unexpected = whitman_splat.content_function_ratio()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_unique_content_words(self):
expected = ['assume', 'atom', 'belonging', 'belongs', 'celebrate', 'every', 'good', 'shall', 'sing']
output = whitman_splat.unique_content_words()
unexpected = frankenstein_splat.unique_content_words()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_unique_function_words(self):
expected = ['and', 'as', 'for', 'i', 'me', 'myself', 'to', 'what', 'you']
output = whitman_splat.unique_function_words()
unexpected = frankenstein_splat.unique_function_words()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
class TestParsing(unittest.TestCase):
def test_garden_path(self):
pass
def test_local_ambiguity(self):
pass
def test_global_ambiguity(self):
pass
class TestComplexity(unittest.TestCase):
global whitman_splat, frankenstein_splat, flesch_splat, roark_splat
def test_mean_idea_density(self):
expected_whitman = 0.5137
output_whitman = whitman_splat.idea_density()
self.assertAlmostEqual(output_whitman, expected_whitman, 4)
expected_frankenstein = 0.5623
output_frankenstein = frankenstein_splat.idea_density()
self.assertAlmostEqual(output_frankenstein, expected_frankenstein, 4)
expected_roark = 0.2500
output_roark = roark_splat.idea_density()
self.assertAlmostEqual(output_roark, expected_roark, 4)
expected_flesch = 0.3846
output_flesch = flesch_splat.idea_density()
self.assertAlmostEqual(output_flesch, expected_flesch, 4)
def test_max_idea_density(self):
expected_whitman = 0.6364
output_whitman = whitman_splat.max_idea_density()
self.assertAlmostEqual(output_whitman, expected_whitman, 4)
expected_frankenstein = 0.7500
output_frankenstein = frankenstein_splat.max_idea_density()
self.assertAlmostEqual(output_frankenstein, expected_frankenstein, 4)
expected_roark = 0.2500
output_roark = roark_splat.max_idea_density()
self.assertAlmostEqual(output_roark, expected_roark, 4)
expected_flesch = 0.3846
output_flesch = flesch_splat.max_idea_density()
self.assertAlmostEqual(output_flesch, expected_flesch, 4)
def test_min_idea_density(self):
expected_whitman = 0.3333
output_whitman = whitman_splat.min_idea_density()
self.assertAlmostEqual(output_whitman, expected_whitman, 4)
expected_frankenstein = 0.3913
output_frankenstein = frankenstein_splat.min_idea_density()
self.assertAlmostEqual(output_frankenstein, expected_frankenstein, 4)
expected_roark = 0.2500
output_roark = roark_splat.min_idea_density()
self.assertAlmostEqual(output_roark, expected_roark, 4)
expected_flesch = 0.3846
output_flesch = flesch_splat.min_idea_density()
self.assertAlmostEqual(output_flesch, expected_flesch, 4)
def test_mean_content_density(self):
expected_whitman = 1.0778
output_whitman = whitman_splat.content_density()
self.assertAlmostEqual(output_whitman, expected_whitman, 4)
expected_frankenstein = 1.6970
output_frankenstein = frankenstein_splat.content_density()
self.assertAlmostEqual(output_frankenstein, expected_frankenstein, 4)
expected_roark = 1.0000
output_roark = roark_splat.content_density()
self.assertAlmostEqual(output_roark, expected_roark, 4)
expected_flesch = 1.6000
output_flesch = flesch_splat.content_density()
self.assertAlmostEqual(output_flesch, expected_flesch, 4)
def test_max_content_density(self):
expected_whitman = 2.0000
output_whitman = whitman_splat.max_content_density()
self.assertAlmostEqual(output_whitman, expected_whitman, 4)
expected_frankenstein = 3.0000
output_frankenstein = frankenstein_splat.max_content_density()
self.assertAlmostEqual(output_frankenstein, expected_frankenstein, 4)
expected_roark = 1.0000
output_roark = roark_splat.max_content_density()
self.assertAlmostEqual(output_roark, expected_roark, 4)
expected_flesch = 1.6000
output_flesch = flesch_splat.max_content_density()
self.assertAlmostEqual(output_flesch, expected_flesch, 4)
def test_min_content_density(self):
expected_whitman = 0.4000
output_whitman = whitman_splat.min_content_density()
self.assertAlmostEqual(output_whitman, expected_whitman, 4)
expected_frankenstein = 1.0000
output_frankenstein = frankenstein_splat.min_content_density()
self.assertAlmostEqual(output_frankenstein, expected_frankenstein, 4)
expected_roark = 1.0000
output_roark = roark_splat.min_content_density()
self.assertAlmostEqual(output_roark, expected_roark, 4)
expected_flesch = 1.6000
output_flesch = flesch_splat.min_content_density()
self.assertAlmostEqual(output_flesch, expected_flesch, 4)
def test_tree_based_yngve(self):
expected = 1.1250
output = roark_splat.tree_based_yngve_score()
unexpected = whitman_splat.tree_based_yngve_score()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_tree_based_frazier(self):
expected = 0.9375
output = roark_splat.tree_based_frazier_score()
unexpected = whitman_splat.tree_based_frazier_score()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
def test_flesch_readability(self):
expected = 37.5
output = flesch_splat.flesch_readability()
self.assertAlmostEqual(output, expected, 1)
def test_flesch_kincaid(self):
expected = 11.3
output = flesch_splat.kincaid_grade_level()
unexpected = whitman_splat.kincaid_grade_level()
self.assertEqual(output, expected)
self.assertNotEqual(output, unexpected)
class TestSyllables(unittest.TestCase):
def test_suffixes(self):
expected_added = 2
output_added = SPLAT("added").syllables()
self.assertEqual(output_added, expected_added)
expected_apples = 2
output_apples = SPLAT("apples").syllables()
self.assertEqual(output_apples, expected_apples)
expected_foxes = 2
output_foxes = SPLAT("foxes").syllables()
self.assertEqual(output_foxes, expected_foxes)
expected_auntie = 2
output_auntie = SPLAT("auntie").syllables()
self.assertEqual(output_auntie, expected_auntie)
def test_syllabic_l(self):
expected_mantle = 2
output_mantle = SPLAT("mantle").syllables()
self.assertEqual(output_mantle, expected_mantle)
expected_bottle = 2
output_bottle = SPLAT("bottle").syllables()
self.assertEqual(output_bottle, expected_bottle)
expected_saddle = 2
output_saddle = SPLAT("saddle").syllables()
self.assertEqual(output_saddle, expected_saddle)
expected_poodle = 2
output_poodle = SPLAT("poodle").syllables()
self.assertEqual(output_poodle, expected_poodle)
expected_pistol = 2
output_pistol = SPLAT("pistol").syllables()
self.assertEqual(output_pistol, expected_pistol)
expected_tunnel = 2
output_tunnel = SPLAT("tunnel").syllables()
self.assertEqual(output_tunnel, expected_tunnel)
def test_syllabic_m(self):
expected_heroism = 4
output_heroism = SPLAT("heroism").syllables()
self.assertEqual(output_heroism, expected_heroism)
expected_feudalism = 4
output_feudalism = SPLAT("feudalism").syllables()
self.assertEqual(output_feudalism, expected_feudalism)
expected_blossom = 2
output_blossom = SPLAT("blossom").syllables()
self.assertEqual(output_blossom, expected_blossom)
expected_rhythm = 2
output_rhythm = SPLAT("rhythm").syllables()
self.assertEqual(output_rhythm, expected_rhythm)
def test_syllabic_n(self):
expected_cotton = 2
output_cotton = SPLAT("cotton").syllables()
self.assertEqual(output_cotton, expected_cotton)
expected_button = 2
output_button = SPLAT("button").syllables()
self.assertEqual(output_button, expected_button)
expected_risen = 2
output_risen = SPLAT("risen").syllables()
self.assertEqual(output_risen, expected_risen)
expected_prison = 2
output_prison = SPLAT("prison").syllables()
self.assertEqual(output_prison, expected_prison)
expected_sadden = 2
output_sadden = SPLAT("sadden").syllables()
self.assertEqual(output_sadden, expected_sadden)
expected_listen = 2
output_listen = SPLAT("listen").syllables()
self.assertEqual(output_listen, expected_listen)
expected_even = 2
output_even = SPLAT("even").syllables()
self.assertEqual(output_even, expected_even)
def test_syllabic_ng(self):
expected_going = 2
output_going = SPLAT("going").syllables()
self.assertEqual(output_going, expected_going)
expected_listening = 3
output_listening = SPLAT("listening").syllables()
self.assertEqual(output_listening, expected_listening)
expected_ringing = 2
output_ringing = SPLAT("ringing").syllables()
self.assertEqual(output_ringing, expected_ringing)
def test_syllabic_r(self):
expected_history = 3
output_history = SPLAT("history").syllables()
self.assertEqual(output_history, expected_history)
expected_hungary = 3
output_hungary = SPLAT("hungary").syllables()
self.assertEqual(output_hungary, expected_hungary)
expected_preference = 3
output_preference = SPLAT("preference").syllables()
self.assertEqual(output_preference, expected_preference)
def test_special_cases(self):
expected_australian = 3
output_australian = SPLAT("australian").syllables()
self.assertEqual(output_australian, expected_australian)
expected_literal = 3
output_literal = SPLAT("literal").syllables()
self.assertEqual(output_literal, expected_literal)
expected_forebodings = 3
output_forebodings = SPLAT("forebodings").syllables()
self.assertEqual(output_forebodings, expected_forebodings)
expected_pterodactyl = 4
output_pterodactyl = SPLAT("pterodactyl").syllables()
self.assertLessEqual(abs(expected_pterodactyl - output_pterodactyl), 1)
expected_wh = 1
output_wh = SPLAT("wh").syllables()
self.assertEqual(output_wh, expected_wh)
expected_am = 1
output_am = SPLAT("am").syllables()
self.assertEqual(output_am, expected_am)
expected_s_hyphen = 1
output_s_hyphen = SPLAT("s-").syllables()
self.assertEqual(output_s_hyphen, expected_s_hyphen)
def test_proper_names(self):
expected_benjamin = 3
output_benjamin = SPLAT("benjamin").syllables()
self.assertEqual(output_benjamin, expected_benjamin)
expected_emily = 3
output_emily = SPLAT("emily").syllables()
self.assertEqual(output_emily, expected_emily)
expected_cissi = 2
output_cissi = SPLAT("cissi").syllables()
self.assertEqual(output_cissi, expected_cissi)
expected_cecilia = 3
output_cecilia = SPLAT("cecilia").syllables()
self.assertEqual(output_cecilia, expected_cecilia)
expected_ares = 2
output_ares = SPLAT("ares").syllables()
#self.assertEqual(output_ares, expected_ares)
self.assertLessEqual(abs(expected_ares - output_ares), 1)
def run_test_suite(cla):
if cla == "TestSyllables":
suite = unittest.TestSuite()
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestSyllables))
unittest.TextTestRunner(verbosity=2).run(suite)
elif cla == "TestComplexity":
suite = unittest.TestSuite()
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestComplexity))
unittest.TextTestRunner(verbosity=2).run(suite)
elif cla == "TestParsing":
suite = unittest.TestSuite()
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestParsing))
unittest.TextTestRunner(verbosity=2).run(suite)
elif cla == "TestLogistics":
suite = unittest.TestSuite()
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestLogistics))
unittest.TextTestRunner(verbosity=2).run(suite)
elif cla == "TestSyntacticFeatures":
suite = unittest.TestSuite()
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestSyntacticFeatures))
unittest.TextTestRunner(verbosity=2).run(suite)
elif cla == "TestLexicalFeatures":
suite = unittest.TestSuite()
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestLexicalFeatures))
unittest.TextTestRunner(verbosity=2).run(suite)
elif cla == "TestGrammaticalFeatures":
suite = unittest.TestSuite()
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestGrammaticalFeatures))
unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == '__main__':
args = sys.argv
print(args)
test_suites = ['TestSyllables', 'TestComplexity', 'TestParsing', 'TestLogistics', 'TestLexicalFeatures',
'TestSyntacticFeatures', 'TestGrammaticalFeatures']
if len(args) >= 2:
for arg in args[1:]:
if arg in test_suites: run_test_suite(arg)
else:
print("WARNING: Invalid argument " + arg)
print("Running all tests...")
unittest.main()
else:
unittest.main()