1 package org.apache.lucene.index;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.StringReader;
23 import java.util.HashSet;
26 import org.apache.lucene.util.LuceneTestCase;
28 import org.apache.lucene.analysis.WordlistLoader;
30 public class TestWordlistLoader extends LuceneTestCase {
32 public void testWordlistLoading() throws IOException {
33 String s = "ONE\n two \nthree";
34 HashSet<String> wordSet1 = WordlistLoader.getWordSet(new StringReader(s));
36 HashSet<String> wordSet2 = WordlistLoader.getWordSet(new BufferedReader(new StringReader(s)));
40 public void testComments() throws Exception {
41 String s = "ONE\n two \nthree\n#comment";
42 HashSet<String> wordSet1 = WordlistLoader.getWordSet(new StringReader(s), "#");
44 assertFalse(wordSet1.contains("#comment"));
45 assertFalse(wordSet1.contains("comment"));
49 private void checkSet(HashSet<String> wordset) {
50 assertEquals(3, wordset.size());
51 assertTrue(wordset.contains("ONE")); // case is not modified
52 assertTrue(wordset.contains("two")); // surrounding whitespace is removed
53 assertTrue(wordset.contains("three"));
54 assertFalse(wordset.contains("four"));
58 * Test stopwords in snowball format
60 public void testSnowballListLoading() throws IOException {
62 "|comment\n" + // commented line
63 " |comment\n" + // commented line with leading whitespace
65 " \t\n" + // line with only whitespace
66 " |comment | comment\n" + // commented line with comment
67 "ONE\n" + // stopword, in uppercase
68 " two \n" + // stopword with leading/trailing space
69 " three four five \n" + // multiple stopwords
70 "six seven | comment\n"; //multiple stopwords + comment
71 Set<String> wordset = WordlistLoader.getSnowballWordSet(new StringReader(s));
72 assertEquals(7, wordset.size());
73 assertTrue(wordset.contains("ONE"));
74 assertTrue(wordset.contains("two"));
75 assertTrue(wordset.contains("three"));
76 assertTrue(wordset.contains("four"));
77 assertTrue(wordset.contains("five"));
78 assertTrue(wordset.contains("six"));
79 assertTrue(wordset.contains("seven"));