Module: Scorerb::String

Included in:
String
Defined in:
lib/scorerb/string.rb

Instance Method Summary collapse

Instance Method Details

#index_anycase(match) ⇒ Object


3
4
5
# File 'lib/scorerb/string.rb', line 3

def index_anycase(match)
  self.index(match.upcase) || self.index(match.downcase)  
end

#score(abrv, fuzzines = 0) ⇒ Object


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
56
57
58
59
60
61
62
63
# File 'lib/scorerb/string.rb', line 8

def score(abrv, fuzzines = 0)

 # If strings are equal return exact one
 return 1.0 if self == abrv
		
 total_char_score = 0
 fuzzies = 1
 bonus_starting_string = false
 abrv_size = abrv.size
 word_size = self.size
 word = self
 final_score = 0

 (0..abrv_size-1).each do |i|
  char = abrv[i,1]
			
  index_in_string = word.index_anycase(char)

  if index_in_string.nil?
   if fuzzines > 0
    fuzzies += 1 - fuzzines
    next
   else
    return 0
   end
  else
   char_score = 0.1
  end

     #The current char is in the same position?
  char_score += 0.1 if word[index_in_string] == char

     #If starting the word
  if index_in_string == 0
   char_score += 0.6
   bonus_starting_string = i == 0
  else
   char_score += 0.8 if word[index_in_string - 1] == " "			
  end

  word = word.slice index_in_string + 1, word.size

  total_char_score += char_score
 end

 abrv_score = total_char_score / abrv_size
		
 #Reduce penalty for longer words
       final_score = ((abrv_score * (abrv_size/word_size)) + abrv_score) / 2

 #Reduce using fuzzies
       final_score = final_score / fuzzies

 final_score += 0.15 if bonus_starting_string && final_score <= 0.85
 final_score
end