~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/analysis/TestNGramFilters.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.solr.analysis;
2
 
 
3
 
/**
4
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
5
 
 * contributor license agreements.  See the NOTICE file distributed with
6
 
 * this work for additional information regarding copyright ownership.
7
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
8
 
 * (the "License"); you may not use this file except in compliance with
9
 
 * the License.  You may obtain a copy of the License at
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
13
 
 * Unless required by applicable law or agreed to in writing, software
14
 
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
 * See the License for the specific language governing permissions and
17
 
 * limitations under the License.
18
 
 */
19
 
 
20
 
import java.io.Reader;
21
 
import java.io.StringReader;
22
 
import java.util.HashMap;
23
 
import java.util.Map;
24
 
 
25
 
import org.apache.lucene.analysis.MockTokenizer;
26
 
import org.apache.lucene.analysis.TokenStream;
27
 
import org.apache.lucene.analysis.Tokenizer;
28
 
 
29
 
/**
30
 
 * Simple tests to ensure the NGram filter factories are working.
31
 
 */
32
 
public class TestNGramFilters extends BaseTokenTestCase {
33
 
  /**
34
 
   * Test NGramTokenizerFactory
35
 
   */
36
 
  public void testNGramTokenizer() throws Exception {
37
 
    Reader reader = new StringReader("test");
38
 
    Map<String,String> args = new HashMap<String,String>();
39
 
    NGramTokenizerFactory factory = new NGramTokenizerFactory();
40
 
    factory.init(args);
41
 
    Tokenizer stream = factory.create(reader);
42
 
    assertTokenStreamContents(stream, 
43
 
        new String[] { "t", "e", "s", "t", "te", "es", "st" });
44
 
  }
45
 
  /**
46
 
   * Test NGramTokenizerFactory with min and max gram options
47
 
   */
48
 
  public void testNGramTokenizer2() throws Exception {
49
 
    Reader reader = new StringReader("test");
50
 
    Map<String,String> args = new HashMap<String,String>();
51
 
    args.put("minGramSize", "2");
52
 
    args.put("maxGramSize", "3");
53
 
    NGramTokenizerFactory factory = new NGramTokenizerFactory();
54
 
    factory.init(args);
55
 
    Tokenizer stream = factory.create(reader);
56
 
    assertTokenStreamContents(stream, 
57
 
        new String[] { "te", "es", "st", "tes", "est" });
58
 
  }
59
 
  /**
60
 
   * Test the NGramFilterFactory
61
 
   */
62
 
  public void testNGramFilter() throws Exception {
63
 
    Reader reader = new StringReader("test");
64
 
    Map<String,String> args = new HashMap<String,String>();
65
 
    NGramFilterFactory factory = new NGramFilterFactory();
66
 
    factory.init(args);
67
 
    TokenStream stream = factory.create(new MockTokenizer(reader, MockTokenizer.WHITESPACE, false));
68
 
    assertTokenStreamContents(stream, 
69
 
        new String[] { "t", "e", "s", "t", "te", "es", "st" });
70
 
  }
71
 
  /**
72
 
   * Test the NGramFilterFactory with min and max gram options
73
 
   */
74
 
  public void testNGramFilter2() throws Exception {
75
 
    Reader reader = new StringReader("test");
76
 
    Map<String,String> args = new HashMap<String,String>();
77
 
    args.put("minGramSize", "2");
78
 
    args.put("maxGramSize", "3");
79
 
    NGramFilterFactory factory = new NGramFilterFactory();
80
 
    factory.init(args);
81
 
    TokenStream stream = factory.create(new MockTokenizer(reader, MockTokenizer.WHITESPACE, false));
82
 
    assertTokenStreamContents(stream, 
83
 
        new String[] { "te", "es", "st", "tes", "est" });
84
 
  }
85
 
  /**
86
 
   * Test EdgeNGramTokenizerFactory
87
 
   */
88
 
