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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/analysis/TestSlowSynonymFilter.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
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.analysis;
19
 
 
20
 
import java.io.IOException;
21
 
import java.io.StringReader;
22
 
import java.util.ArrayList;
23
 
import java.util.Arrays;
24
 
import java.util.Collection;
25
 
import java.util.List;
26
 
 
27
 
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
28
 
import org.apache.lucene.analysis.MockTokenizer;
29
 
import org.apache.lucene.analysis.Token;
30
 
import org.apache.lucene.analysis.TokenStream;
31
 
import org.apache.lucene.analysis.Tokenizer;
32
 
import org.apache.lucene.analysis.tokenattributes.*;
33
 
 
34
 
/**
35
 
 * @deprecated Remove this test in Lucene 5.0
36
 
 */
37
 
@Deprecated
38
 
public class TestSlowSynonymFilter extends BaseTokenStreamTestCase {
39
 
 
40
 
  static List<String> strings(String str) {
41
 
    String[] arr = str.split(" ");
42
 
    return Arrays.asList(arr);
43
 
  }
44
 
 
45
 
  static void assertTokenizesTo(SlowSynonymMap dict, String input,
46
 
      String expected[]) throws IOException {
47
 
    Tokenizer tokenizer = new MockTokenizer(new StringReader(input), MockTokenizer.WHITESPACE, false);
48
 
    SlowSynonymFilter stream = new SlowSynonymFilter(tokenizer, dict);
49
 
    assertTokenStreamContents(stream, expected);
50
 
  }
51
 
  
52
 
  static void assertTokenizesTo(SlowSynonymMap dict, String input,
53
 
      String expected[], int posIncs[]) throws IOException {
54
 
    Tokenizer tokenizer = new MockTokenizer(new StringReader(input), MockTokenizer.WHITESPACE, false);
55
 
    SlowSynonymFilter stream = new SlowSynonymFilter(tokenizer, dict);
56
 
    assertTokenStreamContents(stream, expected, posIncs);
57
 
  }
58
 
  
59
 
  static void assertTokenizesTo(SlowSynonymMap dict, List<Token> input,
60
 
      String expected[], int posIncs[])
61
 
      throws IOException {
62
 
    TokenStream tokenizer = new IterTokenStream(input);
63
 
    SlowSynonymFilter stream = new SlowSynonymFilter(tokenizer, dict);
64
 
    assertTokenStreamContents(stream, expected, posIncs);
65
 
  }
66
 
  
67
 
  static void assertTokenizesTo(SlowSynonymMap dict, List<Token> input,
68
 
      String expected[], int startOffsets[], int endOffsets[], int posIncs[])
69
 
      throws IOException {
70
 
    TokenStream tokenizer = new IterTokenStream(input);
71
 
    SlowSynonymFilter stream = new SlowSynonymFilter(tokenizer, dict);
72
 
    assertTokenStreamContents(stream, expected, startOffsets, endOffsets,
73
 
        posIncs);
74
 
  }
75
 
  
76
 
