Module: Diff

Included in:
HTMLDiff
Defined in:
lib/diff.rb

Defined Under Namespace

Classes: SequenceMatcher

Class Method Summary collapse

Class Method Details

.calculate_ratio(matches, length) ⇒ Object

end class SequenceMatcher



266
267
268
269
# File 'lib/diff.rb', line 266

def self.calculate_ratio(matches, length)
  return 1.0 if length.zero?
  2.0 * matches / length
end

.count_leading(line, ch) ⇒ Object



300
301
302
303
304
305
306
# File 'lib/diff.rb', line 300

def self.count_leading(line, ch)
  i, n = 0, line.length
  while i < n and line[i].chr == ch
    i += 1
  end
  i
end

.get_close_matches(word, possibilities, n = 3, cutoff = 0.6) ⇒ Object

XXX: untested



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/diff.rb', line 272

def self.get_close_matches(word, possibilities, n=3, cutoff=0.6)
  unless n > 0
    raise "n must be > 0: #{n}"
  end
  unless 0.0 <= cutoff and cutoff <= 1.0
    raise "cutoff must be in (0.0..1.0): #{cutoff}"
  end

  result = []
  s = SequenceMatcher.new
  s.set_seq_b word
  possibilities.each do |x|
    s.set_seq_a x
    if s.real_quick_ratio >= cutoff and
        s.quick_ratio >= cutoff and
        s.ratio >= cutoff
      result.push [s.ratio, x]
    end
  end
  
  unless result.nil? or result.empty?
    result.sort
    result.reverse!
    result = result[-n..-1]
  end
  result.collect { |score, x| x }
end