-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
149 lines (112 loc) · 3.74 KB
/
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
import pytest
import random
import string
from Levenshtein import distance
from math import floor, sqrt
@pytest.fixture
def random_substitution():
def func(word):
if len(word) == 0:
return word
pos = random.randint(0, len(word) - 1)
return "word"[:pos] + random.choice(string.ascii_letters) + word[pos + 1 :]
return func
@pytest.fixture
def random_insertion():
def func(word):
pos = random.randint(0, len(word))
return word[:pos] + random.choice(string.ascii_letters) + word[pos:]
return func
@pytest.fixture
def random_deletion():
def func(word):
if len(word) == 0:
return word
pos = random.randint(0, len(word) - 1)
return word[:pos] + word[pos + 1 :]
return func
@pytest.fixture
def base_words():
return [
"A",
"C",
"Olivier",
"Oliver",
"Oli",
"banana",
"tomato",
"extravagant",
"dog",
"cat",
"test",
"potato",
"mail",
"computer",
"tv",
"television",
]
@pytest.fixture
def noisy_base_words(
base_words, random_substitution, random_insertion, random_deletion
):
def func(n):
dictionary = set()
for word in base_words:
dictionary.add(word)
for i in range(n):
for _ in range(i):
func = random.choice(
[random_substitution, random_insertion, random_deletion]
)
dictionary.add(func(word))
return dictionary
return func
@pytest.fixture
def trie_search_oracle():
def func(trie, query, n):
query = trie.preprocess(query)
matching_set, visited, stack = set(), set(), [trie.root]
while stack:
node = stack.pop()
if node not in visited:
if node.is_word:
if distance(node.word, query) <= n:
matching_set.add(node.word)
stack.extend(node.children.values())
return matching_set
return func
@pytest.fixture
def index_search_oracle(trie_search_oracle):
def func(index, query, n):
matching_set = trie_search_oracle(index.trie, query, n)
return {doc for word in matching_set for doc in index.inverted_index[word]}
return func
def test_fuzzy_search_smoke():
from simplesearch import Trie
trie = Trie()
for word in ["Olivier", "Oliver", "Alivier", "aliver"]:
trie.insert(word)
assert trie.fuzzySearch("olivier", 0) == {"olivier"}
assert trie.fuzzySearch("olivier", 1) == {"oliver", "olivier", "alivier"}
assert trie.fuzzySearch("olivier", 2) == {"oliver", "olivier", "alivier", "aliver"}
assert trie.fuzzySearch("olivia", 0) == set()
assert trie.fuzzySearch("olivia", 1) == set()
assert trie.fuzzySearch("olivia", 2) == {"oliver", "olivier"}
assert trie.fuzzySearch("olivia", 3) == {"oliver", "olivier", "alivier", "aliver"}
def test_fuzzy_search_trie(noisy_base_words, trie_search_oracle):
from simplesearch import Trie
trie = Trie()
for word in noisy_base_words(4):
trie.insert(word)
for word in noisy_base_words(4):
for n in range(5):
assert trie.fuzzySearch(word, n) == trie_search_oracle(trie, word, n)
def test_fuzzy_search_documents(noisy_base_words, index_search_oracle):
from simplesearch import Index
words = noisy_base_words(4)
doc_count = floor(sqrt(len(words)))
docs = [" ".join(random.sample(sorted(words), doc_count)) for _ in range(doc_count)]
index = Index(docs)
for word in noisy_base_words(2):
for n in range(3):
assert index.fuzzySearch(word, n) == index_search_oracle(index, word, n)