Class: NBayes::Vocab

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Vocab

Returns a new instance of Vocab.



18
19
20
21
22
# File 'lib/nbayes.rb', line 18

def initialize(options = {})
  @tokens = Hash.new
  # for smoothing, use log of vocab size, rather than vocab size
  @log_size = options[:log_size]
end

Instance Attribute Details

#log_sizeObject

Returns the value of attribute log_size.



16
17
18
# File 'lib/nbayes.rb', line 16

def log_size
  @log_size
end

#tokensObject

Returns the value of attribute tokens.



16
17
18
# File 'lib/nbayes.rb', line 16

def tokens
  @tokens
end

Instance Method Details

#delete(token) ⇒ Object



24
25
26
# File 'lib/nbayes.rb', line 24

def delete(token)
  tokens.delete(token)
end

#each(&block) ⇒ Object



28
29
30
# File 'lib/nbayes.rb', line 28

def each(&block)
  tokens.keys.each(&block)
end

#seen_token(token) ⇒ Object



40
41
42
# File 'lib/nbayes.rb', line 40

def seen_token(token)
  tokens[token] = 1
end

#sizeObject



32
33
34
35
36
37
38
# File 'lib/nbayes.rb', line 32

def size
  if log_size
    Math.log(tokens.count)
  else
    tokens.count
  end
end