Advertisement
Guest User

AspellFilter for Apache Solr

a guest
Jan 22nd, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package com.mimirtech.search.solr.analysis;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.lucene.analysis.Token;
  6. import org.apache.lucene.analysis.TokenFilter;
  7. import org.apache.lucene.analysis.TokenStream;
  8.  
  9. public class AspellFilter extends TokenFilter
  10. {
  11.     static
  12.     {
  13.         try
  14.             {
  15.                 System.loadLibrary( "java_aspell" );
  16.             }
  17.         catch( UnsatisfiedLinkError e )
  18.             {
  19.                 System.loadLibrary( "/usr/local/lib/", "java_aspell" );
  20.                 //System.err.println( "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
  21.                 //System.exit( 1 );
  22.             }
  23.     }
  24.  
  25.     com.mimirtech.speller.aspell.Suggest fsuggest;
  26.  
  27.     public AspellFilter(TokenStream input) throws java.lang.Exception
  28.     {
  29.         super( input );
  30.         fsuggest = new com.mimirtech.speller.aspell.Suggest( "en_SG", "utf-8",
  31.                                                              "", "60" );
  32.     }
  33.     /*********************************************************************/
  34.     public AspellFilter(TokenStream input, String lang, String encoding,
  35.                         String jargon, String size) throws java.lang.Exception
  36.     {
  37.         super( input );
  38.         fsuggest = new com.mimirtech.speller.aspell.Suggest( lang, encoding,
  39.                                                              jargon, size );
  40.     }
  41.     /*********************************************************************/
  42.     public Token next() throws IOException
  43.     {
  44.         return parseToken( this.input.next() );
  45.     }
  46.     /*********************************************************************/
  47.     public Token next(Token result) throws IOException
  48.     {
  49.         return parseToken( this.input.next() );
  50.     }
  51.     /*********************************************************************/
  52.     protected Token parseToken(Token in)
  53.     {
  54.         if( in != null )
  55.             {
  56.                 // FIXME: Alter the char[] from TermBuffer directly to
  57.                 // improve efficiency.
  58.                 // See http://lucene.apache.org/java/2_3_2/api/org/apache/lucene/analysis/Token.html#setTermBuffer(char[],%20int,%20int)
  59.                 String text = String.valueOf( in.termBuffer() );
  60.                 String soundslike = fsuggest.SoundsLike( text );
  61.                 in.setTermBuffer( soundslike, 0, soundslike.length() );
  62.             }
  63.         return in;
  64.     }
  65.     /*********************************************************************/
  66. }  // public class AspellFilter
  67. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@ END OF FILE @@@@@@@@@@@@@@@@@@@@@@@@@@*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement