Class ConlluDependencySampleStream

java.lang.Object
opennlp.tools.formats.conllu.ConlluDependencySampleStream
All Implemented Interfaces:
AutoCloseable, ObjectStream<DependencySample>

public class ConlluDependencySampleStream extends Object implements ObjectStream<DependencySample>
Reads samples directly from CoNLL-U content, mapping the HEAD and DEPREL columns of the basic dependency annotation.

This reader does not use ConlluStream, which merges multiword token ranges with their syntactic words for token and lemma samples. Dependency samples instead omit range lines and empty nodes while retaining the syntactic word rows. Sentences with incomplete or invalid basic dependencies are skipped and counted.

Since:
3.0.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final char
    The byte order mark some editors prepend to UTF-8 content.
    private static final int
    The number of tab-separated columns of a CoNLL-U word line.
    private static final char
    The first character of a comment line.
    private static final int
    The column holding the relation label to the head.
    private static final char
    Marks an empty node id such as 1.1.
    private static final char
    Separates the columns of a word line.
    private boolean
     
    private static final int
    The column holding the word form.
    private static final int
    The column holding the one-based index of the head, 0 for the root.
    private static final int
    The column holding the word index, a multiword token range, or an empty node id.
    private final InputStreamFactory
     
    private static final org.slf4j.Logger
     
    private static final char
    Marks a multiword token range id such as 1-2.
    private static final String
    The CoNLL-U placeholder of a missing value.
     
    private static final String
    The HEAD column value of the root word, which has no governing word.
    private int
     
    private final int
     
    private static final int
    The column holding the universal part-of-speech tag.
    private static final int
    The column holding the language-specific part-of-speech tag.
    private static final char
    The digit zero: the base of the digit values, and the one digit an ID must not start with.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes the stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the ObjectStream and releases all allocated resources.
    convert(List<String[]> words)
    Converts one sentence into a sample.
    private List<String[]>
    Reads the syntactic word lines of the next sentence: comments, multiword token ranges, and empty nodes are dropped; an empty list means the end of the content.
    Opens a fresh UTF-8 reader over the content.
    Returns the next ObjectStream object.
    void
    Repositions the stream at the beginning and the previously seen object sequence will be repeated exactly.
    private int
    Reads a word ID, the one-based position of a syntactic word in its sentence, from the ID column or from the HEAD column of another word.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • COLUMNS

      private static final int COLUMNS
      The number of tab-separated columns of a CoNLL-U word line.
      See Also:
    • ID

      private static final int ID
      The column holding the word index, a multiword token range, or an empty node id.
      See Also:
    • FORM

      private static final int FORM
      The column holding the word form.
      See Also:
    • UPOS

      private static final int UPOS
      The column holding the universal part-of-speech tag.
      See Also:
    • XPOS

      private static final int XPOS
      The column holding the language-specific part-of-speech tag.
      See Also:
    • ROOT_HEAD_ID

      private static final String ROOT_HEAD_ID
      The HEAD column value of the root word, which has no governing word.
      See Also:
    • DEPREL

      private static final int DEPREL
      The column holding the relation label to the head.
      See Also:
    • FIELD_SEPARATOR

      private static final char FIELD_SEPARATOR
      Separates the columns of a word line.
      See Also:
    • MULTIWORD_RANGE

      private static final char MULTIWORD_RANGE
      Marks a multiword token range id such as 1-2.
      See Also:
    • EMPTY_NODE

      private static final char EMPTY_NODE
      Marks an empty node id such as 1.1.
      See Also:
    • PLACEHOLDER

      private static final String PLACEHOLDER
      The CoNLL-U placeholder of a missing value.
      See Also:
    • BOM

      private static final char BOM
      The byte order mark some editors prepend to UTF-8 content.
      See Also:
    • COMMENT

      private static final char COMMENT
      The first character of a comment line.
      See Also:
    • ZERO_DIGIT

      private static final char ZERO_DIGIT
      The digit zero: the base of the digit values, and the one digit an ID must not start with.
      See Also:
    • in

      private final InputStreamFactory in
    • tagColumn

      private final int tagColumn
    • reader

      private BufferedReader reader
    • firstLine

      private boolean firstLine
    • skipped

      private int skipped
  • Constructor Details

    • ConlluDependencySampleStream

      public ConlluDependencySampleStream(InputStreamFactory in, ConlluTagset tagset) throws IOException
      Initializes the stream.
      Parameters:
      in - The CoNLL-U content. Must not be null.
      tagset - The tagset whose part-of-speech column feeds the sample tags. Must not be null.
      Throws:
      IOException - Thrown if opening the content fails.
      IllegalArgumentException - Thrown if any parameter is null.
  • Method Details

    • read

      public DependencySample read() throws IOException
      Returns the next ObjectStream object. Calling this method repeatedly until it returns null will return each object from the underlying source exactly once.

      Sentences without a usable basic dependency annotation are skipped, and their count is logged once the content is exhausted.

      Specified by:
      read in interface ObjectStream<DependencySample>
      Returns:
      The next object or null to signal that the stream is exhausted.
      Throws:
      IOException - Thrown if there is an error during reading.
    • nextSentence

      private List<String[]> nextSentence() throws IOException
      Reads the syntactic word lines of the next sentence: comments, multiword token ranges, and empty nodes are dropped; an empty list means the end of the content.

      Sentences are separated by any line StringUtil.isBlank(CharSequence) accepts.

      Returns:
      The word lines of the next sentence, or an empty list at the end of the content. Never null.
      Throws:
      IOException - Thrown if reading fails.
      InvalidFormatException - Thrown if a word line does not have the expected column count.
    • wordIndex

      private int wordIndex(String id)
      Reads a word ID, the one-based position of a syntactic word in its sentence, from the ID column or from the HEAD column of another word.
      Parameters:
      id - The column value. Must not be null.
      Returns:
      The position, or -1 if the column is not a plain decimal number: empty, signed, with a leading zero, with a non-ASCII digit, or too large for an int.
    • convert

      private DependencySample convert(List<String[]> words)
      Converts one sentence into a sample.
      Parameters:
      words - The word lines of the sentence.
      Returns:
      The converted sample, or null when the sentence's annotation is unusable: an ID or head that is not a plain decimal, an underscore head, selected tag or relation, or a graph that is not a tree.
    • reset

      public void reset() throws IOException, UnsupportedOperationException
      Repositions the stream at the beginning and the previously seen object sequence will be repeated exactly. This method can be used to re-read the stream if multiple passes over the objects are required.

      The implementation of this method is optional.

      Reopens the content through the InputStreamFactory, which must therefore produce a fresh stream on every call.

      Specified by:
      reset in interface ObjectStream<DependencySample>
      Throws:
      IOException - Thrown if there is an error during resetting the stream.
      UnsupportedOperationException - Thrown if the reset() is not supported. By default, this is the case.
    • close

      public void close() throws IOException
      Closes the ObjectStream and releases all allocated resources. After close was called, it's not allowed to call ObjectStream.read() or ObjectStream.reset().
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface ObjectStream<DependencySample>
      Throws:
      IOException - Thrown if there is an error during closing the stream.
    • open

      private BufferedReader open() throws IOException
      Opens a fresh UTF-8 reader over the content.
      Returns:
      A reader positioned at the start of the content. Never null.
      Throws:
      IOException - Thrown if opening the content fails.