-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_gen_tests.py
92 lines (74 loc) · 3.55 KB
/
text_gen_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
""" Simple unit tests for text_gen functions.
Author: Nathan Sprague
Version: 8/26/14
"""
import unittest
import text_gen
class TestNGrams(unittest.TestCase):
def setUp(self):
self.unigrams = {'i': 0.4, 'am': 0.2, 'think': 0.2, 'therefore': 0.2}
self.bigrams = {'i': {'am': 0.25, 'think': 0.75},
'am': {'i': 1.0},
'think': {'i': 0.5, 'therefore': 0.5},
'therefore': {'i': 1.0}}
self.trigrams = {('think', 'i'): {'think': 1.0},
('i', 'am'): {'i': 1.0},
('therefore', 'i'): {'am': 1.0},
('think', 'therefore'): {'i': 1.0},
('i', 'think'): {'i': 0.5, 'therefore': 0.5},
('am', 'i'): {'think': 1.0}}
# Unigram Tests ------------
def test_calculate_unigrams(self):
unigrams = text_gen.calculate_unigrams(['i', 'think',
'therefore', 'i', 'am'])
self.assertEqual(unigrams, self.unigrams)
# Bigram Tests ------------
def test_calculate_bigrams(self):
bigrams = text_gen.calculate_bigrams(['i', 'think', 'therefore',
'i', 'am', 'i', 'think',
'i', 'think'])
self.assertEqual(bigrams, self.bigrams)
def test_calculate_trigrams(self):
trigrams = text_gen.calculate_trigrams(['i', 'think', 'therefore',
'i', 'am', 'i', 'think',
'i', 'think'])
self.assertEqual(trigrams, self.trigrams)
def test_bigram_text_correct_length(self):
text = text_gen.random_bigram_text('i', self.bigrams, 10)
self.assertEqual(len(text.split()), 10)
def test_bigram_text_correct_start(self):
text = text_gen.random_bigram_text('i', self.bigrams, 10)
self.assertEqual(text.split()[0], 'i')
text = text_gen.random_bigram_text('think', self.bigrams, 10)
self.assertEqual(text.split()[0], 'think')
def test_bigram_text_possible(self):
words = text_gen.random_bigram_text('i', self.bigrams, 100).split()
prev = 'i'
for i in range(1, len(words)):
current = words[i]
self.assertGreaterEqual(self.bigrams[prev][current], 0)
prev = current
# Trigram Tests ------------
def test_trigram_text_correct_length(self):
text = text_gen.random_trigram_text('i', 'think', self.bigrams,
self.trigrams, 10)
self.assertEqual(len(text.split()), 10)
def test_trigram_text_correct_start(self):
text = text_gen.random_trigram_text('i', 'think', self.bigrams,
self.trigrams, 10)
self.assertEqual(text.split()[0], 'i')
self.assertEqual(text.split()[1], 'think')
text = text_gen.random_trigram_text('am', 'i', self.bigrams,
self.trigrams, 10)
self.assertEqual(text.split()[0], 'am')
self.assertEqual(text.split()[1], 'i')
def test_trigram_text_possible(self):
words = text_gen.random_trigram_text(
'i', 'think', self.bigrams, self.trigrams, 100).split()
prev = 'i'
for i in range(1, len(words)):
current = words[i]
self.assertGreaterEqual(self.bigrams[prev][current], 0)
prev = current
if __name__ == '__main__':
unittest.main()