-
Notifications
You must be signed in to change notification settings - Fork 100
/
spellchecker_test.py
164 lines (141 loc) · 6.74 KB
/
spellchecker_test.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
''' Unittest class '''
from __future__ import division
import unittest
import os
from spellchecker import SpellChecker
class TestSpellChecker(unittest.TestCase):
''' test the spell checker class '''
def test_correction(self):
''' test spell checker corrections '''
spell = SpellChecker()
self.assertEqual(spell.correction('ths'), 'the')
self.assertEqual(spell.correction('ergo'), 'ergo')
self.assertEqual(spell.correction('alot'), 'a lot')
self.assertEqual(spell.correction('this'), 'this')
self.assertEqual(spell.correction('-'), '-')
self.assertEqual(spell.correction('1213'), '1213')
self.assertEqual(spell.correction('1213.9'), '1213.9')
def test_candidates(self):
''' test spell checker candidates '''
spell = SpellChecker()
cands = {'tes', 'tps', 'th', 'thi', 'tvs', 'tds', 'tbs', 'bhs', 'thf',
'chs', 'tis', 'thes', 'tls', 'tho', 'thu', 'thr', 'dhs',
"th'", 'thus', 'ts', 'ehs', 'tas', 'ahs', 'thos', 'thy',
'tcs', 'nhs', 'the', 'tss', 'hs', 'lhs', 'vhs', "t's", 'tha',
'whs', 'ghs', 'rhs', 'this'}
self.assertEqual(spell.candidates('ths'), cands)
self.assertEqual(spell.candidates('the'), {'the'})
self.assertEqual(spell.candidates('-'), {'-'})
def test_words(self):
''' rest the parsing of words '''
spell = SpellChecker()
res = ['this', 'is', 'a', 'test', 'of', 'this']
self.assertEqual(spell.words('This is a test of this'), res)
def test_word_frequency(self):
''' test word frequency '''
spell = SpellChecker()
# if the default load changes so will this...
self.assertEqual(spell.word_frequency['the'], 6187925)
def test_word_probability(self):
''' test the word probability calculation '''
spell = SpellChecker()
# if the default load changes so will this...
num = spell.word_frequency['the']
denom = spell.word_frequency.total_words
self.assertEqual(spell.word_probability('the'), num / denom)
def test_word_known(self):
''' test if the word is a `known` word or not '''
spell = SpellChecker()
self.assertEqual(spell.known(['this']), {'this'})
self.assertEqual(spell.known(['sherlock']), {'sherlock'})
self.assertEqual(spell.known(['holmes']), {'holmes'})
self.assertEqual(spell.known(['known']), {'known'})
self.assertEqual(spell.known(['-']), {'-'})
self.assertEqual(spell.known(['foobar']), set())
self.assertEqual(spell.known(['ths']), set())
self.assertEqual(spell.known(['ergos']), set())
def test_unknown_words(self):
''' test the unknown word functionality '''
spell = SpellChecker()
self.assertEqual(spell.unknown(['this']), set())
self.assertEqual(spell.unknown(['sherlock']), set())
self.assertEqual(spell.unknown(['holmes']), set())
self.assertEqual(spell.unknown(['known']), set())
self.assertEqual(spell.unknown(['-']), set())
self.assertEqual(spell.unknown(['foobar']), {'foobar'})
self.assertEqual(spell.unknown(['ths']), {'ths'})
self.assertEqual(spell.unknown(['ergos']), {'ergos'})
def test_word_in(self):
''' test the use of the `in` operator '''
spell = SpellChecker()
self.assertTrue('key' in spell)
self.assertFalse('rando' in spell)
def test_word_contains(self):
''' test the contains functionality '''
spell = SpellChecker()
self.assertEqual(spell['the'], 6187925)
def test_spanish_dict(self):
''' test loading in the spanish dictionary '''
spell = SpellChecker(language='es')
res = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'ª', 'º', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ç', 'è', 'é', 'ê', 'ë',
'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'ö', 'ø', 'ù', 'ú',
'û', 'ü', 'ý', 'ń', 'ż', 'ž', 'у']
self.assertEqual(sorted(list(spell.word_frequency.letters)), res)
self.assertTrue('mañana' in spell)
self.assertEqual(spell['que'], 12131641)
def test_missing_dictionary(self):
try:
spell = SpellChecker(language='no')
except ValueError as ex:
msg = 'The provided dictionary language (no) does not exist!'
self.assertEqual(str(ex), msg)
def test_load_external_dictionary(self):
''' test loading a local dictionary '''
here = os.path.dirname(__file__)
filepath = '{}/resources/small_dictionary.json'.format(here)
spell = SpellChecker(language=None, local_dictionary=filepath)
self.assertEqual(spell['a'], 1)
self.assertTrue('apple' in spell)
def test_edit_distance_one(self):
''' test a case where edit distance must be one '''
here = os.path.dirname(__file__)
filepath = '{}/resources/small_dictionary.json'.format(here)
spell = SpellChecker(language=None, local_dictionary=filepath, distance=1)
self.assertEqual(spell.candidates('hike'), {'bike'})
def test_edit_distance_two(self):
''' test a case where edit distance must be two '''
here = os.path.dirname(__file__)
filepath = '{}/resources/small_dictionary.json'.format(here)
spell = SpellChecker(language=None, local_dictionary=filepath)
self.assertEqual(spell.candidates('ale'), {'a', 'apple'})
def test_load_text_file(self):
''' test loading a text file '''
here = os.path.dirname(__file__)
filepath = '{}/resources/small_doc.txt'.format(here)
spell = SpellChecker(language=None) # just from this doc!
spell.word_frequency.load_text_file(filepath)
self.assertEqual(spell['a'], 3)
self.assertEqual(spell['storm'], 2)
self.assertFalse('awesome' in spell)
self.assertTrue(spell['whale'])
self.assertTrue('waves' in spell)
def test_remove_words(self):
''' test is a word is removed '''
spell = SpellChecker()
self.assertEqual(spell['the'], 6187925)
spell.word_frequency.remove_words(['the'])
self.assertEqual(spell['the'], 0)
def test_remove_word(self):
''' test a single word removed '''
spell = SpellChecker()
self.assertEqual(spell['teh'], 6)
spell.word_frequency.remove('teh')
self.assertEqual(spell['teh'], 0)
def test_add_word(self):
''' test adding a word '''
spell = SpellChecker()
self.assertEqual(spell['meh'], 0)
spell.word_frequency.add('meh')
self.assertEqual(spell['meh'], 1)