Class FeedforwardDependencyParser

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

@ThreadSafe public class FeedforwardDependencyParser extends Object implements DependencyParser
The neural DependencyParser: an arc-standard decoder over the FeedforwardDependencyModel. By default it decodes with the beam size the model was refined with, or greedily for a model that was not refined; a beam size given to the constructor overrides that. It scores configurations with learned dense representations instead of the sparse feature conjunctions of DependencyParserME.

Beam decoding retains the highest scoring transition sequences by summed log-probability. Complete arc-standard derivations for a sentence have equal length, so their scores need no length normalization.

The parser does not modify its model and keeps no per-parse state, so one instance can be shared between threads. An optional scoring cache, owned by the parser and bounded in bytes, stores the hidden-layer contribution of each feature position and embedding row it has scored.

Since:
3.0.0
See Also:
  • Field Details

    • GREEDY_BEAM_SIZE

      private static final int GREEDY_BEAM_SIZE
      The beam size at which decoding is greedy.
      See Also:
    • NO_CACHE

      private static final long NO_CACHE
      The scoring cache budget at which no cache is created.
      See Also:
    • network

      private final FeedforwardNetwork network
    • transitions

      private final Transition[] transitions
    • beamSize

      private final int beamSize
    • cache

      private final ContributionCache cache
  • Constructor Details

    • FeedforwardDependencyParser

      public FeedforwardDependencyParser(FeedforwardDependencyModel model)
      Initializes a FeedforwardDependencyParser with the beam size of the model and without a scoring cache.
      Parameters:
      model - The model to parse with. Must not be null.
      Throws:
      IllegalArgumentException - Thrown if model is null.
      See Also:
    • FeedforwardDependencyParser

      public FeedforwardDependencyParser(FeedforwardDependencyModel model, int beamSize)
      Initializes a FeedforwardDependencyParser with a beam and without a scoring cache.
      Parameters:
      model - The model to parse with. Must not be null.
      beamSize - The number of transition sequences to retain. Must be positive; 1 decodes greedily.
      Throws:
      IllegalArgumentException - Thrown if model is null or beamSize is not positive.
    • FeedforwardDependencyParser

      public FeedforwardDependencyParser(FeedforwardDependencyModel model, int beamSize, long scoringCacheBytes)
      Initializes a FeedforwardDependencyParser with a beam and a scoring cache.
      Parameters:
      model - The model to parse with. Must not be null.
      beamSize - The number of transition sequences to retain. Must be positive; 1 decodes greedily.
      scoringCacheBytes - The bytes the scoring cache may use, or 0 for no cache. Must not be negative. The cache needs room for one slot per vocabulary entry and feature position before it stores a contribution.
      Throws:
      IllegalArgumentException - Thrown if model is null, beamSize is not positive, scoringCacheBytes is negative, or a positive scoringCacheBytes is too small for the model's vocabularies.
  • Method Details

    • 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 no model outcome is applicable in a configuration or the model produces a non-finite transition score.
    • greedyParse

      private DependencyTree greedyParse(FeedforwardContext context, int tokenCount)
      Decodes greedily: the highest scoring applicable transition wins each step.
      Parameters:
      context - The sentence features.
      tokenCount - The sentence length.
      Returns:
      The parse. Never null.
      Throws:
      IllegalStateException - Thrown if no model outcome is applicable in a configuration or the model produces a non-finite transition score.
    • beamParse

      private DependencyTree beamParse(FeedforwardContext context, int tokenCount)
      Decodes with a beam and returns its highest scoring complete sequence.
      Parameters:
      context - The sentence features.
      tokenCount - The sentence length.
      Returns:
      The parse. Never null.
      Throws:
      IllegalStateException - Thrown if no beam path can be advanced by a model outcome or the model produces a non-finite transition score.
    • beamSize

      int beamSize()
      Returns the number of transition sequences the parser retains.
      Returns:
      The beam size. Always positive; 1 decodes greedily.
    • cache

      Returns:
      The scoring cache, or null if the parser was created without one.