pull #17 changes adopted to implement a whole word check on the entire keyword, including whitespaces.
This commit is contained in:
parent
76ae8222ea
commit
dc27d6e3e9
@ -80,6 +80,10 @@ public class Trie {
|
||||
removePartialMatches(text, collectedEmits);
|
||||
}
|
||||
|
||||
if (trieConfig.isOnlyWholeWordsWhiteSpaceSeparated()) {
|
||||
removePartialMatchesWhiteSpaceSeparated(text, collectedEmits);
|
||||
}
|
||||
|
||||
if (!trieConfig.isAllowOverlaps()) {
|
||||
IntervalTree intervalTree = new IntervalTree((List<Intervalable>)(List<?>)collectedEmits);
|
||||
intervalTree.removeOverlaps((List<Intervalable>) (List<?>) collectedEmits);
|
||||
@ -161,6 +165,21 @@ public class Trie {
|
||||
}
|
||||
}
|
||||
|
||||
private void removePartialMatchesWhiteSpaceSeparated(CharSequence searchText, List<Emit> collectedEmits) {
|
||||
long size = searchText.length();
|
||||
List<Emit> removeEmits = new ArrayList<>();
|
||||
for (Emit emit : collectedEmits) {
|
||||
if ((emit.getStart() == 0 || Character.isWhitespace(searchText.charAt(emit.getStart() - 1))) &&
|
||||
(emit.getEnd() + 1 == size || Character.isWhitespace(searchText.charAt(emit.getEnd() + 1)))) {
|
||||
continue;
|
||||
}
|
||||
removeEmits.add(emit);
|
||||
}
|
||||
for (Emit removeEmit : removeEmits) {
|
||||
collectedEmits.remove(removeEmit);
|
||||
}
|
||||
}
|
||||
|
||||
private State getState(State currentState, Character character) {
|
||||
State newCurrentState = currentState.nextState(character);
|
||||
while (newCurrentState == null) {
|
||||
@ -237,6 +256,11 @@ public class Trie {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrieBuilder onlyWholeWordsWhiteSpaceSeparated() {
|
||||
this.trieConfig.setOnlyWholeWordsWhiteSpaceSeparated(true);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrieBuilder addKeyword(String keyword) {
|
||||
trie.addKeyword(keyword);
|
||||
return this;
|
||||
|
||||
@ -6,6 +6,8 @@ public class TrieConfig {
|
||||
|
||||
private boolean onlyWholeWords = false;
|
||||
|
||||
private boolean onlyWholeWordsWhiteSpaceSeparated = false;
|
||||
|
||||
private boolean caseInsensitive = false;
|
||||
|
||||
private boolean stopOnHit = false;
|
||||
@ -30,6 +32,12 @@ public class TrieConfig {
|
||||
this.onlyWholeWords = onlyWholeWords;
|
||||
}
|
||||
|
||||
public boolean isOnlyWholeWordsWhiteSpaceSeparated() { return onlyWholeWordsWhiteSpaceSeparated; }
|
||||
|
||||
public void setOnlyWholeWordsWhiteSpaceSeparated(boolean onlyWholeWordsWhiteSpaceSeparated) {
|
||||
this.onlyWholeWordsWhiteSpaceSeparated = onlyWholeWordsWhiteSpaceSeparated;
|
||||
}
|
||||
|
||||
public boolean isCaseInsensitive() {
|
||||
return caseInsensitive;
|
||||
}
|
||||
|
||||
@ -415,9 +415,20 @@ public class TrieTest {
|
||||
checkEmit(firstMatch, 5, 8, "this");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void partialMatchWhiteSpaces() {
|
||||
Trie trie = Trie.builder()
|
||||
.onlyWholeWordsWhiteSpaceSeparated()
|
||||
.addKeyword("#sugar-123")
|
||||
.build();
|
||||
Collection < Emit > emits = trie.parseText("#sugar-123 #sugar-1234"); // left, middle, right test
|
||||
assertEquals(1, emits.size()); // Match must not be made
|
||||
checkEmit(emits.iterator().next(), 0, 9, "#sugar-123");
|
||||
}
|
||||
|
||||
private void checkEmit(Emit next, int expectedStart, int expectedEnd, String expectedKeyword) {
|
||||
assertEquals("Start of emit should have been "+expectedStart, expectedStart, next.getStart());
|
||||
assertEquals("End of emit should have been "+expectedEnd, expectedEnd, next.getEnd());
|
||||
assertEquals("Start of emit should have been " + expectedStart, expectedStart, next.getStart());
|
||||
assertEquals("End of emit should have been " + expectedEnd, expectedEnd, next.getEnd());
|
||||
assertEquals(expectedKeyword, next.getKeyword());
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user