Skip to content

Commit

Permalink
fix custom processors in fuzz.*
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbachmann committed Dec 13, 2020
1 parent fa17ff9 commit cc5fa23
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.14.0
0.14.1
2 changes: 1 addition & 1 deletion src/py_abstraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static inline PyObject* fuzz_call(bool processor_default, PyObject* args, PyObje
}

if (non_default_process(processor)) {
PyObject* proc_s1 = PyObject_CallFunctionObjArgs(processor, py_s2, NULL);
PyObject* proc_s1 = PyObject_CallFunctionObjArgs(processor, py_s1, NULL);
if (proc_s1 == NULL) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rapidfuzz-cpp
Submodule rapidfuzz-cpp updated from 028253 to 3917c1
2 changes: 1 addition & 1 deletion src/rapidfuzz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"""
__author__ = "Max Bachmann"
__license__ = "MIT"
__version__ = "0.14.0"
__version__ = "0.14.1"

from rapidfuzz import process, fuzz, utils, levenshtein
82 changes: 43 additions & 39 deletions tests/test_fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

import unittest
import pytest

from rapidfuzz import process, fuzz, utils

Expand All @@ -19,31 +20,27 @@
]

class RatioTest(unittest.TestCase):
def setUp(self):
self.s1 = "new york mets"
self.s1a = "new york mets"
self.s2 = "new YORK mets"
self.s3 = "the wonderful new york mets"
self.s4 = "new york mets vs atlanta braves"
self.s5 = "atlanta braves vs new york mets"
self.s6 = "new york mets - atlanta braves"

def testEqual(self):
self.assertEqual(fuzz.ratio(self.s1, self.s1a),100)

def testCaseInsensitive(self):
self.assertNotEqual(fuzz.ratio(self.s1, self.s2),100)
self.assertEqual(fuzz.ratio(self.s1, self.s2, processor=True),100)
s1 = "new york mets"
s1a = "new york mets"
s2 = "new YORK mets"
s3 = "the wonderful new york mets"
s4 = "new york mets vs atlanta braves"
s5 = "atlanta braves vs new york mets"
s6 = "new york mets - atlanta braves"

def testNoProcessor(self):
self.assertEqual(fuzz.ratio(self.s1, self.s1a), 100)
self.assertNotEqual(fuzz.ratio(self.s1, self.s2), 100)

def testPartialRatio(self):
self.assertEqual(fuzz.partial_ratio(self.s1, self.s3),100)
self.assertEqual(fuzz.partial_ratio(self.s1, self.s3), 100)

def testTokenSortRatio(self):
self.assertEqual(fuzz.token_sort_ratio(self.s1, self.s1a),100)
self.assertEqual(fuzz.token_sort_ratio(self.s1, self.s1a), 100)

def testPartialTokenSortRatio(self):
self.assertEqual(fuzz.partial_token_sort_ratio(self.s1, self.s1a),100)
self.assertEqual(fuzz.partial_token_sort_ratio(self.s4, self.s5),100)
self.assertEqual(fuzz.partial_token_sort_ratio(self.s1, self.s1a), 100)
self.assertEqual(fuzz.partial_token_sort_ratio(self.s4, self.s5), 100)

def testTokenSetRatio(self):
self.assertEqual(fuzz.token_set_ratio(self.s4, self.s5),100)
Expand Down Expand Up @@ -100,27 +97,34 @@ def testQRatioUnicodeString(self):
score = fuzz.QRatio(s1, s2)
self.assertEqual(0, score)

def testWithProcessor(self):
"""
Any scorer should accept any type as s1 and s2, as long as it is a string
after preprocessing.
"""
s1 = ["chicago cubs vs new york mets", "CitiField", "2011-05-11", "8pm"]
s2 = ["chicago cubs vs new york mets", "CitiFields", "2012-05-11", "9pm"]

for scorer in scorers:
score = scorer(s1, s2, processor=lambda event: event[0])
self.assertEqual(score, 100)

def testHelp(self):
"""
test that all help texts can be printed without throwing an exception,
since they are implemented in C++ aswell
"""

for scorer in scorers:
help(scorer)

@pytest.mark.parametrize("processor", [True, utils.default_process, lambda s: utils.default_process(s)])
def testRatioCaseInsensitive(processor):
assert fuzz.ratio(RatioTest.s1, RatioTest.s2, processor=processor) == 100

@pytest.mark.parametrize("processor", [False, None, lambda s: s])
def testRatioNotCaseInsensitive(processor):
assert fuzz.ratio(RatioTest.s1, RatioTest.s2, processor=processor) != 100

@pytest.mark.parametrize("scorer", scorers)
def testCustomProcessor(scorer):
"""
Any scorer should accept any type as s1 and s2, as long as it is a string
after preprocessing.
"""
s1 = ["chicago cubs vs new york mets", "CitiField", "2011-05-11", "8pm"]
s2 = ["chicago cubs vs new york mets", "CitiFields", "2012-05-11", "9pm"]
s3 = ["different string", "CitiFields", "2012-05-11", "9pm"]
assert scorer(s1, s2, processor=lambda event: event[0]) == 100
assert scorer(s2, s3, processor=lambda event: event[0]) != 100

@pytest.mark.parametrize("scorer", scorers)
def testHelp(scorer):
"""
test that all help texts can be printed without throwing an exception,
since they are implemented in C++ aswell
"""
help(scorer)

if __name__ == '__main__':
unittest.main()

0 comments on commit cc5fa23

Please sign in to comment.