| In: |
libraries/diff/diff.rb
|
# File libraries/diff/diff.rb, line 266
266: def self.calculate_ratio(matches, length)
267: return 1.0 if length.zero?
268: 2.0 * matches / length
269: end
# File libraries/diff/diff.rb, line 300
300: def self.count_leading(line, ch)
301: i, n = 0, line.length
302: while i < n and line[i].chr == ch
303: i += 1
304: end
305: i
306: end
XXX: untested
# File libraries/diff/diff.rb, line 272
272: def self.get_close_matches(word, possibilities, n=3, cutoff=0.6)
273: unless n > 0
274: raise "n must be > 0: #{n}"
275: end
276: unless 0.0 <= cutoff and cutoff <= 1.0
277: raise "cutoff must be in (0.0..1.0): #{cutoff}"
278: end
279:
280: result = []
281: s = SequenceMatcher.new
282: s.set_seq_b word
283: possibilities.each do |x|
284: s.set_seq_a x
285: if s.real_quick_ratio >= cutoff and
286: s.quick_ratio >= cutoff and
287: s.ratio >= cutoff
288: result.push [s.ratio, x]
289: end
290: end
291:
292: unless result.nil? or result.empty?
293: result.sort
294: result.reverse!
295: result = result[-n..-1]
296: end
297: result.collect { |score, x| x }
298: end