Skip to content

Commit

Permalink
Merge branch 'Crystark-feature/53-allowToAckEmits'
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-bor committed May 15, 2017
2 parents f3baace + d9a10a4 commit cebd7b4
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/main/java/org/ahocorasick/trie/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.ahocorasick.interval.Intervalable;
import org.ahocorasick.trie.handler.DefaultEmitHandler;
import org.ahocorasick.trie.handler.EmitHandler;
import org.ahocorasick.trie.handler.StatefulEmitHandler;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -103,10 +104,13 @@ private Token createMatch(Emit emit, String text) {
return new MatchToken(text.substring(emit.getStart(), emit.getEnd() + 1), emit);
}

@SuppressWarnings("unchecked")
public Collection<Emit> parseText(final CharSequence text) {
final DefaultEmitHandler emitHandler = new DefaultEmitHandler();
parseText(text, emitHandler);
return parseText(text, new DefaultEmitHandler());
}

@SuppressWarnings("unchecked")
public Collection<Emit> parseText(final CharSequence text, final StatefulEmitHandler emitHandler) {
parseText(text, (EmitHandler) emitHandler);

final List<Emit> collectedEmits = emitHandler.getEmits();

Expand Down Expand Up @@ -281,8 +285,10 @@ private boolean storeEmits(final int position, final State currentState, final E
// TODO: The check for empty might be superfluous.
if (emits != null && !emits.isEmpty()) {
for (final String emit : emits) {
emitHandler.emit(new Emit(position - emit.length() + 1, position, emit));
emitted = true;
emitted = emitHandler.emit(new Emit(position - emit.length() + 1, position, emit)) || emitted;
if (emitted && trieConfig.isStopOnHit()) {
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.ahocorasick.trie.handler;

import java.util.ArrayList;
import java.util.List;

import org.ahocorasick.trie.Emit;

public abstract class AbstractStatefulEmitHandler implements StatefulEmitHandler {

private final List<Emit> emits = new ArrayList<>();

public void addEmit(final Emit emit) {
this.emits.add(emit);
}

@Override
public List<Emit> getEmits() {
return this.emits;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import java.util.ArrayList;
import java.util.List;

public class DefaultEmitHandler implements EmitHandler {
public class DefaultEmitHandler implements StatefulEmitHandler {

private final List<Emit> emits = new ArrayList<>();

@Override
public void emit(final Emit emit) {
public boolean emit(final Emit emit) {
this.emits.add(emit);
return true;
}

@Override
public List<Emit> getEmits() {
return this.emits;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import org.ahocorasick.trie.Emit;

public interface EmitHandler {
void emit(Emit emit);
boolean emit(Emit emit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.ahocorasick.trie.handler;

import java.util.List;

import org.ahocorasick.trie.Emit;

public interface StatefulEmitHandler extends EmitHandler {
List<Emit> getEmits();
}
36 changes: 34 additions & 2 deletions src/test/java/org/ahocorasick/trie/TrieTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.ahocorasick.trie;

import org.ahocorasick.trie.handler.AbstractStatefulEmitHandler;
import org.ahocorasick.trie.handler.EmitHandler;
import org.ahocorasick.trie.handler.StatefulEmitHandler;
import org.junit.Test;

import java.util.ArrayList;
Expand Down Expand Up @@ -97,9 +99,38 @@ public void ushersTestAndStopOnHit() {
.stopOnHit()
.build();
Collection<Emit> emits = trie.parseText("ushers");
assertEquals(2, emits.size()); // she @ 3, he @ 3, hers @ 5
assertEquals(1, emits.size()); // she @ 3, he @ 3, hers @ 5
Iterator<Emit> iterator = emits.iterator();
checkEmit(iterator.next(), 2, 3, "he");
}

@Test
public void ushersTestStopOnHitSkipOne() {
Trie trie = Trie.builder()
.addKeywords(PRONOUNS)
.stopOnHit()
.build();

StatefulEmitHandler testEmitHandler = new AbstractStatefulEmitHandler() {
boolean first = true;

@Override
public boolean emit(final Emit emit) {
if(first) {
// return false for the first element
first = false;
return false;
}
addEmit(emit);
return true;
}

};

trie.parseText("ushers", testEmitHandler);
Collection<Emit> emits = testEmitHandler.getEmits();
assertEquals(1, emits.size()); // she @ 3, he @ 3, hers @ 5
Iterator<Emit> iterator = emits.iterator();
checkEmit(iterator.next(), 1, 3, "she");
}

Expand Down Expand Up @@ -152,8 +183,9 @@ public void ushersTestByCallback() {
EmitHandler emitHandler = new EmitHandler() {

@Override
public void emit(Emit emit) {
public boolean emit(Emit emit) {
emits.add(emit);
return true;
}
};
trie.parseText("ushers", emitHandler);
Expand Down

0 comments on commit cebd7b4

Please sign in to comment.