# Algorithm based on code kindly provided by j_random_hacker @ Stackoverflow:
# http://stackoverflow.com/questions/7557017/approximate-string-matching-using-backtracking/
module BackTrack
- OK_PATTERN = Regexp.new('^[flsycwphqrimtnkvadegu]+$')
+ OK_PATTERN = Regexp.new('^[bflsycwphqrimtnkvadegu]+$')
MAX_MIS = 5 # Maximum number of mismatches allowed
MAX_INS = 5 # Maximum number of insertions allowed
MAX_DEL = 5 # Maximum number of deletions allowed
# pattern. Matches found in block context return the Match object. Otherwise
# matches are returned in an Array of Match objects.
def patscan(pattern, offset = 0, max_mismatches = 0, max_insertions = 0, max_deletions = 0)
- raise BackTrackError, "Bad pattern: #{pattern}" unless pattern.downcase =~ OK_PATTERN
+ raise BackTrackError, "Bad pattern: #{pattern}" unless pattern.downcase =~ OK_PATTERN
raise BackTrackError, "offset: #{offset} out of range (0 ... #{self.length - 1})" unless (0 ... self.length).include? offset
raise BackTrackError, "max_mismatches: #{max_mismatches} out of range (0 .. #{MAX_MIS})" unless (0 .. MAX_MIS).include? max_mismatches
raise BackTrackError, "max_insertions: #{max_insertions} out of range (0 .. #{MAX_INS})" unless (0 .. MAX_INS).include? max_insertions
while (*s)
{
- e = backtrack(ss, s, p, mm, ins, del);
-
- if (e)
+ if (e = backtrack(ss, s, p, mm, ins, del))
{
tuple = rb_ary_new();
rb_ary_push(tuple, INT2FIX((int) (s - ss)));