LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
treebank.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 CONLLU_TREEBANK_H
7#define CONLLU_TREEBANK_H
8
9#include "file.h"
10
11#include <assert.h>
12#include <iostream>
13#include <string>
14#include <vector>
15#include <map>
16#include <codecvt>
17
18namespace deeplima
19{
20namespace CoNLLU
21{
22 class Annotation;
23
24 class Word
25 {
26
27 };
28
30 {
31 public:
34 const Word& get_word(size_t idx) const;
35 const Word& get_word(idx_t id) const;
36
37 size_t calc_num_of_words(const Annotation& annot) const;
38 size_t get_first_word_idx() const
39 {
40 return m_first_word;
41 }
42
43 protected:
48
49 friend class Annotation;
50 };
51
53 {
54 public:
55 size_t length() const;
56 size_t pos() const;
57
58 protected:
60 size_t m_line_idx;
61 };
62
64 {
65 public:
66 void load(const std::string& fn);
67 const std::wstring& get_text() const
68 {
69 return m_text;
70 }
71
72 const std::string& get_original_text() const
73 {
74 return m_original_text;
75 }
76
77 protected:
78 std::wstring m_text;
79 std::string m_original_text;
80
81 friend class Annotation;
82 };
83
85 {
86 public:
87 struct token_t
88 {
89 enum token_flags_t : uint8_t
90 {
91 none = 0x00,
94 multiword = 0x04, // surface token of a "N-M" multiword token
96 };
97
99 : m_line_idx(0), m_pos(0), m_len(0), m_flags(none) {}
100 token_t(size_t line_idx, size_t pos, uint16_t len=0)
101 : m_line_idx(line_idx), m_pos(pos), m_len(len), m_flags(none) {}
102
103 inline bool eos() const
104 {
105 return m_flags & sentence_brk;
106 }
107
108 inline bool is_multiword() const
109 {
110 return m_flags & multiword;
111 }
112
114 size_t m_pos;
115 uint16_t m_len;
117 };
118
119 struct word_t
120 {
123 word_t(size_t line_idx, size_t token_idx)
124 : m_line_idx(line_idx), m_token_idx(token_idx), m_flags(token_t::token_flags_t::none) {}
125
126 inline bool eos() const
127 {
129 }
130
134 };
135
136 Annotation() : m_pdoc(nullptr) {}
137
138 void load(const std::string& fn);
139 void load(std::istream& input);
140
141 const Sentence& get_sentence(size_t idx) const;
142 inline size_t get_num_sentences() const
143 {
144 return m_sentences.size();
145 }
146
147 inline const std::vector<token_t>& get_tokens() const
148 {
149 return m_tokens;
150 }
151
152 inline const std::vector<word_t>& get_words() const
153 {
154 return m_words;
155 }
156
157 typedef std::vector<token_t>::const_iterator tokens_const_iterator;
159 {
160 return m_tokens.begin();
161 }
162
164 {
165 return m_tokens.end();
166 }
167
168 inline const std::vector<word_t>& words() const
169 {
170 return m_words;
171 }
172
173 typedef std::vector<word_t>::const_iterator words_const_iterator;
175 {
176 return m_words.begin();
177 }
178
180 {
181 return m_words.end();
182 }
183
184 inline const CoNLLULine& get_line(size_t line_idx) const
185 {
186 assert(line_idx < m_lines.size());
187 return m_lines[line_idx];
188 }
189
190 protected:
191 void rebuild_structure(size_t num_sentences, size_t num_tokens, size_t num_words);
192 bool test_structure() const;
193
194 std::vector<CoNLLULine> m_lines;
195
197 std::vector<token_t> m_tokens;
198 std::vector<word_t> m_words;
199 std::vector<Sentence> m_sentences;
200
201 friend class Treebank;
202 };
203
205 {
206 class token_struct
207 {
209 const CoNLLULine& m_line;
210
211 public:
212 token_struct(const Annotation::tokens_const_iterator& it, const CoNLLULine& line)
213 : m_it(it), m_line(line)
214 { }
215
216 inline bool eos() const
217 {
218 return m_it->eos();
219 }
220
221 inline bool is_word() const
222 {
223 return !m_line.idx().is_multiword();
224 }
225
226 inline const std::string& form() const
227 {
228 return m_line.form();
229 }
230 };
231
232 struct iterator_struct
233 {
235 const Annotation& m_data;
236 iterator_struct(Annotation::tokens_const_iterator it, const Annotation& data)
237 : m_it(it), m_data(data) { }
238
239 iterator_struct(const iterator_struct& it)
240 : m_it(it.m_it), m_data(it.m_data) { }
241
242 inline const token_struct operator*() const
243 {
244 return token_struct(m_it, m_data.get_line(m_it->m_line_idx));
245 }
246
247 inline bool operator==(const iterator_struct& other)
248 {
249 return m_it == other.m_it;
250 }
251
252 inline bool operator!=(const iterator_struct& other)
253 {
254 return m_it != other.m_it;
255 }
256
257 inline iterator_struct operator++(int)
258 {
259 iterator_struct rv = *this;
260 m_it++;
261 return rv;
262 }
263 };
264 public:
266 typedef token_struct token_t;
267 typedef iterator_struct const_iterator;
268
270 : m_data(p) {}
271
273 {
274 assert(nullptr != m_data);
275 return iterator_struct(m_data->tokens_begin(), *m_data);
276 }
277
279 {
280 assert(nullptr != m_data);
281 return iterator_struct(m_data->tokens_end(), *m_data);
282 }
283
284 const Annotation& data() const
285 {
286 assert(nullptr != m_data);
287 return *m_data;
288 }
289
290 size_t size() const
291 {
292 assert(nullptr != m_data);
293 return m_data->get_tokens().size();
294 }
295
296 protected:
298 };
299
301 {
302 class word_struct
303 {
305 const CoNLLULine& m_line;
306
307 public:
308 word_struct(const Annotation::words_const_iterator& it, const CoNLLULine& line)
309 : m_it(it), m_line(line)
310 { }
311
312 inline bool eos() const
313 {
314 return m_it->eos();
315 }
316
317 inline bool is_word() const
318 {
319 return m_line.idx().is_real_word();
320 }
321
322 inline const std::string& form() const
323 {
324 if ("_" == m_line.form())
325 {
326 return m_line.lemma();
327 }
328 return m_line.form();
329 }
330
331 inline const std::string& upos() const
332 {
333 return m_line.upos();
334 }
335
336 inline const std::string& xpos() const
337 {
338 return m_line.xpos();
339 }
340
341 inline const std::string& deprel() const
342 {
343 return m_line.deprel();
344 }
345
346 inline const idx_t& head() const
347 {
348 return m_line.head();
349 }
350
351 inline const idx_t& idx() const
352 {
353 return m_line.idx();
354 }
355
356 inline const std::map<std::string, std::set<std::string>>& feats() const
357 {
358 return m_line.feats();
359 }
360 };
361
362 protected:
364 {
369
371 : m_it(it.m_it), m_data(it.m_data) { }
372
373 inline const word_struct operator*() const
374 {
375 return word_struct(m_it, m_data.get_line(m_it->m_line_idx));
376 }
377
378 inline bool operator==(const iterator_struct& other)
379 {
380 return m_it == other.m_it;
381 }
382
383 inline bool operator!=(const iterator_struct& other)
384 {
385 return m_it != other.m_it;
386 }
387
389 {
390 iterator_struct rv = *this;
391 m_it++;
392 return rv;
393 }
394 };
395 public:
397 typedef word_struct token_t;
399
401 : m_data(p) {}
402
403 virtual const_iterator begin() const
404 {
405 assert(nullptr != m_data);
407 }
408
409 virtual const_iterator end() const
410 {
411 assert(nullptr != m_data);
413 }
414
415 const Annotation& data() const
416 {
417 assert(nullptr != m_data);
418 return *m_data;
419 }
420
421 virtual size_t size() const
422 {
423 assert(nullptr != m_data);
424 return m_data->get_words().size();
425 }
426
427 protected:
429 };
430
432 {
433 public:
434 BoundedWordLevelAdapter(const Annotation* p, size_t first_word_idx, size_t len)
435 : WordLevelAdapter(p), m_first_word_idx(first_word_idx), m_len(len) {}
436
437 virtual const_iterator begin() const
438 {
439 assert(nullptr != m_data);
441 }
442
443 virtual const_iterator end() const
444 {
445 assert(nullptr != m_data);
447 }
448
449 virtual size_t size() const
450 {
451 assert(nullptr != m_data);
452 return m_len;
453 }
454
455 protected:
457 size_t m_len;
458 };
459
460 bool parse_ud_file_name(const std::string& fn, std::map<std::string, std::string>& fields);
461
463 {
464 struct AnnotatedDocument
465 {
466 Document doc;
467 Annotation annot;
468 };
469
470 public:
471 explicit Treebank(const std::string& path)
472 {
473 if (!load(path))
474 {
475 throw std::runtime_error(std::string("Error: treebank cannot load path "+path));
476 }
477 }
478
479 const Document& get_doc(const std::string& name) const
480 {
481 auto it = m_parts.find(name);
482 if (m_parts.end() != it)
483 {
484 return it->second.doc;
485 }
486 throw std::runtime_error(std::string("Error: treebank does not have a ")+name+" part");
487 }
488
489 const Annotation& get_annot(const std::string& name) const
490 {
491 auto it = m_parts.find(name);
492 if (m_parts.end() != it)
493 {
494 return it->second.annot;
495 }
496 throw std::runtime_error(std::string("Error: treebank does not have a ")+name+" part");
497 }
498
499 protected:
500 bool load(const std::string& path);
501
502 std::map<std::string, AnnotatedDocument> m_parts;
503 };
504
505} // namespace CoNLLU
506} // namespace deeplima
507
508#endif
std::vector< CoNLLULine > m_lines
Definition treebank.h:194
const std::vector< word_t > & words() const
Definition treebank.h:168
const std::vector< word_t > & get_words() const
Definition treebank.h:152
size_t get_num_sentences() const
Definition treebank.h:142
std::vector< token_t > m_tokens
Definition treebank.h:197
void load(const std::string &fn)
Definition treebank.cpp:91
tokens_const_iterator tokens_end() const
Definition treebank.h:163
words_const_iterator words_begin() const
Definition treebank.h:174
const std::vector< token_t > & get_tokens() const
Definition treebank.h:147
void rebuild_structure(size_t num_sentences, size_t num_tokens, size_t num_words)
Definition treebank.cpp:171
const CoNLLULine & get_line(size_t line_idx) const
Definition treebank.h:184
std::vector< Sentence > m_sentences
Definition treebank.h:199
const Sentence & get_sentence(size_t idx) const
Definition treebank.cpp:380
std::vector< word_t >::const_iterator words_const_iterator
Definition treebank.h:173
words_const_iterator words_end() const
Definition treebank.h:179
std::vector< token_t >::const_iterator tokens_const_iterator
Definition treebank.h:157
std::vector< word_t > m_words
Definition treebank.h:198
tokens_const_iterator tokens_begin() const
Definition treebank.h:158
virtual const_iterator end() const
Definition treebank.h:443
virtual const_iterator begin() const
Definition treebank.h:437
BoundedWordLevelAdapter(const Annotation *p, size_t first_word_idx, size_t len)
Definition treebank.h:434
const std::map< std::string, std::set< std::string > > & feats() const
Definition line.h:178
const idx_t & head() const
Definition line.h:188
const std::string & deprel() const
Definition line.h:173
const std::string & form() const
Definition line.h:153
const std::string & lemma() const
Definition line.h:158
const idx_t & idx() const
Definition line.h:148
const std::string & upos() const
Definition line.h:163
const std::string & xpos() const
Definition line.h:168
std::string m_original_text
Definition treebank.h:79
void load(const std::string &fn)
Definition treebank.cpp:69
const std::wstring & get_text() const
Definition treebank.h:67
const std::string & get_original_text() const
Definition treebank.h:72
size_t calc_num_of_words(const Annotation &annot) const
Definition treebank.cpp:25
size_t get_first_word_idx() const
Definition treebank.h:38
const Word & get_word(size_t idx) const
const Word & get_word(idx_t id) const
TokenLevelAdapter(const Annotation *p)
Definition treebank.h:269
const_iterator end() const
Definition treebank.h:278
const Annotation & data() const
Definition treebank.h:284
const_iterator begin() const
Definition treebank.h:272
const Document & get_doc(const std::string &name) const
Definition treebank.h:479
Treebank(const std::string &path)
Definition treebank.h:471
std::map< std::string, AnnotatedDocument > m_parts
Definition treebank.h:502
const Annotation & get_annot(const std::string &name) const
Definition treebank.h:489
bool load(const std::string &path)
Definition treebank.cpp:43
virtual size_t size() const
Definition treebank.h:421
WordLevelAdapter(const Annotation *p)
Definition treebank.h:400
const Annotation & data() const
Definition treebank.h:415
virtual const_iterator begin() const
Definition treebank.h:403
virtual const_iterator end() const
Definition treebank.h:409
bool parse_ud_file_name(const std::string &fn, map< string, string > &fields)
Definition treebank.cpp:362
token_t(size_t line_idx, size_t pos, uint16_t len=0)
Definition treebank.h:100
token_t::token_flags_t m_flags
Definition treebank.h:133
word_t(size_t line_idx, size_t token_idx)
Definition treebank.h:123
bool operator!=(const iterator_struct &other)
Definition treebank.h:383
Annotation::words_const_iterator m_it
Definition treebank.h:365
bool operator==(const iterator_struct &other)
Definition treebank.h:378
iterator_struct(Annotation::words_const_iterator it, const Annotation &data)
Definition treebank.h:367
bool is_multiword() const
Definition line.h:48
bool is_real_word() const
Definition line.h:58