Permalink
Browse files

Check for null/empty strings in QRatio and WRatio. Add tests. Closes #3.

  • Loading branch information...
1 parent 57c40ab commit b179a06e68cd3257138bf73496b3cd79a1332baf @acslater00 acslater00 committed Jul 21, 2011
Showing with 43 additions and 0 deletions.
  1. +7 −0 fuzzywuzzy/fuzz.py
  2. +28 −0 fuzzywuzzy/tests.py
  3. +8 −0 fuzzywuzzy/utils.py
View
@@ -156,12 +156,19 @@ def partial_token_set_ratio(s1, s2):
# q is for quick
def QRatio(s1, s2):
+ if not utils.validate_string(s1): return 0
+ if not utils.validate_string(s2): return 0
+
p1 = utils.full_process(s1)
p2 = utils.full_process(s2)
+
return ratio(p1, p2)
# w is for weighted
def WRatio(s1, s2):
+ if not utils.validate_string(s1): return 0
+ if not utils.validate_string(s2): return 0
+
p1 = utils.full_process(s1)
p2 = utils.full_process(s2)
View
@@ -184,6 +184,34 @@ def testWithCutoff(self):
best = process.extractOne(query, choices)
self.assertIsNotNone(best)
+ def testEmptyStrings(self):
+ choices = [
+ "",
+ "new york mets vs chicago cubs",
+ "new york yankees vs boston red sox",
+ "",
+ ""
+ ]
+
+ query = "new york mets at chicago cubs"
+
+ best = process.extractOne(query, choices)
+ self.assertEqual(best[0], choices[1])
+
+ def testNullStrings(self):
+ choices = [
+ None,
+ "new york mets vs chicago cubs",
+ "new york yankees vs boston red sox",
+ None,
+ None
+ ]
+
+ query = "new york mets at chicago cubs"
+
+ best = process.extractOne(query, choices)
+ self.assertEqual(best[0], choices[1])
+
if __name__ == '__main__':
unittest.main() # run all tests
View
@@ -21,6 +21,14 @@ def remove_punctuation(s):
s = s.replace(","," ").replace("."," ").replace("-"," ").replace(":"," ")
return s
+def validate_string(s):
+ if s is None: return False
+ try:
+ if len(s) == 0: return False
+ except:
+ return False
+ return True
+
def full_process(s):
s = s.lower()
s = s.strip()

0 comments on commit b179a06

Please sign in to comment.