CoNLL-U
Loading...
Searching...
No Matches
treebank.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_TREEBANK_HPP
16#define TG_CONLLU_TREEBANK_HPP 1
17
18#include "conllu/error.hpp"
19#include "conllu/internal/string_hash.hpp"
20#include "conllu/sentence.hpp"
21
22#include <deque>
23#include <expected>
24#include <ranges>
25#include <string>
26#include <unordered_map>
27#include <unordered_set>
28#include <vector>
29
30namespace tg::conllu {
36 class Treebank final {
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_{};
40
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_{};
43
44 friend class Parser;
45 friend class Sentence;
46 friend class Token;
47 friend class Word;
48 friend class Writer;
49
50 public:
58 constexpr Treebank() noexcept = default;
62 constexpr Treebank(const Treebank&) noexcept = delete;
66 constexpr Treebank(Treebank&& other) noexcept = delete;
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;
111 [[nodiscard]] constexpr std::size_t num_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;
131 constexpr std::error_code remove_sentence(std::string_view sent_id) noexcept;
138 constexpr std::error_code remove_sentence(const Sentence* sentence) noexcept;
147 [[nodiscard]] constexpr bool empty() const noexcept;
151 constexpr void clear() noexcept;
156 [[nodiscard]] constexpr std::error_code validate() const noexcept;
165 constexpr Treebank& operator=(Treebank&& other) noexcept = delete;
169 constexpr Treebank& operator=(const Treebank&) noexcept = delete;
178 constexpr ~Treebank() noexcept = default;
180 };
181} // namespace tg::conllu
182
183#include "conllu/impl/sentence.ipp"
184#include "conllu/impl/token.ipp"
185#include "conllu/impl/treebank.ipp"
186#include "conllu/impl/word.ipp"
187
188#endif // TG_CONLLU_TREEBANK_HPP
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.