CoNLL-U
Loading...
Searching...
No Matches
token.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_TOKEN_HPP
16#define TG_CONLLU_TOKEN_HPP 1
17
18#include "conllu/error.hpp"
19
20#include <charconv>
21#include <expected>
22#include <format>
23#include <optional>
24#include <string>
25#include <unordered_map>
26#include <vector>
27
28namespace tg::conllu {
29 class Sentence;
30 class Word;
31
36 using TokenId = std::uint64_t;
37
43 class Token final {
44 TokenId id_{};
45 std::optional<TokenId> head_{std::nullopt};
46
47 std::vector<std::optional<std::string>> fields_{};
48
49 std::vector<std::unique_ptr<Token>> empty_nodes_{};
50
51 Token *parent_{nullptr};
52 Sentence *sentence_{nullptr};
53
54 [[nodiscard]] constexpr std::optional<std::error_code> set_known_field_(
55 std::size_t position, std::string_view name, const std::optional<std::string_view> &value) noexcept;
56 [[nodiscard]] constexpr std::error_code set_field_value_(std::size_t position,
57 const std::optional<std::string_view> &value) noexcept;
58 [[nodiscard]] constexpr std::error_code set_field_value_(std::size_t position, std::string_view name,
59 const std::optional<std::string_view> &value) noexcept;
60
61 [[nodiscard]] constexpr std::error_code set_head_(const std::optional<std::string_view> &value) noexcept;
62
63 explicit(true) constexpr Token(Sentence *sentence, Token *parent) noexcept;
64
65 friend class Parser;
66 friend class Sentence;
67 friend class Treebank;
68 friend class Word;
69 friend class Writer;
70
71 public:
80 constexpr Token() noexcept = delete;
89 [[nodiscard]] constexpr Sentence *sentence() const noexcept;
93 [[nodiscard]] constexpr Token *get_parent() const noexcept;
97 [[nodiscard]] constexpr TokenId get_id() const noexcept;
102 [[nodiscard]] constexpr std::optional<Token *> get_head() const noexcept;
109 constexpr std::error_code set_head(TokenId id) noexcept;
113 [[nodiscard]] constexpr bool is_empty_node() const noexcept;
119 [[nodiscard]] constexpr std::optional<Word *> get_word() const noexcept;
123 [[nodiscard]] constexpr bool is_in_word() const noexcept;
132 [[nodiscard]] constexpr auto fields() const noexcept;
136 [[nodiscard]] constexpr std::size_t num_fields() const noexcept;
142 [[nodiscard]] constexpr std::optional<std::string_view> get_field_value(std::size_t position) const noexcept;
148 [[nodiscard]] constexpr std::optional<std::string_view> get_field_value(std::string_view name) const noexcept;
155 [[nodiscard]] constexpr std::error_code set_field_value(std::size_t position, std::string_view value) noexcept;
162 [[nodiscard]] constexpr std::error_code set_field_value(std::string_view name, std::string_view value) noexcept;
168 [[nodiscard]] constexpr std::error_code unset_field_value(std::size_t position) noexcept;
174 [[nodiscard]] constexpr std::error_code unset_field_value(std::string_view name) noexcept;
183 [[nodiscard]] constexpr auto empty_nodes() const noexcept;
187 [[nodiscard]] constexpr std::size_t empty_node_count() const noexcept;
192 [[nodiscard]] constexpr std::expected<Token *, std::error_code> add_empty_node() noexcept;
198 constexpr std::error_code remove_empty_node(TokenId id) noexcept;
204 constexpr std::error_code remove_empty_node(Token *node) noexcept;
208 constexpr void remove_all_empty_nodes() noexcept;
218 [[nodiscard]] constexpr std::error_code validate() const noexcept;
227 constexpr ~Token() noexcept = default;
229 };
230} // namespace tg::conllu
231
232#endif // TG_CONLLU_TOKEN_HPP
A single sentence within a Treebank.
Definition sentence.hpp:35
constexpr std::error_code validate() const noexcept
Checks the token for structural inconsistencies.
constexpr bool is_empty_node() const noexcept
Whether this token is an empty node.
constexpr auto fields() const noexcept
View over the field values, in column order.
constexpr void remove_all_empty_nodes() noexcept
Removes all empty nodes attached to this token.
constexpr TokenId get_id() const noexcept
Token identifier. For an empty node, this is its position among its parent's empty nodes.
constexpr std::optional< Word * > get_word() const noexcept
The Word this token belongs to, if any.
constexpr std::error_code set_head(TokenId id) noexcept
Sets the HEAD field, linking this token to its syntactic head.
constexpr std::optional< Token * > get_head() const noexcept
Resolves the HEAD field to the referenced token.
constexpr std::size_t empty_node_count() const noexcept
Number of empty nodes attached to this token.
constexpr std::error_code remove_empty_node(TokenId id) noexcept
Removes the empty node with the given identifier.
constexpr std::expected< Token *, std::error_code > add_empty_node() noexcept
Constructs and appends a new empty node.
constexpr std::size_t num_fields() const noexcept
Number of fields on this token (normally equal to Treebank::num_columns).
constexpr Sentence * sentence() const noexcept
Pointer to the sentence that owns this token.
constexpr std::error_code set_field_value(std::size_t position, std::string_view value) noexcept
Sets a field value by column position.
constexpr auto empty_nodes() const noexcept
View over the empty nodes attached to this token, in order.
constexpr std::optional< std::string_view > get_field_value(std::size_t position) const noexcept
Looks up a field value by column position.
constexpr Token * get_parent() const noexcept
Pointer to the parent token, or nullptr if this is not an empty node.
constexpr bool is_in_word() const noexcept
Whether this token belongs to a Word.
constexpr Token() noexcept=delete
Default construction is disabled; tokens are created via Sentence::add_token or Token::add_empty_node...
constexpr std::error_code unset_field_value(std::size_t position) noexcept
Unsets a field value by column position.
A multiword token: a contiguous span of two or more Tokens sharing a single surface form.
Definition word.hpp:28
std::uint64_t TokenId
Identifier for a token or empty node within a sentence.
Definition token.hpp:36