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 java.io;
   20   import junit.framework;
   21   
   22   import org.apache.lucene;
   23   import org.apache.lucene.analysis;
   24   
   25   public class TestAnalyzers extends TestCase {
   26   
   27      public TestAnalyzers(String name) {
   28         super(name);
   29      }
   30   
   31     public void assertAnalyzesTo(Analyzer a, 
   32                                  String input, 
   33                                  String[] output) throws Exception {
   34       TokenStream ts = a.tokenStream("dummy", new StringReader(input));
   35       for (int i=0; i<output.length; i++) {
   36         Token t = ts.next();
   37         assertNotNull(t);
   38         assertEquals(t.termText(), output[i]);
   39       }
   40       assertNull(ts.next());
   41       ts.close();
   42     }
   43   
   44     public void testSimple() throws Exception {
   45       Analyzer a = new SimpleAnalyzer();
   46       assertAnalyzesTo(a, "foo bar FOO BAR", 
   47                        new String[] { "foo", "bar", "foo", "bar" });
   48       assertAnalyzesTo(a, "foo      bar .  FOO <> BAR", 
   49                        new String[] { "foo", "bar", "foo", "bar" });
   50       assertAnalyzesTo(a, "foo.bar.FOO.BAR", 
   51                        new String[] { "foo", "bar", "foo", "bar" });
   52       assertAnalyzesTo(a, "U.S.A.", 
   53                        new String[] { "u", "s", "a" });
   54       assertAnalyzesTo(a, "C++", 
   55                        new String[] { "c" });
   56       assertAnalyzesTo(a, "B2B", 
   57                        new String[] { "b", "b" });
   58       assertAnalyzesTo(a, "2B", 
   59                        new String[] { "b" });
   60       assertAnalyzesTo(a, "\"QUOTED\" word", 
   61                        new String[] { "quoted", "word" });
   62     }
   63   
   64     public void testNull() throws Exception {
   65       Analyzer a = new WhitespaceAnalyzer();
   66       assertAnalyzesTo(a, "foo bar FOO BAR", 
   67                        new String[] { "foo", "bar", "FOO", "BAR" });
   68       assertAnalyzesTo(a, "foo      bar .  FOO <> BAR", 
   69                        new String[] { "foo", "bar", ".", "FOO", "<>", "BAR" });
   70       assertAnalyzesTo(a, "foo.bar.FOO.BAR", 
   71                        new String[] { "foo.bar.FOO.BAR" });
   72       assertAnalyzesTo(a, "U.S.A.", 
   73                        new String[] { "U.S.A." });
   74       assertAnalyzesTo(a, "C++", 
   75                        new String[] { "C++" });
   76       assertAnalyzesTo(a, "B2B", 
   77                        new String[] { "B2B" });
   78       assertAnalyzesTo(a, "2B", 
   79                        new String[] { "2B" });
   80       assertAnalyzesTo(a, "\"QUOTED\" word", 
   81                        new String[] { "\"QUOTED\"", "word" });
   82     }
   83   
   84     public void testStop() throws Exception {
   85       Analyzer a = new StopAnalyzer();
   86       assertAnalyzesTo(a, "foo bar FOO BAR", 
   87                        new String[] { "foo", "bar", "foo", "bar" });
   88       assertAnalyzesTo(a, "foo a bar such FOO THESE BAR", 
   89                        new String[] { "foo", "bar", "foo", "bar" });
   90     }
   91   }
   92   

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