Class: FuzzyStrings::Match
- Inherits:
-
Object
- Object
- FuzzyStrings::Match
- 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
-
#deletions ⇒ Object
Returns the value of attribute deletions.
-
#insertions ⇒ Object
Returns the value of attribute insertions.
-
#substitutions ⇒ Object
Returns the value of attribute substitutions.
-
#transpositions ⇒ Object
Returns the value of attribute transpositions.
Instance Method Summary collapse
-
#initialize ⇒ Match
constructor
A new instance of Match.
-
#match?(opts = { :score => 3 }) ⇒ Boolean
Is it a match?.
-
#score(use_transpositions = false) ⇒ Object
(also: #cost)
the total cost of the operations.
-
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ Match
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
#deletions ⇒ Object
Returns the value of attribute deletions.
126 127 128 |
# File 'lib/fuzzy_strings.rb', line 126 def deletions @deletions end |
#insertions ⇒ Object
Returns the value of attribute insertions.
126 127 128 |
# File 'lib/fuzzy_strings.rb', line 126 def insertions @insertions end |
#substitutions ⇒ Object
Returns the value of attribute substitutions.
126 127 128 |
# File 'lib/fuzzy_strings.rb', line 126 def substitutions @substitutions end |
#transpositions ⇒ Object
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
-
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_s ⇒ Object
:nodoc:
193 194 195 |
# File 'lib/fuzzy_strings.rb', line 193 def to_s # :nodoc: "{ d: #{@deletions}, i: #{@insertions}, s: #{@substitutions}, t: #{@transpositions} }" end |