Class DependencyParserME

java.lang.Object
opennlp.tools.depparse.DependencyParserME
All Implemented Interfaces:
DependencyParser

@ThreadSafe public class DependencyParserME extends Object implements DependencyParser
A greedy transition-based DependencyParser: a classifier picks the next arc-standard Transition for each configuration until the parse is complete, always taking the highest scoring transition that is applicable.

The parser holds an immutable model and no per-parse state, so one instance can be shared between threads.

Since:
3.0.0
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • decodeOutcomes

      private Transition[] decodeOutcomes(MaxentModel model)
      Decodes the outcome inventory once, so that decoding a sentence indexes it instead of parsing an outcome string per configuration and outcome.
      Parameters:
      model - The transition classification model.
      Returns:
      The transitions by outcome index. Never null.
      Throws:
      IllegalArgumentException - Thrown if an outcome does not decode to a transition, which means the model is not a dependency parser model, a transition repeats, or the model has no SHIFT or no RIGHT_ARC action.
    • parse

      public DependencyTree parse(String[] tokens, String[] tags)
      Parses a sentence into a DependencyTree.
      Specified by:
      parse in interface DependencyParser
      Parameters:
      tokens - The input tokens. Must not be null, must contain at least one token, and must not contain null entries.
      tags - The part-of-speech tags aligned with tokens. Must not be null, must have the same length as tokens, and must not contain null entries.
      Returns:
      A DependencyTree over the given tokens. Never null.
      Throws:
      IllegalStateException - Thrown if the model returns scores that do not match its outcomes or are not finite.
    • checkTokensAndTags

      private void checkTokensAndTags(String[] tokens, String[] tags)
      Validates the input of parse(String[], String[]), with the same rules and messages as DependencySample.
      Parameters:
      tokens - The token array. Must not be null or empty and must not contain null entries.
      tags - The part-of-speech tags aligned with tokens. Must not be null, must have the same length as tokens, and must not contain null entries.
      Throws:
      IllegalArgumentException - Thrown if an array is null, tokens is empty, the lengths do not match, or an entry is null.
    • bestApplicable

      private Transition bestApplicable(ArcStandardState state, String[] tokens, String[] tags)
      Scores all outcomes for the current configuration and picks the best transition that is applicable; inapplicable outcomes are passed over regardless of score.
      Parameters:
      state - The current configuration.
      tokens - The sentence tokens.
      tags - The part-of-speech tags aligned with tokens.
      Returns:
      The highest scoring applicable transition. Never null.
      Throws:
      IllegalStateException - Thrown if the model returns a score count that does not match its outcomes, a non-finite score, or no applicable transition.
    • train

      public static DependencyModel train(String languageCode, ObjectStream<DependencySample> samples, TrainingParameters parameters) throws IOException
      Trains a greedy arc-standard parser model from dependency samples.

      Non-projective samples have no arc-standard derivation and are skipped during event generation.

      Parameters:
      languageCode - The ISO language code of the training data. Must not be null.
      samples - The training samples. Must not be null.
      parameters - The TrainingParameters. Must not be null and must select an event model trainer.
      Returns:
      A trained DependencyModel. Never null.
      Throws:
      IOException - Thrown if reading the samples fails.
      IllegalArgumentException - Thrown if a parameter is null or the configured trainer is not an event model trainer.