-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lucene-10008: Respect ignoreCase flag in CommonGramsFilterFactory #188
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
609a63f
LUCENE-10008: Respect ignoreCase flag in CommonGramsFilterFactory
vigyasharma 6131ed1
LUCENE-10008: Styling fixes from precommit check
vigyasharma eeb1fe3
Spotless violations fix
vigyasharma 17e93a6
Add common base class for Common/Stop/KeepWords filter factories
vigyasharma 31fff40
Linting errors
vigyasharma b5d06e3
Add license header
vigyasharma f4b0558
Move default stop word implementation to concrete subclasses
vigyasharma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...nalysis/common/src/java/org/apache/lucene/analysis/en/AbstractWordsFileFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.analysis.en; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.apache.lucene.analysis.CharArraySet; | ||
import org.apache.lucene.analysis.TokenFilterFactory; | ||
import org.apache.lucene.analysis.WordlistLoader; | ||
import org.apache.lucene.util.ResourceLoader; | ||
import org.apache.lucene.util.ResourceLoaderAware; | ||
|
||
/** | ||
* Abstract parent class for analysis factories that accept a stopwords file as input. | ||
* | ||
* <p>Concrete implementations can leverage the following input attributes. All attributes are | ||
* optional: | ||
* | ||
* <ul> | ||
* <li><code>ignoreCase</code> defaults to <code>false</code> | ||
* <li><code>words</code> should be the name of a stopwords file to parse, if not specified the | ||
* factory will use the value provided by {@link #createDefaultWords()} implementation in | ||
* concrete subclass. | ||
* <li><code>format</code> defines how the <code>words</code> file will be parsed, and defaults to | ||
* <code>wordset</code>. If <code>words</code> is not specified, then <code>format</code> must | ||
* not be specified. | ||
* </ul> | ||
* | ||
* <p>The valid values for the <code>format</code> option are: | ||
* | ||
* <ul> | ||
* <li><code>wordset</code> - This is the default format, which supports one word per line | ||
* (including any intra-word whitespace) and allows whole line comments beginning with the "#" | ||
* character. Blank lines are ignored. See {@link WordlistLoader#getLines | ||
* WordlistLoader.getLines} for details. | ||
* <li><code>snowball</code> - This format allows for multiple words specified on each line, and | ||
* trailing comments may be specified using the vertical line ("|"). Blank lines are | ||
* ignored. See {@link WordlistLoader#getSnowballWordSet WordlistLoader.getSnowballWordSet} | ||
* for details. | ||
* </ul> | ||
*/ | ||
public abstract class AbstractWordsFileFilterFactory extends TokenFilterFactory | ||
implements ResourceLoaderAware { | ||
|
||
public static final String FORMAT_WORDSET = "wordset"; | ||
public static final String FORMAT_SNOWBALL = "snowball"; | ||
|
||
private CharArraySet words; | ||
private final String wordFiles; | ||
private final String format; | ||
private final boolean ignoreCase; | ||
|
||
/** Default ctor for compatibility with SPI */ | ||
protected AbstractWordsFileFilterFactory() { | ||
throw defaultCtorException(); | ||
} | ||
|
||
/** Initialize this factory via a set of key-value pairs. */ | ||
public AbstractWordsFileFilterFactory(Map<String, String> args) { | ||
super(args); | ||
wordFiles = get(args, "words"); | ||
format = get(args, "format", (null == wordFiles ? null : FORMAT_WORDSET)); | ||
ignoreCase = getBoolean(args, "ignoreCase", false); | ||
if (!args.isEmpty()) { | ||
throw new IllegalArgumentException("Unknown parameters: " + args); | ||
} | ||
} | ||
|
||
/** Initialize the set of stopwords provided via ResourceLoader, or using defaults. */ | ||
@Override | ||
public void inform(ResourceLoader loader) throws IOException { | ||
if (wordFiles != null) { | ||
if (FORMAT_WORDSET.equalsIgnoreCase(format)) { | ||
words = getWordSet(loader, wordFiles, ignoreCase); | ||
} else if (FORMAT_SNOWBALL.equalsIgnoreCase(format)) { | ||
words = getSnowballWordSet(loader, wordFiles, ignoreCase); | ||
} else { | ||
throw new IllegalArgumentException( | ||
"Unknown 'format' specified for 'words' file: " + format); | ||
} | ||
} else { | ||
if (null != format) { | ||
throw new IllegalArgumentException( | ||
"'format' can not be specified w/o an explicit 'words' file: " + format); | ||
} | ||
words = createDefaultWords(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this fixes the bug, because we now always dynamically create the |
||
} | ||
} | ||
|
||
/** Default word set implementation. */ | ||
protected abstract CharArraySet createDefaultWords(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woot! |
||
|
||
public CharArraySet getWords() { | ||
return words; | ||
} | ||
|
||
public String getWordFiles() { | ||
return wordFiles; | ||
} | ||
|
||
public String getFormat() { | ||
return format; | ||
} | ||
|
||
public boolean isIgnoreCase() { | ||
return ignoreCase; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, this was the bug? Because this (default) path ignores
ignoreCase
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right.