LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
reader_conllu.h
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef DEEPLIMA_READER_CONLLU_H
7#define DEEPLIMA_READER_CONLLU_H
8
11namespace deeplima
12{
13namespace segmentation
14{
15
17{
18public:
19
21 {
22 m_callback = fn;
23 }
24
26
27protected:
28
30};
31
33{
34public:
35
37 {
39 m_len = 0;
40 }
41
42 virtual ~CoNLLUReader() { }
43
44 virtual void parse_from_stream(const read_callback_t fn)
45 {
46 const int32_t buffer_size = 1024*64;
47 char buff[buffer_size];
48 char* end = buff + buffer_size;
49 bool continue_reading = true;
50 int32_t start_writting = 0;
51 uint32_t reserved_for_lookahead = 1024*2;
52 // longest last token line in UD 2.8 collection.
53
54 std::vector<token_pos> tokens(1024);
55 size_t token_idx = 0;
56
57 do
58 {
59 int32_t bytes_read = 0;
60 continue_reading = fn((uint8_t*)buff + start_writting, bytes_read, buffer_size - start_writting);
61 if (0 == bytes_read)
62 {
63 break;
64 }
65
66 const char* p = buff;
67 char* p_eol = nullptr;
68
69 if (bytes_read < buffer_size - start_writting)
70 {
71 end = buff + start_writting + bytes_read;
72 }
73
74 if (!continue_reading || p >= end - reserved_for_lookahead)
75 {
76 reserved_for_lookahead = 0;
77 }
78
79 while (p < end - reserved_for_lookahead)
80 {
81 p_eol = (char*)memchr(p, '\n', end - p);
82 if (nullptr == p_eol)
83 {
84 break;
85 }
86
87 if (*p == '\n')
88 {
89 p++;
90 if (token_idx > 0)
91 {
92 token_pos& token = tokens[token_idx - 1];
94 }
95 continue;
96 }
97
98 if (*p == '#')
99 {
100 p = p_eol + 1;
101 continue;
102 }
103
104 if (read_token(tokens[token_idx], p, p_eol))
105 {
106 token_idx++;
107
108 if (token_idx >= tokens.size())
109 {
110 // Lookahead for the possible sentence break
111 if (end - p_eol > 1) {
112 const char* p_after_eol = p_eol + 1;
113 if (*p_after_eol == '\n') {
114 token_pos& token = tokens[tokens.size() - 1];
116 }
117 }
118
119 m_callback(tokens, token_idx);
120 token_idx = 0;
122 }
123 }
124
125 p = p_eol + 1;
126 }
127
128 if (token_idx > 0)
129 {
130 // Lookahead for the possible sentence break
131 if (p < end && *p == '\n')
132 {
133 p++;
134
135 token_pos& token = tokens[token_idx - 1];
137 }
138 m_callback(tokens, token_idx);
139 token_idx = 0;
140 }
141
142 start_writting = 0;
143 if (p < end)
144 {
145 start_writting = end - p;
146 memcpy(buff, p, start_writting);
147 }
148
149 } while (continue_reading);
150 }
151
153 virtual void finalize() override {}
154
155protected:
156 inline bool read_token(token_pos& token, const char* p, const char* end, bool eos=false)
157 {
158 while (*p != '\t' && *p != '-' && *p != '.') p++;
159 if (*p == '.') return false;
160
161 token.clear();
162 if (*p == '-')
163 {
164 const char *start = (const char*)memchr(p, '\t', end - p);
165 assert(nullptr != start);
166 start += 1;
167 assert('\n' != *start);
168 m_len = (const char*)memchr(start, '\t', end - start) - start;
169 assert(m_len > 0);
170 memcpy(m_next, start, m_len);
171 m_next[m_len] = 0;
172 return false;
173 }
174
175 token.m_pch = (const char*)memchr(p, '\t', end - p) + 1;
176 assert('\n' != *(token.m_pch));
177
178 /*if ('_' == *token.m_pch)
179 {
180 if (m_len > 0)
181 {
182 token.m_pch = m_next;
183 token.m_len = m_len;
184 //m_next[m_len + 1] = 0;
185 m_next += m_len + 1;
186 m_len = 0;
187 }
188 else
189 {
190 return false;
191 }
192 }
193 else
194 {*/
195 token.m_len = (const char*)memchr(token.m_pch, '\t', end - token.m_pch) - token.m_pch;
196 /*}*/
197
198 if (eos)
199 {
201 }
202
203 return true;
204 }
205
206protected:
207 char m_buffer[1024 * 4];
208 char* m_next;
209 size_t m_len;
210};
211
212} // namespace segmentation
213} // namespace deeplima
214
215#endif
virtual void finalize() override
Cleanup all remaining locks if any.
bool read_token(token_pos &token, const char *p, const char *end, bool eos=false)
virtual void parse_from_stream(const read_callback_t fn)
virtual void register_handler(const segmentation_callback_t fn)
std::function< bool(uint8_t *buffer, int32_t &read, int32_t max) > read_callback_t
std::function< void(const std::vector< token_pos > &tokens, uint32_t len) > segmentation_callback_t
@ sentence_brk
Definition token_type.h:21