CoNLL-U
Loading...
Searching...
No Matches
tg::conllu::Sentence Class Referencefinal

A single sentence within a Treebank. More...

#include <sentence.hpp>

Public Member Functions

Constructors
constexpr Sentence () noexcept=delete
 Default construction is disabled; sentences are created via Treebank::add_sentence.
Treebank
constexpr Treebank * treebank () const noexcept
 Pointer to the treebank that owns this sentence.
Identity
constexpr std::string_view get_sent_id () const noexcept
 Sentence identifier (sent_id metadata).
constexpr std::error_code set_sent_id (std::string_view sent_id) noexcept
 Changes the sentence identifier.
Text
constexpr std::string_view get_text () const noexcept
 Sentence text (text metadata).
constexpr std::error_code set_text (std::string_view text) noexcept
 Sets the sentence text.
Tokens
constexpr auto tokens () const noexcept
 View over the tokens, in sentence order.
constexpr std::size_t num_tokens () const noexcept
 Number of tokens in the sentence.
constexpr std::optional< Token * > get_token_by_id (TokenId id) const noexcept
 Looks up a token by its identifier.
constexpr std::expected< Token *, std::error_code > add_token () noexcept
 Constructs and appends a new token.
constexpr std::error_code remove_token (TokenId id, bool skip_dependency_validation=false) noexcept
 Removes the token with the given identifier.
constexpr std::error_code remove_token (Token *token, bool skip_dependency_validation=false) noexcept
 Removes the given token.
constexpr void remove_all_tokens () noexcept
 Removes all tokens from the sentence.
Words
constexpr auto words () const noexcept
 View over the words, in treebank/range order.
constexpr std::size_t num_words () const noexcept
 Number of words in the sentence.
constexpr std::expected< Word *, std::error_code > add_word (TokenId first, TokenId last) noexcept
 Constructs and appends a new word spanning the given token range.
constexpr std::expected< Word *, std::error_code > add_word (Word::Range range) noexcept
 Constructs and appends a new word spanning the given token range.
constexpr std::error_code remove_word (const Word *word, bool remove_tokens=false) noexcept
 Removes the given word.
constexpr void remove_all_words (bool remove_tokens=false) noexcept
 Removes all words from the sentence.
Metadata
constexpr auto metadata () const noexcept
 View over the metadata entries, as key/value pairs.
constexpr std::size_t num_metadata_entries () const noexcept
 Number of metadata entries.
constexpr std::optional< std::string_view > get_metadata (std::string_view key) const noexcept
 Looks up a metadata value by key.
constexpr std::error_code set_metadata (std::string_view key, std::string_view value=std::string_view{}) noexcept
 Adds a new metadata entry.
constexpr std::error_code set_metadata_from_comment_line (std::string_view line) noexcept
 Parses a #-prefixed comment line and adds it as a metadata entry.
constexpr std::error_code unset_metadata (std::string_view key) noexcept
 Removes a metadata entry.
constexpr void remove_all_metadata () noexcept
 Removes all metadata entries except sent_id, and clears the text.
State
constexpr bool empty () const noexcept
 Whether the sentence contains no tokens.
constexpr void clear () noexcept
 Removes all tokens and metadata (except sent_id), resetting the sentence's content.
constexpr std::error_code validate () const noexcept
 Checks the sentence for structural inconsistencies.
Destructor
constexpr ~Sentence () noexcept=default
 Destructor.

Detailed Description

A single sentence within a Treebank.

Member Function Documentation

◆ add_token()

std::expected< Token *, std::error_code > tg::conllu::Sentence::add_token ( )
nodiscardconstexprnoexcept

Constructs and appends a new token.

Returns
Pointer to the newly created token, or an error_code on failure.

◆ add_word() [1/2]

std::expected< Word *, std::error_code > tg::conllu::Sentence::add_word ( TokenId first,
TokenId last )
nodiscardconstexprnoexcept

Constructs and appends a new word spanning the given token range.

Parameters
[in]firstIdentifier of the first token in the range.
[in]lastIdentifier of the last token in the range.
Returns
Pointer to the newly created word, or an error_code if the range is invalid or overlaps an existing word.

◆ add_word() [2/2]

std::expected< Word *, std::error_code > tg::conllu::Sentence::add_word ( Word::Range range)
nodiscardconstexprnoexcept

