Created
November 9, 2013 08:27
-
-
Save anonymous/7383104 to your computer and use it in GitHub Desktop.
dummy token filter based on the SynonymFilter from "Lucene In Action"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final class TwitterFilter extends TokenFilter { | |
private final Stack<String> synonyms; | |
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class); | |
public TwitterFilter(TokenStream in) { | |
super(in); | |
synonyms = new Stack<String>(); // In theory we don't need a stack here | |
} | |
@Override | |
public final boolean incrementToken() throws IOException { | |
if (synonyms.size() > 0) { | |
final String synonym = synonyms.pop(); | |
termAtt.setEmpty(); | |
termAtt.append(synonym); | |
return true; | |
} else if (input.incrementToken()) { | |
final char[] buffer = termAtt.buffer(); | |
final int length = termAtt.length(); | |
final char firstChar = buffer[0]; | |
if ((firstChar == '#' || firstChar == '@') && length > 1) { | |
// Adds two synonyms to the stack so that they are recognized as separate terms | |
final String term = new String(buffer, 1, length); | |
synonyms.push(term.trim()); | |
} | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment