Fixed formatting changes.
This commit is contained in:
parent
df503bae43
commit
a46e7dfe1d
@ -13,11 +13,9 @@ import java.util.concurrent.LinkedBlockingDeque;
|
||||
*
|
||||
* Based on the Aho-Corasick white paper, Bell technologies:
|
||||
* ftp://163.13.200.222/assistant/bearhero/prog/%A8%E4%A5%A6/ac_bm.pdf
|
||||
*
|
||||
* @author Robert Bor
|
||||
*/
|
||||
public class Trie
|
||||
{
|
||||
public class Trie {
|
||||
|
||||
private TrieConfig trieConfig;
|
||||
|
||||
@ -25,37 +23,31 @@ public class Trie
|
||||
|
||||
private boolean failureStatesConstructed = false;
|
||||
|
||||
public Trie(TrieConfig trieConfig)
|
||||
{
|
||||
public Trie(TrieConfig trieConfig) {
|
||||
this.trieConfig = trieConfig;
|
||||
this.rootState = new State();
|
||||
}
|
||||
|
||||
public Trie()
|
||||
{
|
||||
public Trie() {
|
||||
this(new TrieConfig());
|
||||
}
|
||||
|
||||
public Trie caseInsensitive()
|
||||
{
|
||||
public Trie caseInsensitive() {
|
||||
this.trieConfig.setCaseInsensitive(true);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Trie removeOverlaps()
|
||||
{
|
||||
public Trie removeOverlaps() {
|
||||
this.trieConfig.setAllowOverlaps(false);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Trie onlyWholeWords()
|
||||
{
|
||||
public Trie onlyWholeWords() {
|
||||
this.trieConfig.setOnlyWholeWords(true);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void addKeyword(String keyword)
|
||||
{
|
||||
public void addKeyword(String keyword) {
|
||||
if (keyword == null || keyword.length() == 0) {
|
||||
return;
|
||||
}
|
||||
@ -66,8 +58,7 @@ public class Trie
|
||||
currentState.addEmit(keyword);
|
||||
}
|
||||
|
||||
public Collection<Token> tokenize(String text)
|
||||
{
|
||||
public Collection<Token> tokenize(String text) {
|
||||
|
||||
Collection<Token> tokens = new ArrayList<Token>();
|
||||
|
||||
@ -87,20 +78,16 @@ public class Trie
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private Token createFragment(Emit emit, String text, int lastCollectedPosition)
|
||||
{
|
||||
return new FragmentToken(text.substring(lastCollectedPosition + 1, emit == null ? text.length() : emit.
|
||||
getStart()));
|
||||
private Token createFragment(Emit emit, String text, int lastCollectedPosition) {
|
||||
return new FragmentToken(text.substring(lastCollectedPosition+1, emit == null ? text.length() : emit.getStart()));
|
||||
}
|
||||
|
||||
private Token createMatch(Emit emit, String text)
|
||||
{
|
||||
private Token createMatch(Emit emit, String text) {
|
||||
return new MatchToken(text.substring(emit.getStart(), emit.getEnd()+1), emit);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Collection<Emit> parseText(String text)
|
||||
{
|
||||
public Collection<Emit> parseText(String text) {
|
||||
checkForConstructedFailureStates();
|
||||
|
||||
int position = 0;
|
||||
@ -135,7 +122,6 @@ public class Trie
|
||||
|
||||
public Emit firstMatch(String text)
|
||||
{
|
||||
|
||||
if (!trieConfig.isAllowOverlaps()) {
|
||||
// Slow path. Needs to find all the matches to detect overlaps.
|
||||
Collection<Emit> parseText = parseText(text);
|
||||
@ -144,9 +130,7 @@ public class Trie
|
||||
}
|
||||
} else {
|
||||
// Fast path. Returs first match found.
|
||||
|
||||
checkForConstructedFailureStates();
|
||||
|
||||
int position = 0;
|
||||
State currentState = this.rootState;
|
||||
for (Character character : text.toCharArray()) {
|
||||
@ -154,12 +138,10 @@ public class Trie
|
||||
character = Character.toLowerCase(character);
|
||||
}
|
||||
currentState = getState(currentState, character);
|
||||
|
||||
Collection<String> emitStrs = currentState.emit();
|
||||
if (emitStrs != null && !emitStrs.isEmpty()) {
|
||||
for (String emitStr : emitStrs) {
|
||||
final Emit emit = new Emit(position - emitStr.length() + 1, position, emitStr);
|
||||
|
||||
if (trieConfig.isOnlyWholeWords()) {
|
||||
if (!isPartialMatch(text, emit)) {
|
||||
return emit;
|
||||
@ -169,12 +151,9 @@ public class Trie
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
position++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -188,21 +167,18 @@ public class Trie
|
||||
|
||||
private void removePartialMatches(String searchText, List<Emit> collectedEmits)
|
||||
{
|
||||
long size = searchText.length();
|
||||
List<Emit> removeEmits = new ArrayList<Emit>();
|
||||
for (Emit emit : collectedEmits) {
|
||||
if (isPartialMatch(searchText, emit)) {
|
||||
removeEmits.add(emit);
|
||||
}
|
||||
}
|
||||
|
||||
for (Emit removeEmit : removeEmits) {
|
||||
collectedEmits.remove(removeEmit);
|
||||
}
|
||||
}
|
||||
|
||||
private State getState(State currentState, Character character)
|
||||
{
|
||||
private State getState(State currentState, Character character) {
|
||||
State newCurrentState = currentState.nextState(character);
|
||||
while (newCurrentState == null) {
|
||||
currentState = currentState.failure();
|
||||
@ -211,15 +187,13 @@ public class Trie
|
||||
return newCurrentState;
|
||||
}
|
||||
|
||||
private void checkForConstructedFailureStates()
|
||||
{
|
||||
private void checkForConstructedFailureStates() {
|
||||
if (!this.failureStatesConstructed) {
|
||||
constructFailureStates();
|
||||
}
|
||||
}
|
||||
|
||||
private void constructFailureStates()
|
||||
{
|
||||
private void constructFailureStates() {
|
||||
Queue<State> queue = new LinkedBlockingDeque<State>();
|
||||
|
||||
// First, set the fail state of all depth 1 states to the root state
|
||||
@ -248,8 +222,7 @@ public class Trie
|
||||
}
|
||||
}
|
||||
|
||||
private void storeEmits(int position, State currentState, List<Emit> collectedEmits)
|
||||
{
|
||||
private void storeEmits(int position, State currentState, List<Emit> collectedEmits) {
|
||||
Collection<String> emits = currentState.emit();
|
||||
if (emits != null && !emits.isEmpty()) {
|
||||
for (String emit : emits) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user