/usr/lib/ruby/vendor_ruby/did_you_mean/spell_checkable.rb is in ruby-did-you-mean 1.0.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | # -*- frozen-string-literal: true -*-
require "did_you_mean/levenshtein"
require "did_you_mean/jaro_winkler"
module DidYouMean
module SpellCheckable
def corrections
@corrections ||= candidates.flat_map do |input, candidates|
input = normalize(input)
threshold = input.length > 3 ? 0.834 : 0.77
seed = candidates.select {|candidate| JaroWinkler.distance(normalize(candidate), input) >= threshold }
.sort_by! {|candidate| JaroWinkler.distance(candidate.to_s, input) }
.reverse!
# Correct mistypes
threshold = (input.length * 0.25).ceil
has_mistype = seed.rindex {|c| Levenshtein.distance(normalize(c), input) <= threshold }
corrections = if has_mistype
seed.take(has_mistype + 1)
else
# Correct misspells
seed.select do |candidate|
candidate = normalize(candidate)
length = input.length < candidate.length ? input.length : candidate.length
Levenshtein.distance(candidate, input) < length
end.first(1)
end
corrections
end
end
def candidates
raise NotImplementedError
end
private
def normalize(str_or_symbol) #:nodoc:
str = if str_or_symbol.is_a?(String)
str_or_symbol.dup
else
str_or_symbol.to_s
end
str.downcase!
str.tr!("@", "")
str
end
end
end
|