CoNLL-U
Loading...
Searching...
No Matches
sentence.hpp
1/*
2 * CoNLL-U - https://github.com/tugrulgungor/CoNLL-U
3 *
4 * Copyright (c) 2026. All rights reserved.
5 * Tuğrul Güngör - https://conll-u.tugrulgungor.me
6 *
7 * Distributed under the MIT License.
8 * https://opensource.org/license/mit/
9 */
10
14
15#ifndef TG_CONLLU_SENTENCE_HPP
16#define TG_CONLLU_SENTENCE_HPP 1
17
18#include "conllu/error.hpp"
19#include "conllu/internal/string_hash.hpp"
20#include "conllu/word.hpp"
21
22#include <expected>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
27namespace tg::conllu {
28 class Treebank;
29
35 class Sentence final {
36 std::string sent_id_{};
37 std::string text_{};
38
39 std::unordered_map<std::string, std::string, internal::StringHash, std::equal_to<>> metadata_{};
40
41 std::vector<std::unique_ptr<Token>> tokens_{};
42 std::vector<std::unique_ptr<Word>> words_{};
43
44 Treebank* treebank_;
45
46 static constexpr void split_metadata_line_(std::string_view& line, std::string_view& key,
47 std::string_view& value) noexcept;
48
49 [[nodiscard]] constexpr std::optional<std::error_code> set_known_metadata_(std::string_view key,
50 std::string_view value) noexcept;
51
52 [[nodiscard]] constexpr std::error_code set_sent_id_(std::string_view sent_id) noexcept;
53
54 constexpr std::expected<Word*, std::error_code> add_word_unsafe_(Word::Range range) noexcept;
55 constexpr void remove_empty_words_() noexcept;
56
57 explicit(true) constexpr Sentence(Treebank* treebank) noexcept;
58
59 friend class Parser;
60 friend class Token;
61 friend class Treebank;
62 friend class Word;
63 friend class Writer;
64
65 public:
73 constexpr Sentence() noexcept = delete;
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;
131 [[nodiscard]] constexpr std::optional<Token*> get_token_by_id(TokenId id) const noexcept;
136 [[nodiscard]] constexpr std::expected<Token*, std::error_code> add_token() noexcept;
144 constexpr std::error_code remove_token(TokenId id, bool skip_dependency_validation = false) noexcept;
152 constexpr std::error_code remove_token(Token* token, bool skip_dependency_validation = false) noexcept;
156 constexpr void remove_all_tokens() noexcept;
165 [[nodiscard]] constexpr auto words() const noexcept;
169 [[nodiscard]] constexpr std::size_t num_words() const noexcept;
177 [[nodiscard]] constexpr std::expected<Word*, std::error_code> add_word(TokenId first, TokenId last) 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;
198 constexpr void remove_all_words(bool remove_tokens = false) noexcept;
207 [[nodiscard]] constexpr auto metadata() const noexcept;
211 [[nodiscard]] constexpr std::size_t num_metadata_entries() 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;
231 [[nodiscard]] constexpr std::error_code set_metadata_from_comment_line(std::string_view line) noexcept;
237 constexpr std::error_code unset_metadata(std::string_view key) noexcept;
241 constexpr void remove_all_metadata() noexcept;
250 [[nodiscard]] constexpr bool empty() const noexcept;
254 constexpr void clear() noexcept;
259 [[nodiscard]] constexpr std::error_code validate() const noexcept;
268 constexpr ~Sentence() noexcept = default;
270 };
271} // namespace tg::conllu
272
273#endif // TG_CONLLU_SENTENCE_HPP
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