Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Faizal B H authored and ahmadfaizalbh committed Aug 3, 2020
1 parent 90e05dd commit 08fd015
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions test/spellcheck.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
from spellcheck import correction, words, Counter, WORDS, p, path
import chatbot
from os import path
from collections import Counter
from chatbot.spellcheck import SpellChecker


def unit_tests():
assert correction('speling') == 'spelling' # insert
assert correction('korrectud') == 'corrected' # replace 2
assert correction('bycycle') == 'bicycle' # replace
assert correction('inconvient') == 'inconvenient' # insert 2
assert correction('arrainged') == 'arranged' # delete
assert correction('peotry') == 'poetry' # transpose
assert correction('peotryy') == 'poetry' # transpose + delete
assert correction('word') == 'word' # known
assert correction('quintessential') == 'quintessential' # unknown
assert words('This is a TEST.') == ['this', 'is', 'a', 'test']
assert Counter(words('This is a test. 123; A TEST this is.')) == (
local_path = path.join(path.dirname(path.abspath(chatbot.__file__)), "local")
spell_checker = SpellChecker(local_path, language='en')
assert spell_checker.correction('speling') == 'spelling' # insert
assert spell_checker.correction('korrectud') == 'corrected' # replace 2
assert spell_checker.correction('bycycle') == 'bicycle' # replace
assert spell_checker.correction('inconvient') == 'inconvenient' # insert 2
assert spell_checker.correction('arrainged') == 'arranged' # delete
assert spell_checker.correction('peotry') == 'poetry' # transpose
assert spell_checker.correction('peotryy') == 'poetry' # transpose + delete
assert spell_checker.correction('word') == 'word' # known
assert spell_checker.correction('quintessential') == 'quintessential' # unknown
assert spell_checker.words('This is a TEST.') == ['this', 'is', 'a', 'test']
assert Counter(
spell_checker.words('This is a test. 123; A TEST this is.')) == (
Counter({'123': 1, 'a': 2, 'is': 2, 'test': 2, 'this': 2}))
assert len(WORDS) == 506747
assert sum(WORDS.values()) == 1642669
assert WORDS.most_common(10) == [
assert len(spell_checker.WORDS) == 506747
assert sum(spell_checker.WORDS.values()) == 1642669
assert spell_checker.WORDS.most_common(10) == [
('the', 79811),
('of', 40026),
('and', 38315),
Expand All @@ -27,9 +33,9 @@ def unit_tests():
('he', 12404),
('was', 11411),
('it', 10684)]
assert WORDS['the'] == 79811
assert p('quintessential') > 0
assert 0.04 < p('the') < 0.08
assert spell_checker.WORDS['the'] == 79811
assert spell_checker.probability('quintessential') > 0
assert 0.04 < spell_checker.probability('the') < 0.08
return 'unit_tests pass'


Expand All @@ -41,14 +47,16 @@ def spell_test(tests, verbose=False):
start = time.clock()
good, unknown = 0, 0
n = len(tests)
local_path = path.join(path.dirname(path.abspath(chatbot.__file__)), "local")
spell_checker = SpellChecker(local_path, language='en')
for right, wrong in tests:
w = correction(wrong)
w = spell_checker.correction(wrong)
good += (w == right)
if w != right:
unknown += (right not in WORDS)
unknown += (right not in spell_checker.WORDS)
if verbose:
print('correction({}) => {} ({}); expected {} ({})'
.format(wrong, w, WORDS[w], right, WORDS[right]))
.format(wrong, w, spell_checker.WORDS[w], right, spell_checker.WORDS[right]))
dt = time.clock() - start
print('{:.0%} of {} correct ({:.0%} unknown) at {:.0f} words per second '
.format(good / n, n, unknown / n, n / dt))
Expand Down

0 comments on commit 08fd015

Please sign in to comment.