  public void testMatching() throws IOException {
77
 
    SlowSynonymMap map = new SlowSynonymMap();
78
 
 
79
 
    boolean orig = false;
80
 
    boolean merge = true;
81
 
    map.add(strings("a b"), tokens("ab"), orig, merge);
82
 
    map.add(strings("a c"), tokens("ac"), orig, merge);
83
 
    map.add(strings("a"), tokens("aa"), orig, merge);
84
 
    map.add(strings("b"), tokens("bb"), orig, merge);
85
 
    map.add(strings("z x c v"), tokens("zxcv"), orig, merge);
86
 
    map.add(strings("x c"), tokens("xc"), orig, merge);
87
 
 
88
 
    assertTokenizesTo(map, "$", new String[] { "$" });
89
 
    assertTokenizesTo(map, "a", new String[] { "aa" });
90
 
    assertTokenizesTo(map, "a $", new String[] { "aa", "$" });
91
 
    assertTokenizesTo(map, "$ a", new String[] { "$", "aa" });
92
 
    assertTokenizesTo(map, "a a", new String[] { "aa", "aa" });
93
 
    assertTokenizesTo(map, "b", new String[] { "bb" });
94
 
    assertTokenizesTo(map, "z x c v", new String[] { "zxcv" });
95
 
    assertTokenizesTo(map, "z x c $", new String[] { "z", "xc", "$" });
96
 
 
97
 
    // repeats
98
 
    map.add(strings("a b"), tokens("ab"), orig, merge);
99
 
    map.add(strings("a b"), tokens("ab"), orig, merge);
100
 
    
101
 
    // FIXME: the below test intended to be { "ab" }
102
 
    assertTokenizesTo(map, "a b", new String[] { "ab", "ab", "ab"  });
103
 
 
104
 
    // check for lack of recursion
105
 
    map.add(strings("zoo"), tokens("zoo"), orig, merge);
106
 
    assertTokenizesTo(map, "zoo zoo $ zoo", new String[] { "zoo", "zoo", "$", "zoo" });
107
 
    map.add(strings("zoo"), tokens("zoo zoo"), orig, merge);
108
 
    // FIXME: the below test intended to be { "zoo", "zoo", "zoo", "zoo", "$", "zoo", "zoo" }
109
 
    // maybe this was just a typo in the old test????
110
 
    assertTokenizesTo(map, "zoo zoo $ zoo", new String[] { "zoo", "zoo", "zoo", "zoo", "zoo", "zoo", "$", "zoo", "zoo", "zoo" });
111
 
  }
112
 
 
113
 
  public void testIncludeOrig() throws IOException {
114
 
    SlowSynonymMap map = new SlowSynonymMap();
115
 
 
116
 
    boolean orig = true;
117
 
    boolean merge = true;
118
 
    map.add(strings("a b"), tokens("ab"), orig, merge);
119
 
    map.add(strings("a c"), tokens("ac"), orig, merge);
120
 
    map.add(strings("a"), tokens("aa"), orig, merge);
121
 
    map.add(strings("b"), tokens("bb"), orig, merge);
122
 
    map.add(strings("z x c v"), tokens("zxcv"), orig, merge);
123
 
    map.add(strings("x c"), tokens("xc"), orig, merge);
124
 
 
125
 
    assertTokenizesTo(map, "$", 
126
 
        new String[] { "$" },
127
 
        new int[] { 1 });
128
 
    assertTokenizesTo(map, "a", 
129
 
        new String[] { "a", "aa" },
130
 
        new int[] { 1, 0 });
131
 
    assertTokenizesTo(map, "a", 
132
 
        new String[] { "a", "aa" },
133
 
        new int[] { 1, 0 });
134
 
    assertTokenizesTo(map, "$ a", 
135
 
        new String[] { "$", "a", "aa" },
136
 
        new int[] { 1, 1, 0 });
137
 
    assertTokenizesTo(map, "a $", 
138
 
        new String[] { "a", "aa", "$" },
139
 
        new int[] { 1, 0, 1 });
140
 
    assertTokenizesTo(map, "$ a !", 
141
 
        new String[] { "$", "a", "aa", "!" },
142
 
        new int[] { 1, 1, 0, 1 });
143
 
    assertTokenizesTo(map, "a a", 
144
 
        new String[] { "a", "aa", "a", "aa" },
145
 
        new int[] { 1, 0, 1, 0 });
146
 
    assertTokenizesTo(map, "b", 
147
 
        new String[] { "b", "bb" },
148
 
        new int[] { 1, 0 });
149
 
    assertTokenizesTo(map, "z x c v",
150
 
        new String[] { "z", "zxcv", "x", "c", "v" },
151
 
        new int[] { 1, 0, 1, 1, 1 });
152
 
    assertTokenizesTo(map, "z x c $",
153
 
        new String[] { "z", "x", "xc", "c", "$" },
154
 
        new int[] { 1, 1, 0, 1, 1 });
155
 
 
156
 
    // check for lack of recursion
157
 
    map.add(strings("zoo zoo"), tokens("zoo"), orig, merge);
158
 
    // CHECKME: I think the previous test (with 4 zoo's), was just a typo.
159
 
    assertTokenizesTo(map, "zoo zoo $ zoo",
160
 
        new String[] { "zoo", "zoo", "zoo", "$", "zoo" },
161
 
        new int[] { 1, 0, 1, 1, 1 });
162
 
 
163
 
    map.add(strings("zoo"), tokens("zoo zoo"), orig, merge);
164
 
    assertTokenizesTo(map, "zoo zoo $ zoo",
165
 
        new String[] { "zoo", "zoo", "zoo", "$", "zoo", "zoo", "zoo" },
166
 
        new int[] { 1, 0, 1, 1, 1, 0, 1 });
167
 
  }
168
 
 
169
 
 
170
 
