Advertisement
Guest User

SpamFilter

a guest
Nov 16th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.io.Serializable;
  2. import java.util.Hashtable;
  3.  
  4. public class SpamFilter implements Serializable
  5. {
  6.  
  7.     private Hashtable badWords;
  8.    
  9.     /**
  10.      * Default constructor, creates a new hash table
  11.      */
  12.     SpamFilter()
  13.     {
  14.         badWords = new Hashtable();
  15.     }
  16.    
  17.     /**
  18.      * Inserts an element into the table
  19.      * @param s
  20.      * Element to insert
  21.      */
  22.     public void insert(String s)
  23.     {
  24.         System.out.println("here");
  25.         badWords.put(s.toLowerCase(), s.toLowerCase());
  26.     }
  27.    
  28.     /**
  29.      * Removes an element from the table
  30.      * @param word
  31.      * Element to remove from table
  32.      * @throws ElementNotFoundException
  33.      * Thrown if the element is not found in the table.
  34.      */
  35.     public void remove(String word) throws ElementNotFoundException
  36.     {
  37.         Object removed = badWords.remove(word.toLowerCase());
  38.         if (removed == null)
  39.           throw new ElementNotFoundException(word + " was not found in"
  40.               + " the list of bad words");
  41.     }
  42.     /**
  43.      * Determines if a bad word exists in the table
  44.      * @param checkMe
  45.      * element to locate
  46.      * @return
  47.      * true if found, false otherwise
  48.      */
  49.     public boolean isBadWord(String checkMe)
  50.     {
  51.             return badWords.containsKey(checkMe.toLowerCase());
  52.     }
  53.     /**
  54.      * Checks an email message for bad words and counts them. Returns a ratio of
  55.      * bad words to total words
  56.      * @param message
  57.      * The email to be analyzed
  58.      * @return
  59.      * The ratio of bad words to total words
  60.      */
  61.     public float checkEmail(String message)
  62.     {
  63.         String[] words = message.toLowerCase().split("[^a-zA-Z]");
  64.         int badWordCount = 0;
  65.         for (int i = words.length - 1; i > 0; --i)
  66.         {
  67.             if (isBadWord(words[i])) ++badWordCount;
  68.         }
  69.         return (float) badWordCount / (float) words.length;
  70.     }
  71.    
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement