Class: Gamefic::Keywords

Inherits:
Array
  • Object
show all
Defined in:
lib/gamefic/keywords.rb

Instance Method Summary collapse

Methods inherited from Array

#join_and, #join_or, #pop_random, #random, #shuffle, #shuffle!, #that_are, #that_are_not

Constructor Details

#initialize(statement = '') ⇒ Keywords

Returns a new instance of Keywords.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/gamefic/keywords.rb', line 6

def initialize(statement = '')
  if statement.kind_of?(Keywords)
    self.concat statement
  else
    if statement.kind_of?(Array)
      statement = statement.join(' ')
    end
    self.concat statement.to_s.gsub(/[^a-z0-9]/i, ' ').strip.downcase.split(' ')
  end
  self
end

Instance Method Details

#found_in(other, fuzzy = false) ⇒ Float

Count the number of matching words in another Keywords array. The total includes partial matches; for example, “gre” is a 0.6 match for “green”.

Returns:

  • (Float)

    The total number of matches



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gamefic/keywords.rb', line 23

def found_in(other, fuzzy = false)
  matches = 0.0
  self.each { |my_word|
    if other.include?(my_word)
      matches = matches + 1.0
    else
      other.each { |other_word|
        if my_word.length < other_word.length
          if other_word[0, my_word.length] == my_word and my_word.length > 2
            matches = matches + (my_word.length.to_f / other_word.length.to_f)
          end
        elsif fuzzy
          fuzzy_word = fuzzify my_word
          if other_word[0, fuzzy_word.length] == fuzzy_word and fuzzy_word.length > 2
            matches = matches + (fuzzy_word.length.to_f / other_word.length.to_f)
          elsif fuzzy_word[0, other_word.length] == other_word and other_word.length > 2
            matches = matches + (fuzzy_word.length.to_f / other_word.length.to_f)
          end
        end
      }
    end
  }
  matches
end

#to_sObject



48
49
50
# File 'lib/gamefic/keywords.rb', line 48

def to_s
  self.join(' ')
end