class FuzzyStrings::Match

A Match object holds all the costs of operations for a comparison and can define a match for you

Attributes

deletions[RW]
insertions[RW]
substitutions[RW]
transpositions[RW]

Public Class Methods

new() click to toggle source
# File lib/fuzzy_strings.rb, line 128
def initialize
  @insertions     = 0
  @deletions      = 0
  @substitutions  = 0
  @transpositions = 0
end

Public Instance Methods

cost(use_transpositions=false)
Alias for: score
match?(opts = { :score => 3 }) click to toggle source

Is it a match?

By default checks if the cost of all the operations is no greater then 3

Options

:score
  • Total cost of operations is no greater then X.

  • If specified, doesn’t check any other criterium

:max
  • All of the operations must be no greater then X. So the score may be 3, but there cant be 2 deletions if X = 1.

  • If specified, checks no other criterium.

  • It checks substitutions OR transpositions

:deletions
  • The amount of deletions is no greater then X

:insertions
  • The amount of insertions is no greater then X

:substitutions
  • The amount of substitutions is no greater then X

# File lib/fuzzy_strings.rb, line 151
def match?(opts = { :score => 3 })
  if opts[:score]
    # combined operations
    self.score <= opts[:score]

  elsif opts[:max]
    !(self.deletions > opts[:max]) and !(self.insertions > opts[:max]) \
      and !(self.substitutions > opts[:max]) \
      and !(self.transpositions > opts[:max])

  else
    plausable = true

    if opts[:deletions]
      plausable &= self.deletions <= opts[:deletions]
    end
    if opts[:insertions]
      plausable &= self.insertions <= opts[:insertions]
    end
    if opts[:substitutions]
      plausable &= self.substitutions <= opts[:substitutions]
    end
    if opts[:transpositions]
      plausable &= self.transpositions <= opts[:transpositions]
    end

    plausable
  end
end
score(use_transpositions=false) click to toggle source

the total cost of the operations

Normaly uses substitutions (which is more expensive).

Specify use_transpositions as true to get transposition cost instead of substitutions cost

# File lib/fuzzy_strings.rb, line 188
def score(use_transpositions=false)
  (use_transpositions ? transpositions : substitutions) + insertions + deletions
end
Also aliased as: cost