CoNLL-U
Loading...
Searching...
No Matches
parser.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_PARSER_HPP
16#define TG_CONLLU_PARSER_HPP 1
17
18#include "conllu/treebank.hpp"
19
20#include <algorithm>
21#include <filesystem>
22#include <fstream>
23#include <istream>
24#include <ranges>
25#include <string>
26#include <string_view>
27#include <unordered_set>
28#include <vector>
29
30#if __has_include(<spanstream>)
31#include <spanstream>
32#endif // __has_include(<spanstream>)
33
34namespace tg::conllu {
40 struct ParserOptions final {
45 };
46
52 class Parser final {
53 ParserOptions options_{};
54 std::string sb0_{};
55 std::string sb1_{};
56 std::vector<std::size_t> vb0_{};
57 std::unordered_set<std::string_view> vb1_{};
58
59 [[nodiscard]] std::error_code read_block_(std::istream &input, std::vector<std::string_view> &block) noexcept;
60
61 [[nodiscard]] static std::optional<std::error_code> parse_columns_(std::string_view line,
62 Treebank *treebank) noexcept;
63 [[nodiscard]] std::error_code parse_sentence_(std::vector<std::string_view> &lines,
64 Treebank *treebank) const noexcept;
65
66 [[nodiscard]] static std::size_t count_sentence_meta_lines_(
67 const std::vector<std::string_view> &lines) noexcept;
68 [[nodiscard]] static std::optional<std::string_view> find_sentence_sent_id_(
69 std::vector<std::string_view> &lines, std::size_t &limit) noexcept;
70
71 [[nodiscard]] std::expected<Token *, std::error_code> parse_token_(const Treebank *treebank, Sentence *sentence,
72 Token *last_token,
73 std::string_view line) const noexcept;
74
75 [[nodiscard]] std::error_code parse_word_(const Treebank *treebank, Sentence *sentence, const Token *last_token,
76 const std::vector<std::string_view> &field_values,
77 std::string_view id) const noexcept;
78
79 [[nodiscard]] std::optional<std::string_view> value_view_(std::string_view value) const noexcept;
80
81 public:
89 constexpr Parser() noexcept = default;
94 explicit(true) constexpr Parser(ParserOptions options) noexcept;
105 [[nodiscard]] std::expected<std::unique_ptr<Treebank>, std::error_code> parse(std::istream &&stream) noexcept;
112 [[nodiscard]] std::expected<std::unique_ptr<Treebank>, std::error_code> parse_file(
113 const std::filesystem::path &path) noexcept;
119 [[nodiscard]] std::expected<std::unique_ptr<Treebank>, std::error_code> parse_memory(
120 std::string_view data) noexcept;
128 template <typename DataType>
129 requires(sizeof(DataType) == sizeof(std::uint8_t))
130 [[nodiscard]] std::expected<std::unique_ptr<Treebank>, std::error_code> parse_memory(const DataType *data,
131 std::size_t size) noexcept;
140 constexpr ~Parser() noexcept = default;
142 };
143} // namespace tg::conllu
144
145#include "conllu/impl/parser.ipp"
146
147#endif // TG_CONLLU_PARSER_HPP
std::expected< std::unique_ptr< Treebank >, std::error_code > parse(std::istream &&stream) noexcept
Parses a CoNLL-U formatted stream into a new Treebank.
constexpr Parser() noexcept=default
Default constructor. Initializes a parser with default options.
std::expected< std::unique_ptr< Treebank >, std::error_code > parse_memory(std::string_view data) noexcept
Parses CoNLL-U formatted text already held in memory into a new Treebank.
std::expected< std::unique_ptr< Treebank >, std::error_code > parse_file(const std::filesystem::path &path) noexcept
Opens the file at path and parses its contents as CoNLL-U formatted text into a new Treebank.
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
In-memory representation of a CoNLL-U treebank.
Definition treebank.hpp:36
Configuration options controlling how a Parser interprets CoNLL-U input.
Definition parser.hpp:40
char empty_value_indicator
Character used to represent an empty field value (default: _).
Definition parser.hpp:44