class String
def to_score(abbrev)
return 0.9 if abbrev.empty?
return 0.0 if length < abbrev.length
tail = ""
loop do
return 0 if abbrev.empty?
unless md = match(/#{abbrev}/i)
abbrev, tail = abbrev.split_at(-1)
next
else
remaining_score = md.post_match.to_score(tail)
return calc_score(md, remaining_score)
end
end
end
protected
def calc_score(md, remaining_score)
if remaining_score.zero?
0
else
score = (1 * md.to_s.length) + (remaining_score * md.post_match.length)
if md.pre_match =~ /[\s_]+$/
score += 0.85 * (md.pre_match.length - $&.length) + 1 * $&.length
elsif !md.pre_match.empty? and md.to_s =~ /^[A-Z]/
score += 0.85 * md.pre_match.gsub(/[A-Z]/, "").length
end
score / self.length
end
end
def split_at(index)
return slice(0...index), ( slice(index..-1) || "" )
end
end
Wednesday, March 24, 2010
QuickSilver Scoring method
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment