Package opennlp.tools.formats.conllu
Class ConlluDependencySampleStream
java.lang.Object
opennlp.tools.formats.conllu.ConlluDependencySampleStream
- All Implemented Interfaces:
AutoCloseable,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
FieldsModifier and TypeFieldDescriptionprivate static final charThe byte order mark some editors prepend to UTF-8 content.private static final intThe number of tab-separated columns of a CoNLL-U word line.private static final charThe first character of a comment line.private static final intThe column holding the relation label to the head.private static final charMarks an empty node id such as1.1.private static final charSeparates the columns of a word line.private booleanprivate static final intThe column holding the word form.private static final intThe column holding the one-based index of the head,0for the root.private static final intThe column holding the word index, a multiword token range, or an empty node id.private final InputStreamFactoryprivate static final org.slf4j.Loggerprivate static final charMarks a multiword token range id such as1-2.private static final StringThe CoNLL-U placeholder of a missing value.private BufferedReaderprivate static final StringThe HEAD column value of the root word, which has no governing word.private intprivate final intprivate static final intThe column holding the universal part-of-speech tag.private static final intThe column holding the language-specific part-of-speech tag.private static final charThe digit zero: the base of the digit values, and the one digit an ID must not start with. -
Constructor Summary
ConstructorsConstructorDescriptionInitializes the stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes theObjectStreamand releases all allocated resources.private DependencySampleConverts one sentence into a sample.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.private BufferedReaderopen()Opens a fresh UTF-8 reader over the content.read()Returns the nextObjectStreamobject.voidreset()Repositions the stream at the beginning and the previously seen object sequence will be repeated exactly.private intReads 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.
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
COLUMNS
private static final int COLUMNSThe number of tab-separated columns of a CoNLL-U word line.- See Also:
-
ID
private static final int IDThe column holding the word index, a multiword token range, or an empty node id.- See Also:
-
FORM
private static final int FORMThe column holding the word form.- See Also:
-
UPOS
private static final int UPOSThe column holding the universal part-of-speech tag.- See Also:
-
XPOS
private static final int XPOSThe column holding the language-specific part-of-speech tag.- See Also:
-
HEAD
private static final int HEADThe column holding the one-based index of the head,0for the root.- See Also:
-
ROOT_HEAD_ID
The HEAD column value of the root word, which has no governing word.- See Also:
-
DEPREL
private static final int DEPRELThe column holding the relation label to the head.- See Also:
-
FIELD_SEPARATOR
private static final char FIELD_SEPARATORSeparates the columns of a word line.- See Also:
-
MULTIWORD_RANGE
private static final char MULTIWORD_RANGEMarks a multiword token range id such as1-2.- See Also:
-
EMPTY_NODE
private static final char EMPTY_NODEMarks an empty node id such as1.1.- See Also:
-
PLACEHOLDER
The CoNLL-U placeholder of a missing value.- See Also:
-
BOM
private static final char BOMThe byte order mark some editors prepend to UTF-8 content.- See Also:
-
COMMENT
private static final char COMMENTThe first character of a comment line.- See Also:
-
ZERO_DIGIT
private static final char ZERO_DIGITThe digit zero: the base of the digit values, and the one digit an ID must not start with.- See Also:
-
in
-
tagColumn
private final int tagColumn -
reader
-
firstLine
private boolean firstLine -
skipped
private int skipped
-
-
Constructor Details
-
ConlluDependencySampleStream
Initializes the stream.- Parameters:
in- The CoNLL-U content. Must not benull.tagset- The tagset whose part-of-speech column feeds the sample tags. Must not benull.- Throws:
IOException- Thrown if opening the content fails.IllegalArgumentException- Thrown if any parameter isnull.
-
-
Method Details
-
read
Returns the nextObjectStreamobject. Calling this method repeatedly until it returnsnullwill 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:
readin interfaceObjectStream<DependencySample>- Returns:
- The next object or
nullto signal that the stream is exhausted. - Throws:
IOException- Thrown if there is an error during reading.
-
nextSentence
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
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 benull.- Returns:
- The position, or
-1if the column is not a plain decimal number: empty, signed, with a leading zero, with a non-ASCII digit, or too large for anint.
-
convert
Converts one sentence into a sample.- Parameters:
words- The word lines of the sentence.- Returns:
- The converted sample, or
nullwhen 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
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:
resetin interfaceObjectStream<DependencySample>- Throws:
IOException- Thrown if there is an error during resetting the stream.UnsupportedOperationException- Thrown if thereset()is not supported. By default, this is the case.
-
close
Closes theObjectStreamand releases all allocated resources. After close was called, it's not allowed to callObjectStream.read()orObjectStream.reset().- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceObjectStream<DependencySample>- Throws:
IOException- Thrown if there is an error during closing the stream.
-
open
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.
-