sentence

The Sentence module represents an entire CoNLL sentence, which is composed of two main parts: the comments and the tokens.

Comments

Comments are treated as key-value pairs, where the separating character between key and value is =. If there is no = present then then the comment is treated as a singleton, where the key is the comment string and the corresponding value is None. Read and write methods on this data can be found on methods prefixed with meta_.

For convenience, the id and text of a sentence can be accessed through member properties directly rather than through metadata methods. So sentence.id, rather than sentence.meta_value('id'). Since this API does not support changing a token’s form, the text comment cannot be changed.

Document and Paragraph ID

The document and paragraph id of a sentence are automatically inferred from a CoNLL treebank given sentence comments. Reassigning ids must be done through comments on the sentence level, and there is no API for simplifying this reassignment.

Tokens

These are the meat of the sentence. Tokens can be accessed through their id defined in the CoNLL annotation as a string or as a numeric index. So the same indexing syntax understands, sentence['5'], sentence['2-3'] and sentence[2].

API

Defines the Sentence type and the associated parsing and output logic.

class pyconll.unit.sentence.Sentence(source, _start_line_number=None)[source]

A sentence in a CoNLL-U file. A sentence consists of several components.

First, are comments. Each sentence must have two comments per UD v2 guidelines, which are sent_id and text. Comments are stored as a dict in the meta field. For singleton comments with no key-value structure, the value in the dict has a value of None.

Note the sent_id field is also assigned to the id property, and the text field is assigned to the text property for usability, and their importance as comments. The text property is read only along with the paragraph and document id. This is because the paragraph and document id are not defined per Sentence but across multiple sentences. Instead, these fields can be changed through changing the metadata of the Sentences.

Then comes the token annotations. Each sentence is made up of many token lines that provide annotation to the text provided. While a sentence usually means a collection of tokens, in this CoNLL-U sense, it is more useful to think of it as a collection of annotations with some associated metadata. Therefore the text of the sentence cannot be changed with this class, only the associated annotations can be changed.

__eq__(other)[source]

Defines equality for a sentence.

Parameters:other – The other Sentence to compare for equality against this one.
Returns:True if the this Sentence and the other one are the same. Sentences are the same when their comments are the same and their tokens are the same. Line numbers are not including in the equality definition.
__getitem__(key)[source]

Return the desired tokens from the Sentence.

Parameters:key – The indicator for the tokens to return. Can either be an integer, a string, or a slice. For an integer, the numeric indexes of Tokens are used. For a string, the id of the Token is used. And for a slice the start and end must be the same data types, and can be both string and integer.
Returns:If the key is a string then the appropriate Token. The key can also be a slice in which case a list of tokens is provided.
__init__(source, _start_line_number=None)[source]

Construct a Sentence object from the provided CoNLL-U string.

Parameters:
  • source – The raw CoNLL-U string to parse. Comments must precede token lines.
  • _start_line_number – The starting line of the sentence. For internal use.
Raises:

ParseError – If there is any token that was not valid.

__iter__()[source]

Iterate through all the tokens in the Sentence including multiword tokens.

__len__()[source]

Get the length of this sentence.

Returns:The amount of tokens in this sentence. In the CoNLL-U sense, this includes both all the multiword tokens and their decompositions.
conll()[source]

Convert the sentence to a CoNLL-U representation.

Returns:A string representing the Sentence in CoNLL-U format.
doc_id

Get the document id associated with this Sentence. Read-only.

Returns:The document id or None if no id is associated.
id

Get the sentence id.

Returns:The sentence id. If there is none, then returns None.
meta_present(key)[source]

Check if the key is present as a singleton or as a pair.

Parameters:key – The value to check for in the comments.
Returns:True if the key was provided as a singleton or as a key value pair. False otherwise.
meta_value(key)[source]

Returns the value associated with the key in the metadata (comments).

Parameters:key – The key whose value to look up.
Returns:The value associated with the key as a string. If the key is a singleton then None is returned.
Raises:KeyError – If the key is not present in the comments.
par_id

Get the paragraph id associated with this Sentence. Read-only.

Returns:The paragraph id or None if no id is associated.
set_meta(key, value=None)[source]

Set the metadata or comments associated with this Sentence.

Parameters:
  • key – The key for the comment.
  • value – The value to associate with the key. If the comment is a singleton, this field can be ignored or set to None.
text

Get the continuous text for this sentence. Read-only.

Returns:The continuous text of this sentence. If none is provided in comments, then None is returned.