  public void testEdgeNGramTokenizer() throws Exception {
89
 
    Reader reader = new StringReader("test");
90
 
    Map<String,String> args = new HashMap<String,String>();
91
 
    EdgeNGramTokenizerFactory factory = new EdgeNGramTokenizerFactory();
92
 
    factory.init(args);
93
 
    Tokenizer stream = factory.create(reader);
94
 
    assertTokenStreamContents(stream, 
95
 
        new String[] { "t" });
96
 
  }
97
 
  /**
98
 
   * Test EdgeNGramTokenizerFactory with min and max gram size
99
 
   */
100
 
  public void testEdgeNGramTokenizer2() throws Exception {
101
 
    Reader reader = new StringReader("test");
102
 
    Map<String,String> args = new HashMap<String,String>();
103
 
    args.put("minGramSize", "1");
104
 
    args.put("maxGramSize", "2");
105
 
    EdgeNGramTokenizerFactory factory = new EdgeNGramTokenizerFactory();
106
 
    factory.init(args);
107
 
    Tokenizer stream = factory.create(reader);
108
 
    assertTokenStreamContents(stream, 
109
 
        new String[] { "t", "te" });
110
 
  }
111
 
  /**
112
 
   * Test EdgeNGramTokenizerFactory with side option
113
 
   */
114
 
  public void testEdgeNGramTokenizer3() throws Exception {
115
 
    Reader reader = new StringReader("ready");
116
 
    Map<String,String> args = new HashMap<String,String>();
117
 
    args.put("side", "back");
118
 
    EdgeNGramTokenizerFactory factory = new EdgeNGramTokenizerFactory();
119
 
    factory.init(args);
120
 
    Tokenizer stream = factory.create(reader);
121
 
    assertTokenStreamContents(stream, 
122
 
        new String[] { "y" });
123
 
  }
124
 
  /**
125
 
   * Test EdgeNGramFilterFactory
126
 
   */
127
 
  public void testEdgeNGramFilter() throws Exception {
128
 
    Reader reader = new StringReader("test");
129
 
    Map<String,String> args = new HashMap<String,String>();
130
 
    EdgeNGramFilterFactory factory = new EdgeNGramFilterFactory();
131
 
    factory.init(args);
132
 
    TokenStream stream = factory.create(new MockTokenizer(reader, MockTokenizer.WHITESPACE, false));
133
 
    assertTokenStreamContents(stream, 
134
 
        new String[] { "t" });
135
 
  }
136
 
  /**
137
 
   * Test EdgeNGramFilterFactory with min and max gram size
138
 
   */
139
 
  public void testEdgeNGramFilter2() throws Exception {
140
 
    Reader reader = new StringReader("test");
141
 
    Map<String,String> args = new HashMap<String,String>();
142
 
    args.put("minGramSize", "1");
143
 
    args.put("maxGramSize", "2");
144
 
    EdgeNGramFilterFactory factory = new EdgeNGramFilterFactory();
145
 
    factory.init(args);
146
 
    TokenStream stream = factory.create(new MockTokenizer(reader, MockTokenizer.WHITESPACE, false));
147
 
    assertTokenStreamContents(stream, 
148
 
        new String[] { "t", "te" });
149
 
  }
150
 
  /**
151
 
   * Test EdgeNGramFilterFactory with side option
152
 
   */
153
 
  public void testEdgeNGramFilter3() throws Exception {
154
 
    Reader reader = new StringReader("ready");
155
 
    Map<String,String> args = new HashMap<String,String>();
156
 
    args.put("side", "back");
157
 
    EdgeNGramFilterFactory factory = new EdgeNGramFilterFactory();
158
 
    factory.init(args);
159
 
    TokenStream stream = factory.create(new MockTokenizer(reader, MockTokenizer.WHITESPACE, false));
160
 
    assertTokenStreamContents(stream, 
161
 
        new String[] { "y" });
162
 
  }
163
 
}