Constructs and appends a new word spanning the given token range.

Parameters
[in]rangeToken range for the new word.
Returns
Pointer to the newly created word, or an error_code if the range is invalid or overlaps an existing word.

◆ get_metadata()

std::optional< std::string_view > tg::conllu::Sentence::get_metadata ( std::string_view key) const
nodiscardconstexprnoexcept

Looks up a metadata value by key.

Parameters
[in]keyMetadata key to search for.
Returns
Metadata value, or std::nullopt if no such key exists.

◆ get_token_by_id()

std::optional< Token * > tg::conllu::Sentence::get_token_by_id ( TokenId id) const
nodiscardconstexprnoexcept

Looks up a token by its identifier.

Parameters
[in]idToken identifier to search for.
Returns
Pointer to the token, or std::nullopt if no such token exists.

◆ remove_all_words()

void tg::conllu::Sentence::remove_all_words ( bool remove_tokens = false)
constexprnoexcept

Removes all words from the sentence.

Parameters
[in]remove_tokensIf true, also removes every token covered by each word's range; if false, only the word groupings are removed and their tokens remain.

◆ remove_token() [1/2]

std::error_code tg::conllu::Sentence::remove_token ( Token * token,
bool skip_dependency_validation = false )
constexprnoexcept

Removes the given token.

Parameters
[in]tokenPointer to the token to remove.
[in]skip_dependency_validationIf false, fails when another token's HEAD references token; if true, clears such HEAD references instead of failing.
Returns
Empty error_code on success, or an error describing the failure.

◆ remove_token() [2/2]

std::error_code tg::conllu::Sentence::remove_token ( TokenId id,
bool skip_dependency_validation = false )
constexprnoexcept

Removes the token with the given identifier.

Parameters
[in]idIdentifier of the token to remove.
[in]skip_dependency_validationIf false, fails when another token's HEAD references id; if true, clears such HEAD references instead of failing.
Returns
Empty error_code on success, or an error describing the failure.

◆ remove_word()

std::error_code tg::conllu::Sentence::remove_word ( const Word * word,
bool remove_tokens = false )
constexprnoexcept

Removes the given word.

Parameters
[in]wordPointer to the word to remove.
[in]remove_tokensIf true, also removes every token covered by the word's range (clearing any dangling HEAD references); if false, only the word grouping is removed and its tokens remain in the sentence.
Returns
Empty error_code on success, or an error describing the failure.

◆ set_metadata()

std::error_code tg::conllu::Sentence::set_metadata ( std::string_view key,
std::string_view value = std::string_view{} )
nodiscardconstexprnoexcept

Adds a new metadata entry.

Parameters
[in]keyMetadata key; must be non-empty and not already present.
[in]valueMetadata value.
Returns
Empty error_code on success, or an error describing the failure.

◆ set_metadata_from_comment_line()

std::error_code tg::conllu::Sentence::set_metadata_from_comment_line ( std::string_view line)
nodiscardconstexprnoexcept

Parses a #-prefixed comment line and adds it as a metadata entry.

Parameters
[in]lineComment line, in # key = value or # key form.
Returns
Empty error_code on success, or an error describing the failure.

◆ set_sent_id()

std::error_code tg::conllu::Sentence::set_sent_id ( std::string_view sent_id)
constexprnoexcept

Changes the sentence identifier.

Parameters
[in]sent_idNew identifier for the sentence.
Returns
Empty error_code on success, or an error describing the failure.

◆ set_text()

std::error_code tg::conllu::Sentence::set_text ( std::string_view text)
nodiscardconstexprnoexcept

Sets the sentence text.

Parameters
[in]textNew text for the sentence.
Returns
Empty error_code on success, or an error describing the failure.

◆ unset_metadata()

std::error_code tg::conllu::Sentence::unset_metadata ( std::string_view key)
constexprnoexcept

Removes a metadata entry.

Parameters
[in]keyMetadata key to remove; the sent_id key cannot be removed.
Returns
Empty error_code on success, or an error describing the failure.

◆ validate()

std::error_code tg::conllu::Sentence::validate ( ) const
nodiscardconstexprnoexcept

Checks the sentence for structural inconsistencies.

Returns
Empty error_code on success, or an error describing the first issue found.

The documentation for this class was generated from the following file: