Class: Swallow::SpamDetector
- Inherits:
-
Object
- Object
- Swallow::SpamDetector
- Defined in:
- lib/spamdetector.rb
Direct Known Subclasses
Instance Method Summary collapse
- #generate_cmd(params) ⇒ Object
-
#initialize ⇒ SpamDetector
constructor
A new instance of SpamDetector.
- #is_innocent?(message_text) ⇒ Boolean
- #is_spam?(message_text) ⇒ Boolean
- #process_folder(cmd, folder, should_delete = false) ⇒ Object
- #train_and_clean_folder(folder) ⇒ Object
- #train_and_retain_folder(folder) ⇒ Object
-
#train_emails_as_innocent(emails) ⇒ Object
this function takes an array of email objects and trains them as innocent.
-
#train_emails_as_spam(emails) ⇒ Object
this function takes an array of email objects and trains them as spam.
Constructor Details
#initialize ⇒ SpamDetector
Returns a new instance of SpamDetector.
20 21 22 23 |
# File 'lib/spamdetector.rb', line 20 def initialize @innocent_cmd = "cat" @training_cmd = "cat" end |
Instance Method Details
#generate_cmd(params) ⇒ Object
47 48 49 50 51 |
# File 'lib/spamdetector.rb', line 47 def generate_cmd(params) if params.kind_of? Hash return "echo" end end |
#is_innocent?(message_text) ⇒ Boolean
64 65 |
# File 'lib/spamdetector.rb', line 64 def is_innocent?() end |
#is_spam?(message_text) ⇒ Boolean
61 62 |
# File 'lib/spamdetector.rb', line 61 def is_spam?() end |
#process_folder(cmd, folder, should_delete = false) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/spamdetector.rb', line 25 def process_folder(cmd, folder, should_delete = false) count = 0 if folder.kind_of? MailDirFolder count = folder.emails.length folder.emails.each { |email| pipe(cmd, email.to_s) email.delete if should_delete == true } end if folder.kind_of? String Dir.open(folder).each { |f| next if File.directory?(f) File.open(File.join(folder,f)) do |out| pipe(cmd, out.read) end count += 1 File.delete(File.join(folder,f)) if should_delete == true } end puts "Processed #{count} messages in #{folder}" end |
#train_and_clean_folder(folder) ⇒ Object
53 54 55 |
# File 'lib/spamdetector.rb', line 53 def train_and_clean_folder(folder) process_folder(generate_cmd(:source => "corpus", :class => "spam"), folder, true) end |
#train_and_retain_folder(folder) ⇒ Object
57 58 59 |
# File 'lib/spamdetector.rb', line 57 def train_and_retain_folder(folder) process_folder(generate_cmd(:source => "corpus", :class => "innocent"), folder) end |
#train_emails_as_innocent(emails) ⇒ Object
this function takes an array of email objects and trains them as innocent
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/spamdetector.rb', line 68 def train_emails_as_innocent(emails) if emails != nil then if emails.kind_of? Array then emails.each { |email| if email.kind_of? Email then pipe(generate_cmd(:source => "corpus", :class => "innocent"), email.to_s) end } puts "Trained #{emails.length} emails as innocent" end end end |
#train_emails_as_spam(emails) ⇒ Object
this function takes an array of email objects and trains them as spam
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/spamdetector.rb', line 82 def train_emails_as_spam(emails) if emails != nil then if emails.kind_of? Array then emails.each { |email| if email.kind_of? Email then pipe(generate_cmd(:source => "corpus", :class => "spam"), email.to_s) end } puts "Trained #{emails.length} emails as spam" end end end |