  public void testMapMerge() throws IOException {
171
 
    SlowSynonymMap map = new SlowSynonymMap();
172
 
 
173
 
    boolean orig = false;
174
 
    boolean merge = true;
175
 
    map.add(strings("a"), tokens("a5,5"), orig, merge);
176
 
    map.add(strings("a"), tokens("a3,3"), orig, merge);
177
 
 
178
 
    assertTokenizesTo(map, "a",
179
 
        new String[] { "a3", "a5" },
180
 
        new int[] { 1, 2 });
181
 
 
182
 
    map.add(strings("b"), tokens("b3,3"), orig, merge);
183
 
    map.add(strings("b"), tokens("b5,5"), orig, merge);
184
 
 
185
 
    assertTokenizesTo(map, "b",
186
 
        new String[] { "b3", "b5" },
187
 
        new int[] { 1, 2 });
188
 
 
189
 
    map.add(strings("a"), tokens("A3,3"), orig, merge);
190
 
    map.add(strings("a"), tokens("A5,5"), orig, merge);
191
 
    
192
 
    assertTokenizesTo(map, "a",
193
 
        new String[] { "a3", "A3", "a5", "A5" },
194
 
        new int[] { 1, 0, 2, 0 });
195
 
 
196
 
    map.add(strings("a"), tokens("a1"), orig, merge);
197
 
    assertTokenizesTo(map, "a",
198
 
        new String[] { "a1", "a3", "A3", "a5", "A5" },
199
 
        new int[] { 1, 2, 0, 2, 0 });
200
 
 
201
 
    map.add(strings("a"), tokens("a2,2"), orig, merge);
202
 
    map.add(strings("a"), tokens("a4,4 a6,2"), orig, merge);
203
 
    assertTokenizesTo(map, "a",
204
 
        new String[] { "a1", "a2", "a3", "A3", "a4", "a5", "A5", "a6" },
205
 
        new int[] { 1, 1, 1, 0, 1, 1, 0, 1  });
206
 
  }
207
 
 
208
 
 
209
 
  public void testOverlap() throws IOException {
210
 
    SlowSynonymMap map = new SlowSynonymMap();
211
 
 
212
 
    boolean orig = false;
213
 
    boolean merge = true;
214
 
    map.add(strings("qwe"), tokens("qq/ww/ee"), orig, merge);
215
 
    map.add(strings("qwe"), tokens("xx"), orig, merge);
216
 
    map.add(strings("qwe"), tokens("yy"), orig, merge);
217
 
    map.add(strings("qwe"), tokens("zz"), orig, merge);
218
 
    assertTokenizesTo(map, "$", new String[] { "$" });
219
 
    assertTokenizesTo(map, "qwe",
220
 
        new String[] { "qq", "ww", "ee", "xx", "yy", "zz" },
221
 
        new int[] { 1, 0, 0, 0, 0, 0 });
222
 
 
223
 
    // test merging within the map
224
 
 
225
 
    map.add(strings("a"), tokens("a5,5 a8,3 a10,2"), orig, merge);
226
 
    map.add(strings("a"), tokens("a3,3 a7,4 a9,2 a11,2 a111,100"), orig, merge);
227
 
    assertTokenizesTo(map, "a",
228
 
        new String[] { "a3", "a5", "a7", "a8", "a9", "a10", "a11", "a111" },
229
 
        new int[] { 1, 2, 2, 1, 1, 1, 1, 100 });
230
 
  }
231
 
 
232
 
