Knowledgebase Home Page  >  RapidSpell Web Java
Search the Knowledge Base
How can I customize the parser and/or checker engine in order to create an offensive word checker?
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=121&catId=45

Options

Print this page
Email this to a friend

This attached project shows how to customize the behavior of the spell checker, to turn it into a swear word checker.

 

At the core is the FlandersParser class, which looks for 'offensive' words (in our example, Flanderisms).  This class implements com.keyoti.rapidSpell.ICheckerEngine making it useable in place of the RapidSpellChecker class, in the RapidSpellCheckerPopUp.jsp with the following code;

 

<%

OffensiveWordChecker.FlandersParser parser = new OffensiveWordChecker.FlandersParser();

%>

<RapidSpellWeb:rapidSpellWeb checkerEngine="<%= parser %>"/>

 

The FlandersParser class is fairly simple to follow; essentially it separates a string in to words and then checks a HashMap to see if each word is disallowed.  Of course the implementation can be modified to suit.

 

If you have any questions about this, please email support@keyoti.com

 

ADDENDUM

This modified version of the FlandersParser class will spell check and check for offensive words at the same time.  A license key must be passed to the RapidSpellChecker constructor in order for it to function properly.

package OffensiveWordChecker;

 

import com.keyoti.rapidSpell.AdvancedTextBoundary;

import com.keyoti.rapidSpell.BadWord;

import com.keyoti.rapidSpell.*;

import java.util.Enumeration;

import java.util.HashMap;

 

import java.util.Vector;

 

public class FlandersParser extends CheckerEngineAdapter implements com.keyoti.rapidSpell.ICheckerEngine {

 

 

      HashMap dictionary = new HashMap();

      String theText, suggestion,suggestion2 = null;

      RapidSpellChecker rc = new RapidSpellChecker("<<<YOUR LICENSE KEY HERE>>>>");

      Vector vectorSuggestions = new Vector(20, 1);

 

      Enumeration suggestions;

 

      int wordStart, wordEnd;

 

      AdvancedTextBoundary wordIterator = new AdvancedTextBoundary();

    public void setUserDictionary(com.keyoti.rapidSpell.UserDictionary ud){ rc.setUserDictionary(ud); }

    public com.keyoti.rapidSpell.UserDictionary getUserDictionary(){ return rc.getUserDictionary(); }

   

    public boolean addWord(String word){

       

        return rc.addWord(word);

    }

      public FlandersParser(){

            //load dictionary of flanderisms, a real offensive word parser would of course

            //have a list of swears here.

            dictionary.put("gosh", "****");

            dictionary.put("darn", "****");

            dictionary.put("co-winky-dink", "**-*****-****");

            dictionary.put("doodle", "*****");

            dictionary.put("neighboreeny", "neighbor");

            dictionary.put("hi-diddly-ho", "hello");

 

      }

 

      public void check(String text){

            theText = text;

            wordIterator.setText(text);

 

      }

 

 

 

 

 

      public BadWord nextBadWord(){

            String nextWord = GetNextWord();

            int currentBadWordStart = -1;

            int currentBadWordEnd = -1;

            if (nextWord==null) return null;

            else

            {

 

                  //see if next word is bad or if we are to ignore this word, if not scan for next bad word

                  while(!dictionary.containsKey(nextWord)&&

                              rc.lookUp(nextWord))

                  {

                        nextWord = GetNextWord();

                        //ran out of words

                        if (nextWord==null) return null;

                  }

                  currentBadWordStart = wordStart;

                  currentBadWordEnd = wordEnd;

                  suggestions = null;

                  suggestion=null;

                  if (dictionary.containsKey(nextWord)){

                        suggestion = (String) dictionary.get(nextWord);

 

                  }else {

                   

                        vectorSuggestions=rc.findSuggestions(nextWord);

                   

                  }

 

            }

            return new com.keyoti.rapidSpell.BadWord(nextWord,currentBadWordStart,currentBadWordEnd);

      }

 

 

 

      /// Returns the next word in the text.

      /// <returns>String the next word in the text</returns>

      String GetNextWord()

      {

 

            wordStart = wordEnd;

            if(wordEnd < wordIterator.last())

            {

                  wordEnd = wordIterator.following( wordEnd );

 

                  if(wordEnd-wordStart <= 0

                        || !Character.isLetterOrDigit( theText.charAt(wordStart) )

                        )

                  {

 

                        //ignore and move on to next word

                        return GetNextWord();

                  }

 

                  //return as a word

                  return theText.toString().substring(wordStart, wordEnd );

            }

            else

            {

                  return null;

            }

      }

 

 

 

      public Vector findSuggestions(){

              Vector s = new Vector();

              if(suggestion != null){

                   s.add(suggestion);

                   return s;

              }else if(vectorSuggestions !=null){

                  return vectorSuggestions;

              }else return null;

      }

 

 

 

 

      //@Override

      public boolean getAllowAnyCase() {

            // TODO Auto-generated method stub

            return false;

      }

 

      //@Override

      public boolean getIgnoreWordsWithDigits() {

            // TODO Auto-generated method stub

            return false;

      }

 

      //@Override

      public boolean getWarnDuplicates() {

            // TODO Auto-generated method stub

            return false;

      }

 

      //@Override

      public void setAllowAnyCase(boolean arg0) {

            // TODO Auto-generated method stub

 

      }

 

      //@Override

      public void setIgnoreWordsWithDigits(boolean arg0) {

            // TODO Auto-generated method stub

 

      }

 

      //@Override

      public void setMaximumAnagramLength(int arg0) {

            // TODO Auto-generated method stub

 

      }

 

      //@Override

      public void setWarnDuplicates(boolean arg0) {

            // TODO Auto-generated method stub

 

      }

 

}

 

 

 


Related Questions:

Attachments: