Class: FuzzyTools::WeightedDocumentTokens

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzy_tools/weighted_document_tokens.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, options) ⇒ WeightedDocumentTokens

Returns a new instance of WeightedDocumentTokens.



7
8
9
10
# File 'lib/fuzzy_tools/weighted_document_tokens.rb', line 7

def initialize(tokens, options)
  weight_function = options[:weight_function]
  set_token_weights(tokens, &weight_function)
end

Instance Attribute Details

#weightsObject (readonly)

Returns the value of attribute weights.



5
6
7
# File 'lib/fuzzy_tools/weighted_document_tokens.rb', line 5

def weights
  @weights
end

Instance Method Details

#cosine_similarity(other) ⇒ Object

Rubinius and JRuby



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fuzzy_tools/weighted_document_tokens.rb', line 15

def cosine_similarity(other)
  # equivalent to the C below, but the C is >2x faster
  similarity = 0.0
  other_weights = other.weights
  @weights.each do |token, weight|
    if other_weight = other_weights[token]
      similarity += other_weight*weight
    end
  end
  similarity
end

#tokensObject



65
66
67
# File 'lib/fuzzy_tools/weighted_document_tokens.rb', line 65

def tokens
  @tokens ||= @weights.keys
end