Class: FuzzyStrings::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzy_strings.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMatch

Returns a new instance of Match.



128
129
130
131
132
133
# File 'lib/fuzzy_strings.rb', line 128

def initialize
  @insertions     = 0
  @deletions      = 0
  @substitutions  = 0
  @transpositions = 0
end

Instance Attribute Details

#deletionsObject

Returns the value of attribute deletions.



126
127
128
# File 'lib/fuzzy_strings.rb', line 126

def deletions
  @deletions
end

#insertionsObject

Returns the value of attribute insertions.



126
127
128
# File 'lib/fuzzy_strings.rb', line 126

def insertions
  @insertions
end

#substitutionsObject

Returns the value of attribute substitutions.



126
127
128
# File 'lib/fuzzy_strings.rb', line 126

def substitutions
  @substitutions
end

#transpositionsObject

Returns the value of attribute transpositions.



126
127
128
# File 'lib/fuzzy_strings.rb', line 126

def transpositions
  @transpositions
end

Instance Method Details

#match?(opts = { :score => 3 }) ⇒ Boolean

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

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# 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) ⇒ Object Also known as: cost

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



188
189
190
# File 'lib/fuzzy_strings.rb', line 188

def score(use_transpositions=false)
  (use_transpositions ? transpositions : substitutions) + insertions + deletions
end

#to_sObject

:nodoc:



193
194
195
# File 'lib/fuzzy_strings.rb', line 193

def to_s # :nodoc:
  "{ d: #{@deletions}, i: #{@insertions}, s: #{@substitutions}, t: #{@transpositions} }"
end