Fixed formatting changes.

This commit is contained in:
ryan 2014-10-06 11:02:01 -07:00
parent df503bae43
commit a46e7dfe1d

View File

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