15#ifndef TG_CONLLU_TOKEN_HPP
16#define TG_CONLLU_TOKEN_HPP 1
18#include "conllu/error.hpp"
25#include <unordered_map>
45 std::optional<TokenId> head_{std::nullopt};
47 std::vector<std::optional<std::string>> fields_{};
49 std::vector<std::unique_ptr<Token>> empty_nodes_{};
51 Token *parent_{
nullptr};
52 Sentence *sentence_{
nullptr};
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;
61 [[nodiscard]]
constexpr std::error_code set_head_(
const std::optional<std::string_view> &value)
noexcept;
63 explicit(
true)
constexpr Token(Sentence *
sentence, Token *parent)
noexcept;
66 friend class Sentence;
67 friend class Treebank;
80 constexpr Token() noexcept = delete;
89 [[nodiscard]] constexpr Sentence *
sentence() const noexcept;
93 [[nodiscard]] constexpr Token *
get_parent() const noexcept;
102 [[nodiscard]] constexpr std::optional<Token *>
get_head() const noexcept;
119 [[nodiscard]] constexpr std::optional<Word *>
get_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;
192 [[nodiscard]] constexpr std::expected<Token *, std::error_code>
add_empty_node() noexcept;
218 [[nodiscard]] constexpr std::error_code
validate() const noexcept;
227 constexpr ~Token() noexcept = default;
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