CoNLL-U
Loading...
Searching...
No Matches
word.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_WORD_HPP
16#define TG_CONLLU_WORD_HPP 1
17
18#include "conllu/token.hpp"
19
20namespace tg::conllu {
21 class Sentence;
22
28 class Word final {
29 public:
34 struct Range final {
43
48 [[nodiscard]] constexpr std::size_t count() const noexcept;
54 [[nodiscard]] constexpr bool contains(TokenId id) const noexcept;
60 [[nodiscard]] constexpr bool overlaps(Range range) const noexcept;
66 [[nodiscard]] constexpr bool equals(Range range) const noexcept;
71 [[nodiscard]] constexpr bool is_valid() const noexcept;
77 [[nodiscard]] constexpr bool is_valid(TokenId max) const noexcept;
78 };
79
80 private:
81 Range range_{};
82 std::optional<std::string> form_{std::nullopt};
83 std::optional<std::string> misc_{std::nullopt};
84 std::optional<bool> typo_{std::nullopt};
85
86 Sentence* sentence_{nullptr};
87
88 explicit(true) constexpr Word(Sentence* sentence) noexcept;
89
90 friend class Sentence;
91 friend class Writer;
92
93 public:
101 constexpr Word() noexcept = delete;
113 constexpr std::error_code set_range(Range range) noexcept;
120 constexpr std::error_code set_range(TokenId first, TokenId last) noexcept;
124 [[nodiscard]] constexpr TokenId first_id() const noexcept;
128 [[nodiscard]] constexpr TokenId last_id() const noexcept;
137 [[nodiscard]] constexpr std::optional<std::string_view> get_form() const noexcept;
143 constexpr std::error_code set_form(const std::optional<std::string_view>& form) noexcept;
147 constexpr void unset_form() noexcept;
156 [[nodiscard]] constexpr std::optional<std::string_view> get_misc() const noexcept;
162 constexpr std::error_code set_misc(const std::optional<std::string_view>& misc) noexcept;
166 constexpr void unset_misc() noexcept;
175 [[nodiscard]] constexpr std::optional<bool> get_typo() const noexcept;
180 constexpr void set_typo(std::optional<bool> typo) noexcept;
184 constexpr void unset_typo() noexcept;
193 [[nodiscard]] constexpr auto tokens() const noexcept;
197 [[nodiscard]] constexpr std::size_t num_tokens() const noexcept;
203 [[nodiscard]] constexpr bool contains_token(Token* token) const noexcept;
209 [[nodiscard]] constexpr bool contains_token_id(TokenId id) const noexcept;
219 [[nodiscard]] constexpr std::error_code validate() const noexcept;
228 constexpr ~Word() noexcept = default;
230 };
231} // namespace tg::conllu
232
233#endif // TG_CONLLU_WORD_HPP
A single sentence within a Treebank.
Definition sentence.hpp:35
A single token within a Sentence, or an empty node within a Token.
Definition token.hpp:43
constexpr std::error_code validate() const noexcept
Checks the word for structural inconsistencies.
constexpr Word() noexcept=delete
Default construction is disabled; words are created via Sentence::add_word.
constexpr std::error_code set_form(const std::optional< std::string_view > &form) noexcept
Sets the word form.
constexpr std::error_code set_misc(const std::optional< std::string_view > &misc) noexcept
Sets the word-level MISC field.
constexpr TokenId last_id() const noexcept
Identifier of the last token in the word's range.
constexpr void set_typo(std::optional< bool > typo) noexcept
Sets whether the word is flagged as a typo.
constexpr std::optional< std::string_view > get_form() const noexcept
Word form (surface form of the multiword token).
constexpr void unset_misc() noexcept
Unsets the word-level MISC field.
constexpr void unset_typo() noexcept
Unsets the typo flag.
constexpr bool contains_token(Token *token) const noexcept
Checks whether a token belongs to this word.
constexpr bool contains_token_id(TokenId id) const noexcept
Checks whether a token identifier belongs to this word.
constexpr std::size_t num_tokens() const noexcept
Number of tokens covered by this word.
constexpr auto tokens() const noexcept
View over the tokens covered by this word, in sentence order.
constexpr std::error_code set_range(Range range) noexcept
Sets the token range covered by this word.
constexpr std::optional< bool > get_typo() const noexcept
Whether the word is flagged as a typo.
constexpr std::optional< std::string_view > get_misc() const noexcept
Word-level MISC field.
constexpr void unset_form() noexcept
Unsets the word form.
constexpr TokenId first_id() const noexcept
Identifier of the first token in the word's range.
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
constexpr bool is_valid() const noexcept
Checks whether the range is well-formed on its own.
constexpr bool equals(Range range) const noexcept
Checks whether this range has the same bounds as another range.
TokenId first
Identifier of the first token in the range.
Definition word.hpp:38
constexpr std::size_t count() const noexcept
Number of tokens covered by the range.
constexpr bool overlaps(Range range) const noexcept
Checks whether this range shares any token identifier with another range.
TokenId last
Identifier of the last token in the range.
Definition word.hpp:42
constexpr bool contains(TokenId id) const noexcept
Checks whether a token identifier falls within the range.