Class: Lda::Backends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lda-ruby/backends/base.rb

Direct Known Subclasses

Native, PureRuby, Rust

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(random_seed: nil) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lda-ruby/backends/base.rb', line 17

def initialize(random_seed: nil)
  @random = random_seed.nil? ? Random.new : Random.new(random_seed)

  @max_iter = 20
  @convergence = 1e-6
  @em_max_iter = 100
  @em_convergence = 1e-4
  @num_topics = 20
  @init_alpha = 0.3
  @est_alpha = 1
  @verbose = true

  @corpus = nil
end

Instance Attribute Details

#convergenceObject

Returns the value of attribute convergence.



8
9
10
# File 'lib/lda-ruby/backends/base.rb', line 8

def convergence
  @convergence
end

#corpusObject

Returns the value of attribute corpus.



6
7
8
# File 'lib/lda-ruby/backends/base.rb', line 6

def corpus
  @corpus
end

#em_convergenceObject

Returns the value of attribute em_convergence.



8
9
10
# File 'lib/lda-ruby/backends/base.rb', line 8

def em_convergence
  @em_convergence
end

#em_max_iterObject

Returns the value of attribute em_max_iter.



8
9
10
# File 'lib/lda-ruby/backends/base.rb', line 8

def em_max_iter
  @em_max_iter
end

#est_alphaObject

Returns the value of attribute est_alpha.



8
9
10
# File 'lib/lda-ruby/backends/base.rb', line 8

def est_alpha
  @est_alpha
end

#init_alphaObject

Returns the value of attribute init_alpha.



8
9
10
# File 'lib/lda-ruby/backends/base.rb', line 8

def init_alpha
  @init_alpha
end

#max_iterObject

Returns the value of attribute max_iter.



8
9
10
# File 'lib/lda-ruby/backends/base.rb', line 8

def max_iter
  @max_iter
end

#num_topicsObject

Returns the value of attribute num_topics.



8
9
10
# File 'lib/lda-ruby/backends/base.rb', line 8

def num_topics
  @num_topics
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/lda-ruby/backends/base.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#betaObject

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/lda-ruby/backends/base.rb', line 90

def beta
  raise NotImplementedError, "#{self.class} must implement #beta"
end

#compute_phiObject

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/lda-ruby/backends/base.rb', line 98

def compute_phi
  raise NotImplementedError, "#{self.class} must implement #compute_phi"
end

#em(_start) ⇒ Object

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/lda-ruby/backends/base.rb', line 86

def em(_start)
  raise NotImplementedError, "#{self.class} must implement #em"
end

#fast_load_corpus_from_file(filename) ⇒ Object



41
42
43
# File 'lib/lda-ruby/backends/base.rb', line 41

def fast_load_corpus_from_file(filename)
  self.corpus = Lda::DataCorpus.new(filename)
end

#gammaObject

Raises:

  • (NotImplementedError)


94
95
96
# File 'lib/lda-ruby/backends/base.rb', line 94

def gamma
  raise NotImplementedError, "#{self.class} must implement #gamma"
end

#load_settings(settings_file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lda-ruby/backends/base.rb', line 45

def load_settings(settings_file)
  File.readlines(settings_file).each do |line|
    next if line.strip.empty? || line.strip.start_with?("#")

    key, value = line.split(/\s+/, 2)
    next if value.nil?

    case key.downcase
    when "max_iter", "var_max_iter"
      self.max_iter = value.to_i
    when "convergence", "var_converged"
      self.convergence = value.to_f
    when "em_max_iter"
      self.em_max_iter = value.to_i
    when "em_convergence", "em_converged"
      self.em_convergence = value.to_f
    when "num_topics", "ntopics"
      self.num_topics = value.to_i
    when "init_alpha", "initial_alpha", "alpha"
      self.init_alpha = value.to_f
    when "est_alpha", "estimate_alpha"
      self.est_alpha = value.to_i
    when "verbose"
      self.verbose = value.to_i != 0
    end
  end

  true
end

#modelObject

Raises:

  • (NotImplementedError)


102
103
104
# File 'lib/lda-ruby/backends/base.rb', line 102

def model
  raise NotImplementedError, "#{self.class} must implement #model"
end

#nameObject



32
33
34
# File 'lib/lda-ruby/backends/base.rb', line 32

def name
  self.class.name.split("::").last.downcase
end

#set_config(init_alpha, num_topics, max_iter, convergence, em_max_iter, em_convergence, est_alpha) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/lda-ruby/backends/base.rb', line 75

def set_config(init_alpha, num_topics, max_iter, convergence, em_max_iter, em_convergence, est_alpha)
  self.init_alpha = init_alpha
  self.num_topics = num_topics
  self.max_iter = max_iter
  self.convergence = convergence
  self.em_max_iter = em_max_iter
  self.em_convergence = em_convergence
  self.est_alpha = est_alpha
  true
end

#topic_document_probability(_phi_matrix, _document_counts) ⇒ Object



106
107
108
# File 'lib/lda-ruby/backends/base.rb', line 106

def topic_document_probability(_phi_matrix, _document_counts)
  nil
end