Class TokenizerME
- java.lang.Object
-
- opennlp.tools.tokenize.TokenizerME
-
- All Implemented Interfaces:
Tokenizer
public class TokenizerME extends Object
A Tokenizer for converting raw text into separated tokens. It uses Maximum Entropy to make its decisions. The features are loosely based off of Jeff Reynar's UPenn thesis "Topic Segmentation: Algorithms and Applications.", which is available from his homepage: http://www.cis.upenn.edu/~jcreynar.This tokenizer needs a statistical model to tokenize a text which reproduces the tokenization observed in the training data used to create the model. The
TokenizerModelclass encapsulates the model and provides methods to create it from the binary representation.A tokenizer instance is not thread safe. For each thread one tokenizer must be instantiated which can share one
TokenizerModelinstance to safe memory.To train a new model {
train(ObjectStream, TokenizerFactory, TrainingParameters)method can be used.Sample usage:
InputStream modelIn;
...
TokenizerModel model = TokenizerModel(modelIn);
Tokenizer tokenizer = new TokenizerME(model);
String tokens[] = tokenizer.tokenize("A sentence to be tokenized.");- See Also:
Tokenizer,TokenizerModel,TokenSample
-
-
Field Summary
Fields Modifier and Type Field Description static PatternalphaNumericDeprecated.As of release 1.5.2, replaced byFactory.getAlphanumeric(String)static StringNO_SPLITConstant indicates no token split.static StringSPLITConstant indicates a token split.
-
Constructor Summary
Constructors Constructor Description TokenizerME(String language)Initializes the tokenizer by downloading a default model.TokenizerME(TokenizerModel model)TokenizerME(TokenizerModel model, Factory factory)Deprecated.useTokenizerFactoryto extend the Tokenizer functionality
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double[]getTokenProbabilities()Returns the probabilities associated with the most recent calls toTokenizer.tokenize(String)ortokenizePos(String).voidsetKeepNewLines(boolean keepNewLines)String[]tokenize(String s)Splits a string into its atomic partsSpan[]tokenizePos(String d)Tokenizes the string.static TokenizerModeltrain(ObjectStream<TokenSample> samples, TokenizerFactory factory, TrainingParameters mlParams)Trains a model for theTokenizerME.booleanuseAlphaNumericOptimization()Returns the value of the alpha-numeric optimization flag.
-
-
-
Field Detail
-
SPLIT
public static final String SPLIT
Constant indicates a token split.- See Also:
- Constant Field Values
-
NO_SPLIT
public static final String NO_SPLIT
Constant indicates no token split.- See Also:
- Constant Field Values
-
alphaNumeric
@Deprecated public static final Pattern alphaNumeric
Deprecated.As of release 1.5.2, replaced byFactory.getAlphanumeric(String)Alpha-Numeric Pattern
-
-
Constructor Detail
-
TokenizerME
public TokenizerME(String language) throws IOException
Initializes the tokenizer by downloading a default model.- Parameters:
language- The language of the tokenizer.- Throws:
IOException- Thrown if the model cannot be downloaded or saved.
-
TokenizerME
public TokenizerME(TokenizerModel model)
-
TokenizerME
public TokenizerME(TokenizerModel model, Factory factory)
Deprecated.useTokenizerFactoryto extend the Tokenizer functionality
-
-
Method Detail
-
getTokenProbabilities
public double[] getTokenProbabilities()
Returns the probabilities associated with the most recent calls toTokenizer.tokenize(String)ortokenizePos(String).- Returns:
- probability for each token returned for the most recent call to tokenize. If not applicable an empty array is returned.
-
tokenizePos
public Span[] tokenizePos(String d)
Tokenizes the string.- Parameters:
d- The string to be tokenized.- Returns:
- A span array containing individual tokens as elements.
-
train
public static TokenizerModel train(ObjectStream<TokenSample> samples, TokenizerFactory factory, TrainingParameters mlParams) throws IOException
Trains a model for theTokenizerME.- Parameters:
samples- the samples used for the training.factory- aTokenizerFactoryto get resources frommlParams- the machine learning train parameters- Returns:
- the trained
TokenizerModel - Throws:
IOException- it throws anIOExceptionif anIOExceptionis thrown during IO operations on a temp file which is created during training. Or if reading from theObjectStreamfails.
-
useAlphaNumericOptimization
public boolean useAlphaNumericOptimization()
Returns the value of the alpha-numeric optimization flag.- Returns:
- true if the tokenizer should use alpha-numeric optimization, false otherwise.
-
tokenize
public String[] tokenize(String s)
Description copied from interface:TokenizerSplits a string into its atomic parts
-
setKeepNewLines
public void setKeepNewLines(boolean keepNewLines)
-
-