15#ifndef TG_CONLLU_SENTENCE_HPP
16#define TG_CONLLU_SENTENCE_HPP 1
18#include "conllu/error.hpp"
19#include "conllu/internal/string_hash.hpp"
20#include "conllu/word.hpp"
24#include <unordered_map>
35 class Sentence final {
36 std::string sent_id_{};
39 std::unordered_map<std::string, std::string, internal::StringHash, std::equal_to<>> metadata_{};
41 std::vector<std::unique_ptr<Token>> tokens_{};
42 std::vector<std::unique_ptr<Word>> words_{};
46 static constexpr void split_metadata_line_(std::string_view& line, std::string_view& key,
47 std::string_view& value)
noexcept;
49 [[nodiscard]]
constexpr std::optional<std::error_code> set_known_metadata_(std::string_view key,
50 std::string_view value)
noexcept;
52 [[nodiscard]]
constexpr std::error_code set_sent_id_(std::string_view sent_id)
noexcept;
54 constexpr std::expected<Word*, std::error_code> add_word_unsafe_(
Word::Range range)
noexcept;
55 constexpr void remove_empty_words_()
noexcept;
61 friend class Treebank;
82 [[nodiscard]] constexpr Treebank*
treebank() const noexcept;
91 [[nodiscard]] constexpr std::string_view
get_sent_id() const noexcept;
97 constexpr std::error_code
set_sent_id(std::string_view sent_id) noexcept;
106 [[nodiscard]] constexpr std::string_view
get_text() const noexcept;
112 [[nodiscard]] constexpr std::error_code
set_text(std::string_view text) noexcept;
121 [[nodiscard]] constexpr auto
tokens() const noexcept;
125 [[nodiscard]] constexpr std::
size_t num_tokens() const noexcept;
136 [[nodiscard]] constexpr std::expected<Token*, std::error_code>
add_token() noexcept;
152 constexpr std::error_code
remove_token(Token* token,
bool skip_dependency_validation = false) noexcept;
165 [[nodiscard]] constexpr auto
words() const noexcept;
169 [[nodiscard]] constexpr std::
size_t num_words() const noexcept;
184 [[nodiscard]] constexpr std::expected<Word*, std::error_code>
add_word(Word::Range range) noexcept;
192 constexpr std::error_code
remove_word(const Word* word,
bool remove_tokens = false) noexcept;
207 [[nodiscard]] constexpr auto
metadata() const noexcept;
217 [[nodiscard]] constexpr std::optional<std::string_view>
get_metadata(std::string_view key) const noexcept;
224 [[nodiscard]] constexpr std::error_code
set_metadata(std::string_view key,
225 std::string_view value = std::string_view{})
noexcept;
250 [[nodiscard]] constexpr
bool empty() const noexcept;
259 [[nodiscard]] constexpr std::error_code
validate() const noexcept;
268 constexpr ~Sentence() noexcept = default;
constexpr std::error_code remove_token(TokenId id, bool skip_dependency_validation=false) noexcept
Removes the token with the given identifier.
constexpr void remove_all_tokens() noexcept
Removes all tokens from the sentence.
constexpr auto tokens() const noexcept
View over the tokens, in sentence order.
constexpr std::error_code remove_word(const Word *word, bool remove_tokens=false) noexcept
Removes the given word.
constexpr auto metadata() const noexcept
View over the metadata entries, as key/value pairs.
constexpr void clear() noexcept
Removes all tokens and metadata (except sent_id), resetting the sentence's content.
constexpr std::optional< std::string_view > get_metadata(std::string_view key) const noexcept
Looks up a metadata value by key.
constexpr std::string_view get_sent_id() const noexcept
Sentence identifier (sent_id metadata).
constexpr std::error_code validate() const noexcept
Checks the sentence for structural inconsistencies.
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::size_t num_metadata_entries() const noexcept
Number of metadata entries.
constexpr std::optional< Token * > get_token_by_id(TokenId id) const noexcept
Looks up a token by its identifier.
constexpr void remove_all_metadata() noexcept
Removes all metadata entries except sent_id, and clears the text.
constexpr void remove_all_words(bool remove_tokens=false) noexcept
Removes all words from the sentence.
constexpr Treebank * treebank() const noexcept
Pointer to the treebank that owns this sentence.
constexpr std::string_view get_text() const noexcept
Sentence text (text metadata).
constexpr std::size_t num_tokens() const noexcept
Number of tokens in the sentence.
constexpr std::expected< Token *, std::error_code > add_token() noexcept
Constructs and appends a new token.
constexpr auto words() const noexcept
View over the words, in treebank/range order.
constexpr std::error_code set_metadata(std::string_view key, std::string_view value=std::string_view{}) noexcept
Adds a new metadata entry.
constexpr bool empty() const noexcept
Whether the sentence contains no tokens.
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 std::size_t num_words() const noexcept
Number of words in the sentence.
constexpr std::error_code set_text(std::string_view text) noexcept
Sets the sentence text.
constexpr std::error_code set_sent_id(std::string_view sent_id) noexcept
Changes the sentence identifier.
constexpr Sentence() noexcept=delete
Default construction is disabled; sentences are created via Treebank::add_sentence.
In-memory representation of a CoNLL-U treebank.
Definition treebank.hpp:36
std::uint64_t TokenId
Identifier for a token or empty node within a sentence.
Definition token.hpp:36
A closed, inclusive interval of Tokens' identifiers, spanning [first, last].
Definition word.hpp:34