Search in sources :

Example 1 with StemmerOverrideMap

use of org.apache.lucene.analysis.miscellaneous.StemmerOverrideFilter.StemmerOverrideMap in project lucene-solr by apache.

the class TestStemmerOverrideFilter method testRandomRealisticKeyword.

public void testRandomRealisticKeyword() throws IOException {
    Map<String, String> map = new HashMap<>();
    int numTerms = atLeast(50);
    for (int i = 0; i < numTerms; i++) {
        String randomRealisticUnicodeString = TestUtil.randomRealisticUnicodeString(random());
        if (randomRealisticUnicodeString.length() > 0) {
            String value = TestUtil.randomSimpleString(random());
            map.put(randomRealisticUnicodeString, value.isEmpty() ? "a" : value);
        }
    }
    if (map.isEmpty()) {
        map.put("booked", "books");
    }
    // This test might fail if ignoreCase is true since the map might have twice the same key, once
    // lowercased and once uppercased
    StemmerOverrideFilter.Builder builder = new StemmerOverrideFilter.Builder(false);
    Set<Entry<String, String>> entrySet = map.entrySet();
    for (Entry<String, String> entry : entrySet) {
        builder.add(entry.getKey(), entry.getValue());
    }
    StemmerOverrideMap build = builder.build();
    for (Entry<String, String> entry : entrySet) {
        if (random().nextBoolean()) {
            Tokenizer tokenizer = new KeywordTokenizer();
            tokenizer.setReader(new StringReader(entry.getKey()));
            TokenStream stream = new PorterStemFilter(new StemmerOverrideFilter(tokenizer, build));
            assertTokenStreamContents(stream, new String[] { entry.getValue() });
        }
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) HashMap(java.util.HashMap) PorterStemFilter(org.apache.lucene.analysis.en.PorterStemFilter) KeywordTokenizer(org.apache.lucene.analysis.core.KeywordTokenizer) StemmerOverrideMap(org.apache.lucene.analysis.miscellaneous.StemmerOverrideFilter.StemmerOverrideMap) Entry(java.util.Map.Entry) StringReader(java.io.StringReader) WhitespaceTokenizer(org.apache.lucene.analysis.core.WhitespaceTokenizer) Tokenizer(org.apache.lucene.analysis.Tokenizer) KeywordTokenizer(org.apache.lucene.analysis.core.KeywordTokenizer)

Aggregations

StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 TokenStream (org.apache.lucene.analysis.TokenStream)1 Tokenizer (org.apache.lucene.analysis.Tokenizer)1 KeywordTokenizer (org.apache.lucene.analysis.core.KeywordTokenizer)1 WhitespaceTokenizer (org.apache.lucene.analysis.core.WhitespaceTokenizer)1 PorterStemFilter (org.apache.lucene.analysis.en.PorterStemFilter)1 StemmerOverrideMap (org.apache.lucene.analysis.miscellaneous.StemmerOverrideFilter.StemmerOverrideMap)1