Issue #23 removed the ParseConfiguration, rely on CharSequence instead
This commit is contained in:
parent
88799fb3da
commit
055e13c298
@ -2,7 +2,6 @@ package org.ahocorasick.trie;
|
||||
|
||||
import org.ahocorasick.interval.IntervalTree;
|
||||
import org.ahocorasick.interval.Intervalable;
|
||||
import org.ahocorasick.trie.configuration.ParseConfiguration;
|
||||
import org.ahocorasick.trie.handler.DefaultEmitHandler;
|
||||
import org.ahocorasick.trie.handler.EmitHandler;
|
||||
|
||||
@ -91,9 +90,7 @@ public class Trie {
|
||||
@SuppressWarnings("unchecked")
|
||||
public Collection<Emit> parseText(String text) {
|
||||
DefaultEmitHandler emitHandler = new DefaultEmitHandler();
|
||||
parseText(new ParseConfiguration()
|
||||
.setEmitHandler(emitHandler)
|
||||
.setText(text));
|
||||
parseText(text, emitHandler);
|
||||
|
||||
List<Emit> collectedEmits = emitHandler.getEmits();
|
||||
|
||||
@ -109,18 +106,17 @@ public class Trie {
|
||||
return collectedEmits;
|
||||
}
|
||||
|
||||
public void parseText(ParseConfiguration parseConfiguration) {
|
||||
public void parseText(CharSequence text, EmitHandler emitHandler) {
|
||||
checkForConstructedFailureStates();
|
||||
|
||||
int position = 0;
|
||||
State currentState = this.rootState;
|
||||
for (Character character : parseConfiguration) {
|
||||
for (int position = 0; position < text.length(); position++) {
|
||||
Character character = text.charAt(position);
|
||||
if (trieConfig.isCaseInsensitive()) {
|
||||
character = Character.toLowerCase(character);
|
||||
}
|
||||
currentState = getState(currentState, character);
|
||||
storeEmits(position, currentState, parseConfiguration.getEmitHandler());
|
||||
position++;
|
||||
storeEmits(position, currentState, emitHandler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,43 +0,0 @@
|
||||
package org.ahocorasick.trie.configuration;
|
||||
|
||||
import org.ahocorasick.trie.handler.EmitHandler;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ParseConfiguration implements Iterable<Character> {
|
||||
|
||||
private String text;
|
||||
|
||||
private Reader reader;
|
||||
|
||||
private EmitHandler emitHandler;
|
||||
|
||||
public ParseConfiguration setText(String text) {
|
||||
this.text = text;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParseConfiguration setText(Reader reader) {
|
||||
this.reader = reader;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParseConfiguration setEmitHandler(EmitHandler emitHandler) {
|
||||
this.emitHandler = emitHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmitHandler getEmitHandler() {
|
||||
return emitHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Character> iterator() {
|
||||
if (reader != null) {
|
||||
return new ReaderIterator(reader);
|
||||
}
|
||||
return new StringIterator(text);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,44 +0,0 @@
|
||||
package org.ahocorasick.trie.configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ReaderIterator implements Iterator<Character> {
|
||||
|
||||
private Reader reader;
|
||||
|
||||
private int readCharacter;
|
||||
|
||||
public ReaderIterator(Reader reader) {
|
||||
this.reader = reader;
|
||||
readIntoBuffer();
|
||||
}
|
||||
|
||||
private void readIntoBuffer() {
|
||||
try {
|
||||
readCharacter = reader.read();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return readCharacter != -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character next() {
|
||||
Character returnChar = (char)readCharacter;
|
||||
readIntoBuffer();
|
||||
return returnChar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
package org.ahocorasick.trie.configuration;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class StringIterator implements Iterator<Character> {
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
private String text;
|
||||
|
||||
public StringIterator(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return counter < text.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character next() {
|
||||
return text.charAt(counter++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package org.ahocorasick.trie;
|
||||
|
||||
import org.ahocorasick.trie.configuration.ParseConfiguration;
|
||||
import org.ahocorasick.trie.handler.EmitHandler;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -73,7 +72,7 @@ public class TrieTest {
|
||||
emits.add(emit);
|
||||
}
|
||||
};
|
||||
trie.parseText(new ParseConfiguration().setText("ushers").setEmitHandler(emitHandler));
|
||||
trie.parseText("ushers", emitHandler);
|
||||
assertEquals(3, emits.size()); // she @ 3, he @ 3, hers @ 5
|
||||
Iterator<Emit> iterator = emits.iterator();
|
||||
checkEmit(iterator.next(), 2, 3, "he");
|
||||
|
||||
@ -1,33 +0,0 @@
|
||||
package org.ahocorasick.trie.configuration;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ParseConfigurationTest {
|
||||
|
||||
@Test
|
||||
public void reader() throws IOException {
|
||||
StringReader reader = new StringReader("hällö");
|
||||
ParseConfiguration parseConfiguration = new ParseConfiguration().setText(reader);
|
||||
assertIterator(parseConfiguration);
|
||||
reader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string() throws IOException {
|
||||
ParseConfiguration parseConfiguration = new ParseConfiguration().setText("hällö");
|
||||
assertIterator(parseConfiguration);
|
||||
}
|
||||
|
||||
private void assertIterator(ParseConfiguration parseConfiguration) {
|
||||
StringBuffer text = new StringBuffer();
|
||||
for (Character character : parseConfiguration) {
|
||||
text.append(character);
|
||||
}
|
||||
assertEquals("hällö", text.toString());
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user