Issue #22 added possibility to stop processing on generating at least one emit
This commit is contained in:
parent
4399e42b99
commit
b85f8fc08f
@ -48,6 +48,11 @@ public class Trie {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Trie stopOnHit() {
|
||||||
|
this.trieConfig.setStopOnHit(true);
|
||||||
|
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;
|
||||||
@ -116,7 +121,9 @@ public class Trie {
|
|||||||
character = Character.toLowerCase(character);
|
character = Character.toLowerCase(character);
|
||||||
}
|
}
|
||||||
currentState = getState(currentState, character);
|
currentState = getState(currentState, character);
|
||||||
storeEmits(position, currentState, emitHandler);
|
if (storeEmits(position, currentState, emitHandler) && trieConfig.isStopOnHit()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -183,13 +190,16 @@ public class Trie {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void storeEmits(int position, State currentState, EmitHandler emitHandler) {
|
private boolean storeEmits(int position, State currentState, EmitHandler emitHandler) {
|
||||||
|
boolean emitted = false;
|
||||||
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) {
|
||||||
emitHandler.emit(new Emit(position - emit.length() + 1, position, emit));
|
emitHandler.emit(new Emit(position - emit.length() + 1, position, emit));
|
||||||
|
emitted = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return emitted;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,12 @@ public class TrieConfig {
|
|||||||
|
|
||||||
private boolean caseInsensitive = false;
|
private boolean caseInsensitive = false;
|
||||||
|
|
||||||
|
private boolean stopOnHit = false;
|
||||||
|
|
||||||
|
public boolean isStopOnHit() { return stopOnHit; }
|
||||||
|
|
||||||
|
public void setStopOnHit(boolean stopOnHit) { this.stopOnHit = stopOnHit; }
|
||||||
|
|
||||||
public boolean isAllowOverlaps() {
|
public boolean isAllowOverlaps() {
|
||||||
return allowOverlaps;
|
return allowOverlaps;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,6 +41,21 @@ public class TrieTest {
|
|||||||
checkEmit(iterator.next(), 0, 2, "bcd");
|
checkEmit(iterator.next(), 0, 2, "bcd");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ushersTestAndStopOnHit() {
|
||||||
|
Trie trie = new Trie();
|
||||||
|
trie.addKeyword("hers");
|
||||||
|
trie.addKeyword("his");
|
||||||
|
trie.addKeyword("she");
|
||||||
|
trie.addKeyword("he");
|
||||||
|
trie.stopOnHit();
|
||||||
|
Collection<Emit> emits = trie.parseText("ushers");
|
||||||
|
assertEquals(2, emits.size()); // she @ 3, he @ 3, hers @ 5
|
||||||
|
Iterator<Emit> iterator = emits.iterator();
|
||||||
|
checkEmit(iterator.next(), 2, 3, "he");
|
||||||
|
checkEmit(iterator.next(), 1, 3, "she");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void ushersTest() {
|
public void ushersTest() {
|
||||||
Trie trie = new Trie();
|
Trie trie = new Trie();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user