15#ifndef TG_CONLLU_TREEBANK_HPP
16#define TG_CONLLU_TREEBANK_HPP 1
18#include "conllu/error.hpp"
19#include "conllu/internal/string_hash.hpp"
20#include "conllu/sentence.hpp"
26#include <unordered_map>
27#include <unordered_set>
37 std::deque<std::string> columns_{};
38 std::vector<std::string_view> column_views_{};
39 std::unordered_map<std::string_view, std::size_t, internal::StringHash, std::equal_to<>> column_idx_{};
41 std::vector<std::unique_ptr<Sentence>> sentences_{};
42 std::unordered_map<std::string_view, std::size_t, internal::StringHash, std::equal_to<>> sentence_idx_{};
45 friend class Sentence;
75 [[nodiscard]] constexpr const std::vector<std::string_view>&
columns() const noexcept;
79 [[nodiscard]] constexpr std::
size_t num_columns() const noexcept;
85 [[nodiscard]] constexpr std::optional<std::
size_t>
get_column_pos(std::string_view name) const noexcept;
91 [[nodiscard]] constexpr std::optional<std::string_view>
get_column_name(std::
size_t position) const noexcept;
97 constexpr std::expected<std::
size_t, std::error_code>
add_column(std::string_view name) noexcept;
107 [[nodiscard]] constexpr auto
sentences() const noexcept;
118 [[nodiscard]] constexpr std::expected<Sentence*, std::error_code>
add_sentence(
119 std::string_view sent_id, std::string_view text = std::string_view()) noexcept;
125 [[nodiscard]] constexpr std::optional<Sentence*>
get_sentence_by_id(std::string_view sent_id) const noexcept;
147 [[nodiscard]] constexpr
bool empty() const noexcept;
156 [[nodiscard]] constexpr std::error_code
validate() const noexcept;
183#include "conllu/impl/sentence.ipp"
184#include "conllu/impl/token.ipp"
185#include "conllu/impl/treebank.ipp"
186#include "conllu/impl/word.ipp"
constexpr std::optional< std::size_t > get_column_pos(std::string_view name) const noexcept
Looks up a column's position by name.
constexpr std::size_t num_columns() const noexcept
Number of columns in the treebank.
constexpr Treebank() noexcept=default
Default constructor. Initializes an empty treebank.
constexpr std::error_code remove_sentence(std::string_view sent_id) noexcept
Removes the sentence with the given identifier.
constexpr const std::vector< std::string_view > & columns() const noexcept
Column names, in declaration order.
constexpr std::expected< Sentence *, std::error_code > add_sentence(std::string_view sent_id, std::string_view text=std::string_view()) noexcept
Constructs and appends a new sentence.
constexpr auto sentences() const noexcept
View over the sentences, in treebank order.
constexpr bool empty() const noexcept
Whether the treebank contains no sentences.
constexpr std::expected< std::size_t, std::error_code > add_column(std::string_view name) noexcept
Appends a new column, extending every existing token with an empty field for it.
constexpr void clear() noexcept
Removes all columns and sentences, resetting the treebank to its default-constructed state.
constexpr std::optional< std::string_view > get_column_name(std::size_t position) const noexcept
Looks up a column's name by position.
constexpr std::error_code validate() const noexcept
Checks the treebank for structural inconsistencies.
constexpr std::optional< Sentence * > get_sentence_by_id(std::string_view sent_id) const noexcept
Looks up a sentence by its identifier.
constexpr std::size_t num_sentences() const noexcept
Number of sentences in the treebank.