-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbenchmark.py
228 lines (202 loc) · 8.09 KB
/
benchmark.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
To Check install
pip install pytest pytest-benchmark symspellpy SymSpellCppPy
pytest benchmark.py
"""
from symspellpy import SymSpell as SymSpellPy, Verbosity as VerbosityPy
from SymSpellCppPy import SymSpell as SymSpellCpp, Verbosity as VerbosityCpp
import pytest
import os
dict_path = "resources/frequency_dictionary_en_82_765.txt"
@pytest.mark.benchmark(
group="load_dict",
min_rounds=5,
disable_gc=True,
warmup=False
)
def test_load_dict_symspellpy(benchmark):
sym_spell = SymSpellPy(max_dictionary_edit_distance=2, prefix_length=7)
benchmark(sym_spell.load_dictionary, dict_path, term_index=0, count_index=1, separator=" ")
@pytest.mark.benchmark(
group="load_dict",
min_rounds=5,
disable_gc=True,
warmup=False
)
def test_load_dict_symspellcpppy(benchmark):
sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
benchmark(sym_spell.load_dictionary, dict_path, term_index=0, count_index=1, separator=" ")
@pytest.mark.benchmark(
group="lookup",
min_rounds=5,
disable_gc=True,
warmup=False
)
def test_lookup_term_symspellpy(benchmark):
sym_spell = SymSpellPy(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, term_index=0, count_index=1, separator=" ")
input_term = "mEmEbers"
result = benchmark(sym_spell.lookup, input_term, VerbosityPy.CLOSEST, max_edit_distance=2, transfer_casing=True)
assert (result[0].term.lower() == "members")
@pytest.mark.benchmark(
group="lookup",
min_rounds=5,
disable_gc=True,
warmup=False
)
def test_lookup_term_symspellcpppy(benchmark):
sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, term_index=0, count_index=1, separator=" ")
input_term = "mEmEbers"
result = benchmark(sym_spell.lookup, input_term, VerbosityCpp.CLOSEST, max_edit_distance=2)
assert (result[0].term == "members")
@pytest.mark.benchmark(
group="lookup_compound",
min_rounds=5,
disable_gc=True,
warmup=False
)
def test_lookup_compound_term_symspellpy(benchmark):
sym_spell = SymSpellPy(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, term_index=0, count_index=1, separator=" ")
input_term = "whereis th elove"
result = benchmark(sym_spell.lookup_compound, input_term, max_edit_distance=2)
assert (result[0].term == "whereas the love")
@pytest.mark.benchmark(
group="lookup_compound",
min_rounds=5,
disable_gc=True,
warmup=False
)
def test_lookup_compound_term_symspellcpppy(benchmark):
sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, term_index=0, count_index=1, separator=" ")
input_term = "whereis th elove"
result = benchmark(sym_spell.lookup_compound, input_term, max_edit_distance=2)
assert (result[0].term == "whereas the love")
@pytest.mark.benchmark(
group="word_segmentation",
min_rounds=5,
disable_gc=True,
warmup=False
)
def test_word_segmentation_symspellpy(benchmark):
sym_spell = SymSpellPy(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, term_index=0, count_index=1, separator=" ")
input_term = "thequickbrownfoxjumpsoverthelazydog"
result = benchmark(sym_spell.word_segmentation, input_term, max_edit_distance=0, max_segmentation_word_length=5)
assert (result.segmented_string == "the quick brown fox jumps over the lazy dog")
@pytest.mark.benchmark(
group="word_segmentation",
min_rounds=5,
disable_gc=True,
warmup=False
)
def test_word_segmentation_symspellcpppy(benchmark):
sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, term_index=0, count_index=1, separator=" ")
input_term = "thequickbrownfoxjumpsoverthelazydog"
result = benchmark(sym_spell.word_segmentation, input_term, max_edit_distance=0, max_segmentation_word_length=5)
assert (result.segmented_string == "the quick brown fox jumps over the lazy dog")
@pytest.mark.benchmark(
group="save_pickle",
min_rounds=1,
disable_gc=True,
warmup=False
)
def test_save_pickle_symspellpy(benchmark):
sym_spell = SymSpellPy(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, term_index=0, count_index=1, separator=" ")
os.makedirs("temp_py", exist_ok=True)
result = benchmark(sym_spell.save_pickle, "temp_py/temp.pk")
assert (sym_spell._max_length == 28)
@pytest.mark.benchmark(
group="save_pickle",
min_rounds=1,
disable_gc=True,
warmup=False
)
def test_save_pickle_symspellcpppy(benchmark):
sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, term_index=0, count_index=1, separator=" ")
os.makedirs("temp_cpppy", exist_ok=True)
result = benchmark(sym_spell.save_pickle, "temp_cpppy/temp.bin")
assert (sym_spell.max_length() == 28)
@pytest.mark.benchmark(
group="load_pickle",
min_rounds=1,
disable_gc=True,
warmup=False
)
def test_load_pickle_symspellpy(benchmark):
sym_spell = SymSpellPy(max_dictionary_edit_distance=2, prefix_length=7)
benchmark(sym_spell.load_pickle, "temp_py/temp.pk")
os.remove("temp_py/temp.pk")
os.rmdir("temp_py")
assert (sym_spell.lookup("tke", VerbosityPy.CLOSEST)[0].term == "the")
@pytest.mark.benchmark(
group="load_pickle",
min_rounds=1,
disable_gc=True,
warmup=False
)
def test_load_pickle_symspellcpppy(benchmark):
sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
benchmark(sym_spell.load_pickle, "temp_cpppy/temp.bin")
os.remove("temp_cpppy/temp.bin")
os.rmdir("temp_cpppy")
assert (sym_spell.lookup("tke", VerbosityCpp.CLOSEST)[0].term == "the")
@pytest.mark.benchmark(
group="lookup_transfer_casing",
min_rounds=100,
disable_gc=True,
warmup=False
)
def test_lookup_transfer_casing_symspellpy(benchmark):
sym_spell = SymSpellPy(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.create_dictionary_entry("steam", 4)
result = benchmark(sym_spell.lookup, "StreaM", VerbosityPy.TOP, 2,
transfer_casing=True)
assert (result[0].term == "SteaM")
@pytest.mark.benchmark(
group="lookup_transfer_casing",
min_rounds=100,
disable_gc=True,
warmup=False
)
def test_lookup_transfer_casing_symspellcpppy(benchmark):
sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.create_dictionary_entry("steam", 4)
result = benchmark(sym_spell.lookup, "StreaM", VerbosityCpp.TOP, 2,
transfer_casing=True)
assert (result[0].term == "SteaM")
@pytest.mark.benchmark(
group="lookup_compund_transfer_casing",
min_rounds=100,
disable_gc=True,
warmup=False
)
def test_lookup_compund_transfer_casing_symspellpy(benchmark):
sym_spell = SymSpellPy(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, 0, 1)
typo = ("Whereis th elove hehaD Dated forImuch of thepast who "
"couqdn'tread in sixthgrade AND ins pired him")
correction = ("Whereas the love heaD Dated for much of the past "
"who couldn't read in sixth grade AND inspired him")
results = benchmark(sym_spell.lookup_compound, typo, 2, transfer_casing=True)
assert (results[0].term == correction)
@pytest.mark.benchmark(
group="lookup_compund_transfer_casing",
min_rounds=100,
disable_gc=True,
warmup=False
)
def test_lookup_compund_transfer_casing_symspellcpppy(benchmark):
sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
sym_spell.load_dictionary(dict_path, 0, 1)
typo = ("Whereis th elove hehaD Dated forImuch of thepast who "
"couqdn'tread in sixthgrade AND ins pired him")
correction = ("Whereas the love heaD Dated for much of the past "
"who couldn't read in sixth grade AND inspired him")
results = benchmark(sym_spell.lookup_compound, typo, 2, transfer_casing=True)
assert (results[0].term == correction)