Welcome Guest: Sign InRegister

Inversoft Profanity Filter Documentation | Fast Filter

The Fast Filter

This guide will help you create a Fast Filter by hand instead of using the ProfanityFilterFactory. Creating a filter by hand allows you to have finer grained control over the performance and functionality of the filter.

The com.inversoft.profanity.FastProfanityFilter class uses a linear search algorithm that provides nearly the same accuracy as the Regex Filter, but in nearly linear time. This filter scans the text being searched only once and incrementally filters profanity. This filter is the best choice for most situations where speed is of the upmost importance.

This filter uses the same interfaces as the Regex Filter. The documentation for the Regex Filter should be used as a starting point to learn how to create instances of the com.inversoft.profanity.ProfanitySource and com.inversoft.profanity.CommonWordSource interfaces. Once you understand how to create those interfaces, return here to learn how to create and use the FastProfanityFilter.

Constructing the Fast Filter

Now that we have all the class required to create the FastProfanityFilter we can construct it like this:

  ProfanitySource profanitySource = new StaticFullXMLProfanitySource(5, "profanity-database-2.0.xml");
  CommonWordSource commonWordSource = new ResourceBundleCommonWordSource();
  ProfanityFilter filter = new FastProfanityFilter(profanitySource, commonWordSource);

Calling the filter

Once the filter has been instantiated it is a simple matter of calling the findProfanity method. Here is an example of calling the method:

  // Example call using JDK 5.0
  String str = getStringFromSomewhere();
  ProfanityResult[] results = filter.findProfanity(str, 4, "Swear", "Slang");

  // using JDK 1.4 this would become
  // ProfanityResult[] results = filter.findProfanity(str, 4, new String[]{"Swear", "Slang"});

  if (results.length > 0) {
     for (int i = 0; i < results.length; i++) {
         System.out.println("Found profanity at " + results[i].getOffset());
     }
  }