Class: FuzzyTools::WeightedDocumentTokens
- Inherits:
-
Object
- Object
- FuzzyTools::WeightedDocumentTokens
- Defined in:
- lib/fuzzy_tools/weighted_document_tokens.rb
Instance Attribute Summary collapse
-
#weights ⇒ Object
readonly
Returns the value of attribute weights.
Instance Method Summary collapse
-
#cosine_similarity(other) ⇒ Object
Rubinius and JRuby.
-
#initialize(tokens, options) ⇒ WeightedDocumentTokens
constructor
A new instance of WeightedDocumentTokens.
- #tokens ⇒ Object
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, ) weight_function = [:weight_function] set_token_weights(tokens, &weight_function) end |
Instance Attribute Details
#weights ⇒ Object (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 |
#tokens ⇒ Object
65 66 67 |
# File 'lib/fuzzy_tools/weighted_document_tokens.rb', line 65 def tokens @tokens ||= @weights.keys end |