  public void testPositionIncrements() throws IOException {
233
 
    SlowSynonymMap map = new SlowSynonymMap();
234
 
 
235
 
    boolean orig = false;
236
 
    boolean merge = true;
237
 
 
238
 
    // test that generated tokens start at the same posInc as the original
239
 
    map.add(strings("a"), tokens("aa"), orig, merge);
240
 
    assertTokenizesTo(map, tokens("a,5"), 
241
 
        new String[] { "aa" },
242
 
        new int[] { 5 });
243
 
    assertTokenizesTo(map, tokens("a,0"),
244
 
        new String[] { "aa" },
245
 
        new int[] { 0 });
246
 
 
247
 
    // test that offset of first replacement is ignored (always takes the orig offset)
248
 
    map.add(strings("b"), tokens("bb,100"), orig, merge);
249
 
    assertTokenizesTo(map, tokens("b,5"),
250
 
        new String[] { "bb" },
251
 
        new int[] { 5 });
252
 
    assertTokenizesTo(map, tokens("b,0"),
253
 
        new String[] { "bb" },
254
 
        new int[] { 0 });
255
 
 
256
 
    // test that subsequent tokens are adjusted accordingly
257
 
    map.add(strings("c"), tokens("cc,100 c2,2"), orig, merge);
258
 
    assertTokenizesTo(map, tokens("c,5"),
259
 
        new String[] { "cc", "c2" },
260
 
        new int[] { 5, 2 });
261
 
    assertTokenizesTo(map, tokens("c,0"),
262
 
        new String[] { "cc", "c2" },
263
 
        new int[] { 0, 2 });
264
 
  }
265
 
 
266
 
 
267
 
  public void testPositionIncrementsWithOrig() throws IOException {
268
 
    SlowSynonymMap map = new SlowSynonymMap();
269
 
 
270
 
    boolean orig = true;
271
 
    boolean merge = true;
272
 
 
273
 
    // test that generated tokens start at the same offset as the original
274
 
    map.add(strings("a"), tokens("aa"), orig, merge);
275
 
    assertTokenizesTo(map, tokens("a,5"),
276
 
        new String[] { "a", "aa" },
277
 
        new int[] { 5, 0 });
278
 
    assertTokenizesTo(map, tokens("a,0"),
279
 
        new String[] { "a", "aa" },
280
 
        new int[] { 0, 0 });
281
 
 
282
 
    // test that offset of first replacement is ignored (always takes the orig offset)
283
 
    map.add(strings("b"), tokens("bb,100"), orig, merge);
284
 
    assertTokenizesTo(map, tokens("b,5"),
285
 
        new String[] { "b", "bb" },
286
 
        new int[] { 5, 0 });
287
 
    assertTokenizesTo(map, tokens("b,0"),
288
 
        new String[] { "b", "bb" },
289
 
        new int[] { 0, 0 });
290
 
 
291
 
    // test that subsequent tokens are adjusted accordingly
292
 
    map.add(strings("c"), tokens("cc,100 c2,2"), orig, merge);
293
 
    assertTokenizesTo(map, tokens("c,5"),
294
 
        new String[] { "c", "cc", "c2" },
295
 
        new int[] { 5, 0, 2 });
296
 
    assertTokenizesTo(map, tokens("c,0"),
297
 
        new String[] { "c", "cc", "c2" },
298
 
        new int[] { 0, 0, 2 });
299
 
  }
300
 
 
301
 
 
302
 
  public void testOffsetBug() throws IOException {
303
 
    // With the following rules:
304
 
    // a a=>b
305
 
    // x=>y
306
 
    // analysing "a x" causes "y" to have a bad offset (end less than start)
307
 
    // SOLR-167
308
 
    SlowSynonymMap map = new SlowSynonymMap();
309
 
 
310
 
    boolean orig = false;
311
 
    boolean merge = true;
312
 
 
313
 
    map.add(strings("a a"), tokens("b"), orig, merge);
314
 
    map.add(strings("x"), tokens("y"), orig, merge);
315
 
 
316
 
    // "a a x" => "b y"
317
 
    assertTokenizesTo(map, tokens("a,1,0,1 a,1,2,3 x,1,4,5"),
318
 
        new String[] { "b", "y" },
319
 
        new int[] { 0, 4 },
320
 
        new int[] { 3, 5 },
321
 
        new int[] { 1, 1 });
322
 
  }
323
 
 
324
 
  
325
 
