import java.io.Serializable;
import java.util.Hashtable;
public class SpamFilter implements Serializable
{
private Hashtable badWords;
/**
* Default constructor, creates a new hash table
*/
SpamFilter()
{
badWords = new Hashtable();
}
/**
* Inserts an element into the table
* @param s
* Element to insert
*/
public void insert(String s)
{
System.out.println("here");
badWords.put(s.toLowerCase(), s.toLowerCase());
}
/**
* Removes an element from the table
* @param word
* Element to remove from table
* @throws ElementNotFoundException
* Thrown if the element is not found in the table.
*/
public void remove(String word) throws ElementNotFoundException
{
Object removed = badWords.remove(word.toLowerCase());
if (removed == null)
throw new ElementNotFoundException(word + " was not found in"
+ " the list of bad words");
}
/**
* Determines if a bad word exists in the table
* @param checkMe
* element to locate
* @return
* true if found, false otherwise
*/
public boolean isBadWord(String checkMe)
{
return badWords.containsKey(checkMe.toLowerCase());
}
/**
* Checks an email message for bad words and counts them. Returns a ratio of
* bad words to total words
* @param message
* The email to be analyzed
* @return
* The ratio of bad words to total words
*/
public float checkEmail(String message)
{
String[] words = message.toLowerCase().split("[^a-zA-Z]");
int badWordCount = 0;
for (int i = words.length - 1; i > 0; --i)
{
if (isBadWord(words[i])) ++badWordCount;
}
return (float) badWordCount / (float) words.length;
}
}