1 package org.apache.lucene.analysis; 2 3 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 93 | Popular Tags |