  /***
326
 
   * Return a list of tokens according to a test string format:
327
 
   * a b c  =>  returns List<Token> [a,b,c]
328
 
   * a/b   => tokens a and b share the same spot (b.positionIncrement=0)
329
 
   * a,3/b/c => a,b,c all share same position (a.positionIncrement=3, b.positionIncrement=0, c.positionIncrement=0)
330
 
   * a,1,10,11  => "a" with positionIncrement=1, startOffset=10, endOffset=11
331
 
   * @deprecated (3.0) does not support attributes api
332
 
   */
333
 
  @Deprecated
334
 
  private List<Token> tokens(String str) {
335
 
    String[] arr = str.split(" ");
336
 
    List<Token> result = new ArrayList<Token>();
337
 
    for (int i=0; i<arr.length; i++) {
338
 
      String[] toks = arr[i].split("/");
339
 
      String[] params = toks[0].split(",");
340
 
 
341
 
      int posInc;
342
 
      int start;
343
 
      int end;
344
 
 
345
 
      if (params.length > 1) {
346
 
        posInc = Integer.parseInt(params[1]);
347
 
      } else {
348
 
        posInc = 1;
349
 
      }
350
 
 
351
 
      if (params.length > 2) {
352
 
        start = Integer.parseInt(params[2]);
353
 
      } else {
354
 
        start = 0;
355
 
      }
356
 
 
357
 
      if (params.length > 3) {
358
 
        end = Integer.parseInt(params[3]);
359
 
      } else {
360
 
        end = start + params[0].length();
361
 
      }
362
 
 
363
 
      Token t = new Token(params[0],start,end,"TEST");
364
 
      t.setPositionIncrement(posInc);
365
 
      
366
 
      result.add(t);
367
 
      for (int j=1; j<toks.length; j++) {
368
 
        t = new Token(toks[j],0,0,"TEST");
369
 
        t.setPositionIncrement(0);
370
 
        result.add(t);
371
 
      }
372
 
    }
373
 
    return result;
374
 
  }
375
 
  
376
 
  /**
377
 
   * @deprecated (3.0) does not support custom attributes
378
 
   */
379
 
  @Deprecated
380
 
  private static class IterTokenStream extends TokenStream {
381
 
    final Token tokens[];
382
 
    int index = 0;
383
 
    CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
384
 
    OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
385
 
    PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);
386
 
    FlagsAttribute flagsAtt = addAttribute(FlagsAttribute.class);
387
 
    TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
388
 
    PayloadAttribute payloadAtt = addAttribute(PayloadAttribute.class);
389
 
    
390
 
    public IterTokenStream(Token... tokens) {
391
 
      super();
392
 
      this.tokens = tokens;
393
 
    }
394
 
    
395
 
    public IterTokenStream(Collection<Token> tokens) {
396
 
      this(tokens.toArray(new Token[tokens.size()]));
397
 
    }
398
 
    
399
 
    @Override
400
 
    public boolean incrementToken() throws IOException {
401
 
      if (index >= tokens.length)
402
 
        return false;
403
 
      else {
404
 
        clearAttributes();
405
 
        Token token = tokens[index++];
406
 
        termAtt.setEmpty().append(token);
407
 
        offsetAtt.setOffset(token.startOffset(), token.endOffset());
408
 
        posIncAtt.setPositionIncrement(token.getPositionIncrement());
409
 
        flagsAtt.setFlags(token.getFlags());
410
 
        typeAtt.setType(token.type());
411
 
        payloadAtt.setPayload(token.getPayload());
412
 
        return true;
413
 
      }
414
 
    }
415
 
  }
416
 
}