Issue #16 #20 #21 adopted pull request from remen which makes sure the failure states are constructed as part of the trie construction. This prevents the NPE which the referenced issues are complaining about.
This commit is contained in:
parent
fcefdfdaf9
commit
023c253c93
@ -40,7 +40,7 @@ public class Trie {
|
||||
|
||||
public Collection<Token> tokenize(String text) {
|
||||
|
||||
Collection<Token> tokens = new ArrayList<Token>();
|
||||
Collection<Token> tokens = new ArrayList<>();
|
||||
|
||||
Collection<Emit> collectedEmits = parseText(text);
|
||||
int lastCollectedPosition = -1;
|
||||
@ -86,8 +86,6 @@ public class Trie {
|
||||
}
|
||||
|
||||
public void parseText(CharSequence text, EmitHandler emitHandler) {
|
||||
checkForConstructedFailureStates();
|
||||
|
||||
State currentState = this.rootState;
|
||||
for (int position = 0; position < text.length(); position++) {
|
||||
Character character = text.charAt(position);
|
||||
@ -130,7 +128,7 @@ public class Trie {
|
||||
}
|
||||
|
||||
private void constructFailureStates() {
|
||||
Queue<State> queue = new LinkedBlockingDeque<State>();
|
||||
Queue<State> queue = new LinkedBlockingDeque<>();
|
||||
|
||||
// First, set the fail state of all depth 1 states to the root state
|
||||
for (State depthOneState : this.rootState.getStates()) {
|
||||
@ -201,6 +199,11 @@ public class Trie {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrieBuilder stopOnHit() {
|
||||
trie.trieConfig.setStopOnHit(true);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Trie build() {
|
||||
trie.constructFailureStates();
|
||||
return trie;
|
||||
|
||||
@ -46,12 +46,13 @@ public class TrieTest {
|
||||
|
||||
@Test
|
||||
public void ushersTestAndStopOnHit() {
|
||||
Trie trie = new Trie();
|
||||
trie.addKeyword("hers");
|
||||
trie.addKeyword("his");
|
||||
trie.addKeyword("she");
|
||||
trie.addKeyword("he");
|
||||
trie.stopOnHit();
|
||||
Trie trie = Trie.builder()
|
||||
.addKeyword("hers")
|
||||
.addKeyword("his")
|
||||
.addKeyword("she")
|
||||
.addKeyword("he")
|
||||
.stopOnHit()
|
||||
.build();
|
||||
Collection<Emit> emits = trie.parseText("ushers");
|
||||
assertEquals(2, emits.size()); // she @ 3, he @ 3, hers @ 5
|
||||
Iterator<Emit> iterator = emits.iterator();
|
||||
@ -62,11 +63,11 @@ public class TrieTest {
|
||||
@Test
|
||||
public void ushersTest() {
|
||||
Trie trie = Trie.builder()
|
||||
.addKeyword("hers")
|
||||
.addKeyword("his")
|
||||
.addKeyword("she")
|
||||
.addKeyword("he")
|
||||
.build();
|
||||
.addKeyword("hers")
|
||||
.addKeyword("his")
|
||||
.addKeyword("she")
|
||||
.addKeyword("he")
|
||||
.build();
|
||||
Collection<Emit> emits = trie.parseText("ushers");
|
||||
assertEquals(3, emits.size()); // she @ 3, he @ 3, hers @ 5
|
||||
Iterator<Emit> iterator = emits.iterator();
|
||||
@ -77,11 +78,12 @@ public class TrieTest {
|
||||
|
||||
@Test
|
||||
public void ushersTestByCallback() {
|
||||
Trie trie = new Trie();
|
||||
trie.addKeyword("hers");
|
||||
trie.addKeyword("his");
|
||||
trie.addKeyword("she");
|
||||
trie.addKeyword("he");
|
||||
Trie trie = Trie.builder()
|
||||
.addKeyword("hers")
|
||||
.addKeyword("his")
|
||||
.addKeyword("she")
|
||||
.addKeyword("he")
|
||||
.build();
|
||||
|
||||
final List<Emit> emits = new ArrayList<>();
|
||||
EmitHandler emitHandler = new EmitHandler() {
|
||||
@ -262,7 +264,7 @@ public class TrieTest {
|
||||
@Test
|
||||
public void unicodeIssueBug8ReportedByDwyerk() {
|
||||
String target = "LİKE THIS"; // The second character ('İ') is Unicode, which was read by AC as a 2-byte char
|
||||
assertEquals("THIS", target.substring(5,9)); // Java does it the right way
|
||||
assertEquals("THIS", target.substring(5, 9)); // Java does it the right way
|
||||
Trie trie = Trie.builder().caseInsensitive().onlyWholeWords()
|
||||
.addKeyword("this")
|
||||
.build();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user