Class: Documentally::Document
- Inherits:
-
Object
- Object
- Documentally::Document
- Defined in:
- lib/documentally/document.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#term_hash ⇒ Object
Returns the value of attribute term_hash.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #frequency(term) ⇒ Object
-
#initialize(name, terms) ⇒ Document
constructor
A new instance of Document.
- #normalize!(corpus) ⇒ Object
- #similarity(query) ⇒ Object
- #terms ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, terms) ⇒ Document
Returns a new instance of Document.
5 6 7 8 9 10 11 12 |
# File 'lib/documentally/document.rb', line 5 def initialize(name, terms) @name = name @term_hash = Hash.new(0.0) terms.each do |term| @term_hash[term] += 1 end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/documentally/document.rb', line 3 def name @name end |
#term_hash ⇒ Object
Returns the value of attribute term_hash.
2 3 4 |
# File 'lib/documentally/document.rb', line 2 def term_hash @term_hash end |
Instance Method Details
#==(other) ⇒ Object
26 27 28 29 |
# File 'lib/documentally/document.rb', line 26 def ==(other) union_of_terms = (terms + other.terms).uniq union_of_terms.all? { |term| frequency(term) == other.frequency(term) } end |
#frequency(term) ⇒ Object
22 23 24 |
# File 'lib/documentally/document.rb', line 22 def frequency(term) term_hash[term] end |
#normalize!(corpus) ⇒ Object
35 36 37 38 39 |
# File 'lib/documentally/document.rb', line 35 def normalize!(corpus) terms.each do |term| term_hash[term] /= corpus.frequency(term) end end |
#similarity(query) ⇒ Object
31 32 33 |
# File 'lib/documentally/document.rb', line 31 def similarity(query) terms.map { |term| frequency(term) * query.frequency(term) }.inject(&:+) end |
#terms ⇒ Object
18 19 20 |
# File 'lib/documentally/document.rb', line 18 def terms term_hash.keys end |
#to_s ⇒ Object
14 15 16 |
# File 'lib/documentally/document.rb', line 14 def to_s name.to_s end |