-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
146 lines (126 loc) · 6.22 KB
/
tests.py
File metadata and controls
146 lines (126 loc) · 6.22 KB
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
import unittest
import spell_checker
import os
WORDS = spell_checker.get_word_counts(['data/corpora/big.txt'])
ERR_DIST = spell_checker.create_error_distribution('data/misspellings/wikipedia_common_misspellings.txt', WORDS)
lm = spell_checker.learn_language_model(['data/corpora/big.txt'])
def learn_stupid_text(func):
text = """
Hello, my name is Elad.
I am happy to meet you.
I love sea food and wine.
"""
def new_func(*args, **kwargs):
with open('temp_file.txt', 'wb') as f:
f.write(text)
test_lm = spell_checker.learn_language_model(['temp_file.txt'])
args = args + (test_lm,)
func(*args, **kwargs)
os.remove('temp_file.txt')
return new_func
class SpellCheckerTest(unittest.TestCase):
# @unittest.skip('just for now')
def test_correct_word(self):
err_to_word = {
'abou': 'about',
'aboux': 'about',
'helo': 'hello',
'heloe': 'helped',
'corect': 'correct',
'accademic': 'academic',
'academic': 'academic',
'ook': 'look',
'bok': 'book',
'vox': 'box',
'exemple': 'example',
'exellent': 'excellent',
'familes': 'families',
'ell': 'all',
'simpel': 'simple',
'sunstiturion': 'substitution',
'similer': 'similar',
'newq': 'new',
'ingo': 'into',
'betwen': 'between'
}
for err, word in err_to_word.iteritems():
print 'correcting {}...'.format(err)
correction = spell_checker.correct_word(err, WORDS, ERR_DIST)
print '{}->{}'.format(err, correction)
self.assertEqual(correction, word,
'expected {}->{}, got {} instead'.format(err, word, correction))
def test_correct_word_two_errors(self):
err_to_word = {'parlleament': 'parliament'}
for err, word in err_to_word.iteritems():
correction = spell_checker.correct_word(err, WORDS, ERR_DIST)
self.assertEqual(correction, word,
'expected {}->{}, got {} instead'.format(err, word, correction))
# @unittest.skip('just for now')
def test_language_model(self):
"""w1 w2 w3 w1 w4"""
example_lm = spell_checker.learn_language_model(['../example.txt'], 3, None)
self.assertDictEqual(
example_lm,
{
'w1': {'': 1, 'w2 w3': 1},
'w2': {'w1': 1},
'w3': {'w1 w2': 1},
'w4': {'w3 w1': 1},
'': {'w1 w4': 1, 'w4': 1}
}
)
self.assertEqual(spell_checker.get_count_word_in_context('w1', ['w2', 'w3'], example_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('w1', ['2', 'w3'], example_lm), 0)
self.assertEqual(spell_checker.get_count_word_in_context('w1', [''], example_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('w2', ['w1'], example_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('w1', ['w3'], example_lm), 1)
# @unittest.skip('just for now')
def test_correct_sentence(self):
self.assertEqual(spell_checker.correct_sentence(
"I would be in te room", lm, ERR_DIST, 1, 0.8),
"I would be in the room")
self.assertEqual(spell_checker.correct_sentence(
"Go te the prison", lm, ERR_DIST, 1, 0.8),
"Go to the prison")
# @unittest.skip('just for now')
def test_correct_sentence_real_word(self):
self.assertEqual(spell_checker.correct_sentence(
"I would be in he room", lm, ERR_DIST, 1, 0.8),
"I would be in her room")
# @unittest.skip('just for now')
def test_correct_sentence_two_non_words(self):
self.assertEqual(spell_checker.correct_sentence(
"I hav to goe", lm, ERR_DIST, 2, 0.8),
"I have to go")
# @unittest.skip('just for now')
def test_get_n_from_language_model(self):
lm_2 = spell_checker.learn_language_model(['data/corpora/big.txt'], 2, None)
lm_3 = spell_checker.learn_language_model(['data/corpora/big.txt'], 3, None)
lm_4 = spell_checker.learn_language_model(['data/corpora/big.txt'], 4, None)
self.assertEqual(spell_checker.get_n_from_language_model(lm_2), 2)
self.assertEqual(spell_checker.get_n_from_language_model(lm_3), 3)
self.assertEqual(spell_checker.get_n_from_language_model(lm_4), 4)
# @unittest.skip('just for now')
@learn_stupid_text
def test_count_word_in_context(self, test_lm):
self.assertEqual(spell_checker.get_count_word_in_context('elad', ['is'], test_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('elad', ['name', 'is'], test_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('i', [''], test_lm), 2)
self.assertEqual(spell_checker.get_count_word_in_context('you', ['wanna', 'meet'], test_lm), 0)
self.assertEqual(spell_checker.get_count_word_in_context('you', ['meet'], test_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('elad', ['is'], test_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('elad', ['name', 'is'], test_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('', ['elad'], test_lm), 2)
self.assertEqual(spell_checker.get_count_word_in_context('', ['is', 'elad'], test_lm), 1)
self.assertEqual(spell_checker.get_count_word_in_context('love', ['', 'i'], test_lm), 1)
# @unittest.skip('just for now')
@learn_stupid_text
def test_get_counts_of_context(self, test_lm):
self.assertEqual(spell_checker.get_count_of_context(['my', 'name'], test_lm), 1)
# @unittest.skip('just for now')
@learn_stupid_text
def test_get_unique_context_count(self, test_lm):
self.assertEqual(spell_checker.unique_context_of_len_count(3, test_lm), 11)
self.assertEqual(spell_checker.unique_context_of_len_count(2, test_lm), 14)
if __name__ == '__main__':
unittest.main(verbosity=2)