Added missing override annotations. Added final modifier to Interval member variables. Updated documentation for ignoreCase (issue #33) and moved the ignore methods to the top of the builder to reflect their preferred calling order.

This commit is contained in:
djarvis 2016-11-29 22:34:55 -08:00 committed by robert-bor
parent 89e7efab72
commit 5edf6d8126
2 changed files with 158 additions and 146 deletions

View File

@ -2,8 +2,8 @@ package org.ahocorasick.interval;
public class Interval implements Intervalable {
private int start;
private int end;
private final int start;
private final int end;
/**
* Constructs an interval with a start and end position.
@ -21,6 +21,7 @@ public class Interval implements Intervalable {
*
* @return A number between 0 (start of text) and the text length.
*/
@Override
public int getStart() {
return this.start;
}
@ -30,6 +31,7 @@ public class Interval implements Intervalable {
*
* @return A number between getStart() + 1 and the text length.
*/
@Override
public int getEnd() {
return this.end;
}
@ -39,6 +41,7 @@ public class Interval implements Intervalable {
*
* @return The end position less the start position, plus one.
*/
@Override
public int size() {
return end - start + 1;
}
@ -47,6 +50,7 @@ public class Interval implements Intervalable {
* Answers whether the given interval overlaps this interval
* instance.
*
* @param other
* @return true The intervals overlap.
*/
public boolean overlapsWith(final Interval other) {

View File

@ -1,19 +1,15 @@
package org.ahocorasick.trie;
import org.ahocorasick.interval.IntervalTree;
import org.ahocorasick.interval.Intervalable;
import org.ahocorasick.trie.handler.DefaultEmitHandler;
import org.ahocorasick.trie.handler.EmitHandler;
import static java.lang.Character.isWhitespace;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import static java.lang.Character.*;
import java.lang.Character;
import org.ahocorasick.interval.IntervalTree;
import org.ahocorasick.interval.Intervalable;
import org.ahocorasick.trie.handler.DefaultEmitHandler;
import org.ahocorasick.trie.handler.EmitHandler;
/**
* Based on the Aho-Corasick white paper, Bell technologies:
@ -36,16 +32,41 @@ public class Trie {
* Used by the builder to add a text search keyword.
*
* @param keyword The search term to add to the list of search terms.
*
* @throws NullPointerException if the keyword is null.
*/
private void addKeyword(String keyword) {
if (keyword.length() > 0) {
if (isCaseInsensitive()) {
keyword = keyword.toLowerCase();
}
addState(keyword).addEmit(keyword);
if( keyword.isEmpty() ) {
return;
}
if( isCaseInsensitive() ) {
keyword = keyword.toLowerCase();
}
addState(keyword).addEmit(keyword);
}
/**
* Delegates to addKeyword.
*
* @param keywords List of search term to add to the list of search terms.
*/
private void addKeywords( final String[] keywords ) {
for( final String keyword : keywords ) {
addKeyword( keyword );
}
}
/**
* Delegates to addKeyword.
*
* @param keywords List of search term to add to the list of search terms.
*/
private void addKeywords( final Collection<String> keywords ) {
for( final String keyword : keywords ) {
addKeyword( keyword );
}
}
private State addState(final String keyword) {
@ -73,15 +94,12 @@ public class Trie {
return tokens;
}
private Token createFragment(
final Emit emit,
final String text,
final int lastCollectedPosition) {
return new FragmentToken(text.substring(lastCollectedPosition + 1, emit == null ? text.length() : emit.getStart()));
private Token createFragment(final Emit emit, final String text, final int lastCollectedPosition) {
return new FragmentToken(text.substring(lastCollectedPosition+1, emit == null ? text.length() : emit.getStart()));
}
private Token createMatch(final Emit emit, final String text) {
return new MatchToken(text.substring(emit.getStart(), emit.getEnd() + 1), emit);
private Token createMatch(Emit emit, String text) {
return new MatchToken(text.substring(emit.getStart(), emit.getEnd()+1), emit);
}
@SuppressWarnings("unchecked")
@ -100,7 +118,7 @@ public class Trie {
}
if (!trieConfig.isAllowOverlaps()) {
IntervalTree intervalTree = new IntervalTree((List<Intervalable>) (List<?>) collectedEmits);
IntervalTree intervalTree = new IntervalTree((List<Intervalable>)(List<?>)collectedEmits);
intervalTree.removeOverlaps((List<Intervalable>) (List<?>) collectedEmits);
}
@ -119,7 +137,7 @@ public class Trie {
// TODO: Maybe lowercase the entire string at once?
if (trieConfig.isCaseInsensitive()) {
character = toLowerCase(character);
character = Character.toLowerCase(character);
}
currentState = getState(currentState, character);
@ -129,10 +147,17 @@ public class Trie {
}
}
/**
* The first matching text sequence.
*
* @param text The text to search for keywords.
* @return null if no matches found.
*/
public Emit firstMatch(final CharSequence text) {
if (!trieConfig.isAllowOverlaps()) {
// Slow path. Needs to find all the matches to detect overlaps.
Collection<Emit> parseText = parseText(text);
final Collection<Emit> parseText = parseText(text);
if (parseText != null && !parseText.isEmpty()) {
return parseText.iterator().next();
}
@ -145,7 +170,7 @@ public class Trie {
// TODO: Lowercase the entire string at once?
if (trieConfig.isCaseInsensitive()) {
character = toLowerCase(character);
character = Character.toLowerCase(character);
}
currentState = getState(currentState, character);
@ -171,9 +196,9 @@ public class Trie {
private boolean isPartialMatch(final CharSequence searchText, final Emit emit) {
return (emit.getStart() != 0 &&
isAlphabetic(searchText.charAt(emit.getStart() - 1))) ||
(emit.getEnd() + 1 != searchText.length() &&
isAlphabetic(searchText.charAt(emit.getEnd() + 1)));
Character.isAlphabetic(searchText.charAt(emit.getStart() - 1))) ||
(emit.getEnd() + 1 != searchText.length() &&
Character.isAlphabetic(searchText.charAt(emit.getEnd() + 1)));
}
private void removePartialMatches(final CharSequence searchText, final List<Emit> collectedEmits) {
@ -196,7 +221,7 @@ public class Trie {
for (final Emit emit : collectedEmits) {
if ((emit.getStart() == 0 || isWhitespace(searchText.charAt(emit.getStart() - 1))) &&
(emit.getEnd() + 1 == size || isWhitespace(searchText.charAt(emit.getEnd() + 1)))) {
(emit.getEnd() + 1 == size || isWhitespace(searchText.charAt(emit.getEnd() + 1)))) {
continue;
}
removeEmits.add(emit);
@ -207,16 +232,15 @@ public class Trie {
}
}
private State getState(final State initialState, final Character character) {
State currentState = initialState;
State updatedState = currentState.nextState(character);
private State getState(State currentState, final Character character) {
State newCurrentState = currentState.nextState(character);
while (updatedState == null) {
while (newCurrentState == null) {
currentState = currentState.failure();
updatedState = currentState.nextState(character);
newCurrentState = currentState.nextState(character);
}
return updatedState;
return newCurrentState;
}
private void constructFailureStates() {
@ -249,10 +273,7 @@ public class Trie {
}
}
private boolean storeEmits(
final int position,
final State currentState,
final EmitHandler emitHandler) {
private boolean storeEmits(final int position, final State currentState, final EmitHandler emitHandler) {
boolean emitted = false;
final Collection<String> emits = currentState.emit();
@ -268,16 +289,15 @@ public class Trie {
}
private boolean isCaseInsensitive() {
return trieConfig.isCaseInsensitive();
return trieConfig.isCaseInsensitive();
}
private State getRootState() {
return this.rootState;
return this.rootState;
}
/**
* Constructs a TrieBuilder instance for configuring the Trie using a fluent
* interface.
* Provides a fluent interface for constructing Trie instances.
*
* @return The builder used to configure its Trie.
*/
@ -285,9 +305,6 @@ public class Trie {
return new TrieBuilder();
}
/**
* Provides a fluent interface for constructing Trie instances.
*/
public static class TrieBuilder {
private final TrieConfig trieConfig = new TrieConfig();
@ -297,53 +314,18 @@ public class Trie {
/**
* Default (empty) constructor.
*/
private TrieBuilder() {
}
private TrieBuilder() {}
/**
* Adds a keyword to the Trie's list of text search keywords.
*
* @param keyword The keyword to add to the list.
* @return This builder.
* @throws NullPointerException if the keyword is null.
*/
public TrieBuilder addKeyword(final CharSequence keyword) {
getTrie().addKeyword(keyword.toString());
return this;
}
/**
* Adds a list of keywords to the Trie's list of text search keywords.
*
* @param keywords The keywords to add to the list.
* @return This builder.
*/
public TrieBuilder addKeywords(final CharSequence... keywords) {
for (final CharSequence keyword : keywords) {
addKeyword(keyword);
}
return this;
}
/**
* Adds a list of keywords to the Trie's list of text search keywords.
*
* @param keywords The keywords to add to the list.
* @return This builder.
*/
public TrieBuilder addKeywords(final Collection<CharSequence> keywords) {
return addKeywords(keywords.toArray(new CharSequence[keywords.size()]));
}
/**
* Configure the Trie to ignore case when searching for keywords in the
* text.
* Configure the Trie to ignore case when searching for keywords in
* the text. This must be called before calling addKeyword because
* the algorithm converts keywords to lowercase as they are added,
* depending on this case sensitivity setting.
*
* @return This builder.
*/
public TrieBuilder ignoreCase() {
getTrieConfig().setCaseInsensitive(true);
this.trieConfig.setCaseInsensitive(true);
return this;
}
@ -353,17 +335,54 @@ public class Trie {
* @return This builder.
*/
public TrieBuilder ignoreOverlaps() {
getTrieConfig().setAllowOverlaps(false);
this.trieConfig.setAllowOverlaps(false);
return this;
}
/**
* Adds a keyword to the Trie's list of text search keywords.
*
* @param keyword The keyword to add to the list.
*
* @return This builder.
* @throws NullPointerException if the keyword is null.
*/
public TrieBuilder addKeyword(final String keyword) {
this.trie.addKeyword(keyword);
return this;
}
/**
* Adds a list of keywords to the Trie's list of text search keywords.
*
* @param keywords The keywords to add to the list.
*
* @return This builder.
*/
public TrieBuilder addKeywords(final String... keywords) {
this.trie.addKeywords(keywords);
return this;
}
/**
* Adds a list of keywords to the Trie's list of text search keywords.
*
* @param keywords The keywords to add to the list.
*
* @return This builder.
*/
public TrieBuilder addKeywords(final Collection<String> keywords) {
this.trie.addKeywords(keywords);
return this;
}
/**
* Configure the Trie to match whole keywords in the text.
*
* @return This builder.
*/
public TrieBuilder onlyWholeWords() {
getTrieConfig().setOnlyWholeWords(true);
this.trieConfig.setOnlyWholeWords(true);
return this;
}
@ -375,48 +394,35 @@ public class Trie {
* @return This builder.
*/
public TrieBuilder onlyWholeWordsWhiteSpaceSeparated() {
getTrieConfig().setOnlyWholeWordsWhiteSpaceSeparated(true);
this.trieConfig.setOnlyWholeWordsWhiteSpaceSeparated(true);
return this;
}
/**
* Configure the Trie to stop searching for matches after the first
* keyword is found in the text.
* Configure the Trie to stop after the first keyword is found in the
* text.
*
* @return This builder.
*/
public TrieBuilder onlyFirstMatch() {
getTrieConfig().setStopOnHit(true);
public TrieBuilder stopOnHit() {
trie.trieConfig.setStopOnHit(true);
return this;
}
/**
* Construct the Trie using the builder settings.
* Configure the Trie based on the builder settings.
*
* @return The configured Trie.
*/
public Trie build() {
getTrie().constructFailureStates();
return getTrie();
}
private Trie getTrie() {
this.trie.constructFailureStates();
return this.trie;
}
private TrieConfig getTrieConfig() {
return this.trieConfig;
}
/**
* @deprecated Use onlyFirstMatch()
*/
public TrieBuilder stopOnHit() {
return onlyFirstMatch();
}
/**
* @deprecated Use ignoreCase()
*
* @return This builder.
*/
public TrieBuilder caseInsensitive() {
return ignoreCase();
@ -424,6 +430,8 @@ public class Trie {
/**
* @deprecated Use ignoreOverlaps()
*
* @return This builder.
*/
public TrieBuilder removeOverlaps() {
return ignoreOverlaps();