Class: Bayes::FilterBase

Inherits:
Object
  • Object
show all
Defined in:
lib/bayes.rb,
lib/bayes/convert.rb

Direct Known Subclasses

PaulGraham, PlainBayes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_name = nil, charset = nil) ⇒ FilterBase

Returns a new instance of FilterBase.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/bayes.rb', line 138

def initialize(db_name=nil, charset=nil)
	@spam = self.class::Corpus.new
	@ham = self.class::Corpus.new
	@charset = charset

	@db_name = db_name
	if db_name && File.exist?(db_name)
		PStore.new(db_name).transaction(true) do |db|
			@spam = db["spam"]
			@ham = db["ham"]
			@charset = db["charset"]
		end
	end
end

Instance Attribute Details

#charsetObject (readonly)

Returns the value of attribute charset.



136
137
138
# File 'lib/bayes.rb', line 136

def charset
  @charset
end

#db_nameObject (readonly)

Returns the value of attribute db_name.



136
137
138
# File 'lib/bayes.rb', line 136

def db_name
  @db_name
end

#hamObject (readonly)

Returns the value of attribute ham.



136
137
138
# File 'lib/bayes.rb', line 136

def ham
  @ham
end

#spamObject (readonly)

Returns the value of attribute spam.



136
137
138
# File 'lib/bayes.rb', line 136

def spam
  @spam
end

Instance Method Details

#[](token) ⇒ Object



165
166
167
# File 'lib/bayes.rb', line 165

def [](token)
	score(token)
end

#convert(to_code, from_code) ⇒ Object



28
29
30
31
32
# File 'lib/bayes/convert.rb', line 28

def convert(to_code, from_code)
	@charset = to_code
	@ham = convert_corpus(@ham, to_code, from_code)
	@spam = convert_corpus(@spam, to_code, from_code)
end

#save(db_name = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/bayes.rb', line 153

def save(db_name=nil)
	db_name ||= @db_name
	@db_name ||= db_name
	return unless @db_name
	PStore.new(@db_name).transaction do |db|
		db["spam"] = @spam
		db["ham"] = @ham
		db["charset"] = @charset
		yield(db) if block_given?
	end
end