Class TokenizerME

All Implemented Interfaces:
Probabilistic, Tokenizer

public class TokenizerME extends AbstractTokenizer implements Probabilistic
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 implementation needs a statistical model to tokenize a text which reproduces the tokenization observed in the training data used to create the model. The TokenizerModel class encapsulates that 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 TokenizerModel instance to safe memory.

To train a new model, the 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:
  • Field Details

  • Constructor Details

  • Method Details

    • probs

      public double[] probs()
      Retrieves the probabilities of the last decoded sequence. The sequence was determined based on the previous call to tokenizePos(String).
      Specified by:
      probs in interface Probabilistic
      Returns:
      An array with the same number of probabilities as tokens were sent to the computational method when tokenizePos(String) was last called. If not applicable an empty array is returned.
    • getTokenProbabilities

      @Deprecated(forRemoval=true, since="2.5.5") public double[] getTokenProbabilities()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use probs() instead.
      Returns:
      the probabilities associated with the most recent calls to tokenizePos(String). If not applicable an empty array is returned.
    • tokenizePos

      public Span[] tokenizePos(String d)
      Tokenizes the string.
      Specified by:
      tokenizePos in interface Tokenizer
      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 the TokenizerME.
      Parameters:
      samples - The samples used for the training.
      factory - A TokenizerFactory to get resources from.
      mlParams - The machine learning train parameters.
      Returns:
      A trained TokenizerModel.
      Throws:
      IOException - Thrown during IO operations on a temp file which is created during training. Or if reading from the ObjectStream fails.
    • useAlphaNumericOptimization

      public boolean useAlphaNumericOptimization()
      Returns:
      true if the tokenizer uses alphanumeric optimization, false otherwise.
    • isAcceptableAbbreviation

      protected boolean isAcceptableAbbreviation(CharSequence s)
      Allows checking a token abbreviation candidate for acceptability.

      Note: The implementation always returns false if no abbreviation dictionary is available for the underlying model.

      Parameters:
      s - the token to check for.
      Returns:
      true if the candidate is acceptable, false otherwise.