-
Notifications
You must be signed in to change notification settings - Fork 5
/
word_tests.py
74 lines (60 loc) · 3.24 KB
/
word_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
__author__ = 'simone'
import unittest
from word import Word
class WordDeletesTests(unittest.TestCase):
def test_deletes(self):
self.assertSetEqual(Word.deletes('ciao', 3),
set(['iao', 'cio', 'cao', 'cia', 'co', 'ca', 'ci', 'ao', 'io', 'ia', 'c', 'i', 'a', 'o']))
self.assertSetEqual(Word.deletes('ciao', 2),
set(['iao', 'cio', 'cao', 'cia', 'co', 'ca', 'ci', 'ao', 'io', 'ia']))
self.assertSetEqual(Word.deletes('ciao', 1),
set(['iao', 'cio', 'cao', 'cia']))
self.assertSetEqual(Word.deletes('ciao', 0),
set())
self.assertSetEqual(Word.deletes('aaa', 30),
set(['aa', 'a']))
self.assertSetEqual(Word.deletes('bbb', 3),
set(['bb', 'b']))
self.assertSetEqual(Word.deletes('woho', 1),
set(['woh', 'oho', 'who', 'woo']))
class WordDamerauLevenstheinTests(unittest.TestCase):
def test_damerau_levenshtein_distance(self):
self.assertEqual(Word.damerau_levenshtein_distance('ciao', 'ciao'), 0)
self.assertEqual(Word.damerau_levenshtein_distance('ciao', 'cia'), 1)
self.assertEqual(Word.damerau_levenshtein_distance('cia', 'ciao'), 1)
self.assertEqual(Word.damerau_levenshtein_distance('ciao', 'co'), 2)
self.assertEqual(Word.damerau_levenshtein_distance('ciao', 'c'), 3)
self.assertEqual(Word.damerau_levenshtein_distance('ciao', ''), 4)
self.assertEqual(Word.damerau_levenshtein_distance('simone', 'siomne'), 1)
self.assertEqual(Word.damerau_levenshtein_distance('simone', 'siomen'), 2)
class WordDistTests(unittest.TestCase):
def setUp(self):
self.edit_distance_max = 2
self.shorter_words_within_distance = Word.shorter_words_within_distance
self.words = set(['apXplYe', 'apXple', 'apple',
'mellon', 'melon', 'meln',
'pear', 'pr',
'orange', 'rng'])
def test_shorter_words_within_distance(self):
words = self.words.copy()
res = self.shorter_words_within_distance('apXplYe', words, self.edit_distance_max)
self.assertSetEqual(res, set(['apXple', 'apple']))
self.assertSetEqual(words, self.words - res)
words = self.words.copy()
res = self.shorter_words_within_distance('apXple', words, self.edit_distance_max)
self.assertSetEqual(res, set(['apple']))
self.assertSetEqual(words, self.words - res)
words = self.words.copy()
res = self.shorter_words_within_distance('mellon', words, self.edit_distance_max)
self.assertSetEqual(res, set(['melon', 'meln']))
self.assertSetEqual(words, self.words - res)
words = self.words.copy()
res = self.shorter_words_within_distance('pear', words, self.edit_distance_max)
self.assertSetEqual(res, set(['pr']))
self.assertSetEqual(words, self.words - res)
words = self.words.copy()
res = self.shorter_words_within_distance('orange', words, self.edit_distance_max)
self.assertSetEqual(res, set())
self.assertSetEqual(words, self.words - res)
if __name__ == '__main__':
unittest.main()