Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » analysis » [javadoc | source]
    1   package org.apache.lucene.analysis;
    2   
    3   /**
    4    * Copyright 2004 The Apache Software Foundation
    5    *
    6    * Licensed under the Apache License, Version 2.0 (the "License");
    7    * you may not use this file except in compliance with the License.
    8    * You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing, software
   13    * distributed under the License is distributed on an "AS IS" BASIS,
   14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    * See the License for the specific language governing permissions and
   16    * limitations under the License.
   17    */
   18   
   19   import junit.framework.TestCase;
   20   
   21   import java.io.StringReader;
   22   import java.io.IOException;
   23   import java.util.Set;
   24   import java.util.HashSet;
   25   
   26   public class TestStopAnalyzer extends TestCase {
   27     private StopAnalyzer stop = new StopAnalyzer();
   28   
   29     private Set inValidTokens = new HashSet();
   30     public TestStopAnalyzer(String s) {
   31       super(s);
   32     }
   33   
   34     protected void setUp() {
   35       for (int i = 0; i < StopAnalyzer.ENGLISH_STOP_WORDS.length; i++) {
   36         inValidTokens.add(StopAnalyzer.ENGLISH_STOP_WORDS[i]);
   37       }
   38     }
   39   
   40     public void testDefaults() {
   41       assertTrue(stop != null);
   42       StringReader reader = new StringReader("This is a test of the english stop analyzer");
   43       TokenStream stream = stop.tokenStream("test", reader);
   44       assertTrue(stream != null);
   45       Token token = null;
   46       try {
   47         while ((token = stream.next()) != null)
   48         {
   49           assertTrue(inValidTokens.contains(token.termText()) == false);
   50         }
   51       } catch (IOException e) {
   52         assertTrue(false);
   53       }
   54     }
   55   
   56     public void testStopList() {
   57       Set stopWordsSet = new HashSet();
   58       stopWordsSet.add("good");
   59       stopWordsSet.add("test");
   60       stopWordsSet.add("analyzer");
   61       StopAnalyzer newStop = new StopAnalyzer((String[])stopWordsSet.toArray(new String[3]));
   62       StringReader reader = new StringReader("This is a good test of the english stop analyzer");
   63       TokenStream stream = newStop.tokenStream("test", reader);
   64       assertTrue(stream != null);
   65       Token token = null;
   66       try {
   67         while ((token = stream.next()) != null)
   68         {
   69           String text = token.termText();
   70           assertTrue(stopWordsSet.contains(text) == false);
   71         }
   72       } catch (IOException e) {
   73         assertTrue(false);
   74       }
   75     }
   76   }

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » analysis » [javadoc | source]