#!/usr/bin/env python class ToBigTolerance(Exception): def __init__(self, keyword): self.keyword = keyword def __str__(self): return 'ToBigTolerance: tolerance must be smaller than half keyboard length: %s' % (len(self.keyword)/2) def typo_comparison(keyword, string, tolerance): """ A simple function to make assumptions about string matchings. No regexp - just regular comparisons of strings. Based on assumption that data is rarely typed correctly. """ keyword = keyword.lower() string = string.lower() result = False error_count = 0 if tolerance == 0: _basic_compare(keyword, string) elif tolerance == 1: _basic_compare(keyword, string) if len(string) == len(keyword): for letter in string: if letter in keyword: result = True else: result = False break else: result = False return result else: tolerance -= 1 counter = 0 if tolerance > (len(keyword) / 2): raise ToBigTolerance(keyword) if len(string) >= (len(keyword) - tolerance) and len(string) <= (len(keyword) + tolerance): for letter in string: print "letter is: %s" % letter low_border = counter - tolerance high_border = counter + tolerance + 1 if high_border >= len(keyword): high_border = len(keyword) low_border = high_border - (tolerance *2) -1 elif low_border <= 0: low_border = 0 high_border = (tolerance * 2) +1 print 'counter at: %s' % counter print 'comparing against: %s' % keyword[low_border:high_border] print 'range: %s - %s' % (low_border, high_border) counter += 1 if letter in keyword[low_border:high_border]: continue else: error_count+=1 if error_count > tolerance: result = False else: result = True else: result = False return result def _basic_compare(keyword, string): if keyword == string: result = True else: sresult = False return result