LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
segmentation_decoder.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_APPS_SEGMENTATION_DECODER_H
7#define DEEPLIMA_APPS_SEGMENTATION_DECODER_H
8
9#include <iostream>
10#include <functional>
11#include <stdexcept>
12#include <string>
13#include <vector>
14
15#include <unicode/uchar.h>
16
18#include "deeplima/token_type.h"
19
20namespace deeplima
21{
22namespace segmentation
23{
24
26{
27 uint16_t m_offset; // offset from previous token end
28 uint16_t m_len; // length of this token in bytes
29 const char* m_pch;
31
32 // Multiword-token (MWT) expansion metadata, set by MwtExpander. On the first
33 // sub-word of an expanded surface token (e.g. "de" of "du"->de+le) m_mwt_len
34 // holds the number of sub-words and m_mwt_surface_* the surface form bytes;
35 // both are zero/null on normal tokens and on the non-first sub-words.
36 uint8_t m_mwt_len;
37 const char* m_mwt_surface_pch;
39
43
44 inline void clear()
45 {
46 m_offset = m_len = 0;
47 m_pch = nullptr;
49 m_mwt_len = 0;
50 m_mwt_surface_pch = nullptr;
52 }
53
54 inline bool empty() const
55 {
56 return (0 == m_offset) && (0 == m_len) && (nullptr == m_pch);
57 }
58
59 inline bool too_long() const
60 {
61 return m_len & (1 << (sizeof(uint16_t) * 8 - 1));
62 }
63};
64
65typedef std::function < void (const std::vector<token_pos>& tokens,
66 uint32_t len) > segmentation_callback_t;
67
68enum segm_tag_t : uint8_t
69{
70 // tokenization-only tags
71 X = 0x00,
72 B = 0x01,
73 I = 0x02,
74 E = 0x03,
75 S = 0x04,
77
78 // + sentence segmentation tags
79 E_EOS = 0x05,
80 S_EOS = 0x06,
82
83 // + multiword-token tags: marks a surface token to be expanded into sub-words
84 // (e.g. French "du"->de+le). Appended after the base tags so models trained
85 // without MWT (<= max_segm_tag classes) keep identical tag values. Each is the
86 // base end-tag value + max_tok_tag (E_MWT=E+max_tok_tag, etc.).
87 E_MWT = 0x07,
88 S_MWT = 0x08,
89 E_EOS_MWT = 0x09,
90 S_EOS_MWT = 0x0A,
92};
93
94namespace impl
95{
96
97template <uint8_t M = 4>
99{
100public:
101
103 : m_buff_len(0)
104 {
105 memset(m_buff, 0, M);
106 }
107
108 inline bool read(const char** pch, const char* end, uint8_t len)
109 {
110 assert(*pch < end);
111 assert(len <= M);
112 assert(m_buff_len <= M);
113
114 size_t available_bytes = end - *pch;
115
116 if (available_bytes < len)
117 {
118 assert(m_buff_len + available_bytes <= M);
119 memcpy(m_buff + m_buff_len, *pch, available_bytes);
120 m_buff_len += available_bytes;
121 *pch += available_bytes;
122 assert(m_buff_len <= M);
123 return false;
124 }
125
126 return true;
127 }
128
129 inline bool not_empty() const
130 {
131 return m_buff_len > 0;
132 }
133
134 inline bool start_reading(const char** pch,
135 const char* end,
136 uint8_t len, // length of the current character in bytes
137 std::vector<uint8_t>& dst, // destination buffer
138 token_pos& tmp,
139 bool inside)
140 {
141 assert(*pch < end);
142 assert(len <= M);
143 assert(m_buff_len <= M);
144
145 if (m_buff_len + (end - *pch) < len)
146 {
147 // full character isn't still available
148 return read(pch, end, len);
149 }
150
151 // TODO: Don't trust ML model.
152 // This code skips non-space characters
153 // in case of ML errors (wrong X tag).
154 if (inside)
155 {
156 if (dst.size() < size_t(tmp.m_len + len + 1))
157 {
158 dst.resize(tmp.m_len + len + 1);
159 }
160
161 memcpy(dst.data() + tmp.m_len, m_buff, m_buff_len);
162 memcpy(dst.data() + tmp.m_len + m_buff_len, *pch, len - m_buff_len);
163
164 tmp.m_len += len;
165 tmp.m_pch = (const char*)dst.data();
166 }
167 else
168 {
169 tmp.m_offset += len;
170 }
171
172 m_buff_len = 0;
173
174 return true;
175 }
176
177protected:
178 char m_buff[M];
179 uint8_t m_buff_len;
180};
181
183{
184public:
185
186 SegmentationDecoder(std::shared_ptr< StdMatrix<uint8_t> > out, const std::vector<uint8_t>& len)
187 : m_out(out),
188 m_len(len)
189 {
190 init();
191 }
192
193 void init()
194 {
195 m_tokens.resize(1024);
196 }
197
199 {
200 m_callback = fn;
201 }
202
203 // Forgets the token left open by the previous text, before a new one
204 void reset()
205 {
206 if (!m_tokens.empty())
207 {
208 m_tokens[0].clear();
209 }
210 }
211
212 // Emits the token decode() left open at the end of the text: when the
213 // model tags the last characters B or I, decode() keeps the token for the
214 // next buffer, and at the end of the input none comes. The token was
215 // silently lost (2026 campaign: "i." at the end of UD_Italian-MERGE dev,
216 // which made the scorer reject the whole analysis). It ends the text, hence
217 // its last sentence. Call once all the buffers are decoded.
218 void flush()
219 {
220 if (m_tokens[0].m_len > 0)
221 {
224 }
225 m_tokens[0].clear();
226 }
227
228 inline void save_current_token(size_t& pos, size_t& temp_token_len, const char* start)
229 {
230 if (m_tokens[pos].m_len > 0)
231 {
232 if (m_tokens[pos].m_pch == (const char*)m_temp_text.data())
233 {
234 assert(0 == pos);
235 if (m_temp_text.size() < size_t(m_tokens[pos].m_len + 1))
236 {
237 m_temp_text.resize(m_tokens[pos].m_len + 1);
238 }
239 memcpy(m_temp_text.data() + temp_token_len, start, m_tokens[pos].m_len - temp_token_len);
240 m_tokens[pos].m_pch = (const char*)(m_temp_text.data());
241 }
242 pos++;
243 if (pos >= m_tokens.size())
244 {
245 m_tokens.resize(m_tokens.size() + 1024);
246 }
247 m_tokens[pos].clear();
248 }
249 }
250
251 inline void consome_character(size_t& pos, uint64_t from, const char* pch)
252 {
253 if (nullptr == m_tokens[pos].m_pch)
254 {
255 assert(0 == m_tokens[pos].m_len);
256 m_tokens[pos].m_pch = pch;
257 }
258 m_tokens[pos].m_len += m_len[from];
259 }
260
261 inline uint64_t decode(const char** pch, uint32_t max, uint64_t from, uint64_t to)
262 {
263 assert(nullptr != pch);
264 assert(nullptr != *pch);
265 const char* start = *pch;
266 const char* end = *pch + max;
267
268 size_t pos = 0;
269 size_t temp_token_len = m_tokens[0].m_len;
270
271 if (not_empty())
272 {
273 uint8_t bytes_stored = m_buff_len;
274 if (start_reading(pch, end, m_len[from], m_temp_text, m_tokens[0], m_out->get(from, 0)))
275 {
276 *pch += m_len[from] - bytes_stored;
277 start += m_len[from] - bytes_stored;
278 if (m_out->get(from, 0) != segm_tag_t::X)
279 {
280 temp_token_len += m_len[from];
281 }
282 from++;
283 }
284 else
285 {
286 // can't read full character (we need more data)
287 return from;
288 }
289 }
290
291 while (from < to && *pch < end)
292 {
293 if (! read(pch, end, m_len[from]))
294 {
295 break;
296 }
297
298//#ifndef NDEBUG
299// std::cerr << "[" << from << "]==" << (int)(m_out->get(from, 0)) << std::endl;
300//#endif
301 int8_t gen_cat = 0;
302 UChar uch;
303 int32_t zero = 0;
304 // TODO: move U8_NEXT from here (we don't handle UTF-8 while decoding)
305 U8_NEXT(*pch, zero, m_len[from], uch);
306 gen_cat = u_charType(uch);
307
308 auto tag = m_out->get(from, 0);
309
310 switch (tag)
311 {
312 case segm_tag_t::X:
313 save_current_token(pos, temp_token_len, start);
314
315 if (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_LINE_SEPARATOR
316 || gen_cat == U_PARAGRAPH_SEPARATOR || gen_cat == U_CONTROL_CHAR
317 || gen_cat == U_FORMAT_CHAR)
318 {
319 m_tokens[pos].m_offset += m_len[from];
320 }
321 else
322 {
323 m_tokens[pos].m_pch = *pch;
324 m_tokens[pos].m_len += m_len[from];
325 save_current_token(pos, temp_token_len, start);
326 }
327 break;
328
329 case segm_tag_t::B:
330 save_current_token(pos, temp_token_len, start);
331
332 if (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_PARAGRAPH_SEPARATOR
333 || gen_cat == U_LINE_SEPARATOR || gen_cat == U_CONTROL_CHAR
334 || gen_cat == U_FORMAT_CHAR)
335 {
336 m_tokens[pos].m_offset += m_len[from];
337 break;
338 }
339 else
340 {
341 assert(0 == m_tokens[pos].m_len);
342 m_tokens[pos].m_pch = *pch;
343 }
344 [[fallthrough]];
345
346 case segm_tag_t::I:
347 if (0 == m_tokens[pos].m_len
348 && (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_PARAGRAPH_SEPARATOR
349 || gen_cat == U_LINE_SEPARATOR || gen_cat == U_CONTROL_CHAR
350 || gen_cat == U_FORMAT_CHAR))
351 {
352 m_tokens[pos].m_offset += m_len[from];
353 }
354 else
355 {
356 consome_character(pos, from, *pch);
357 }
358 break;
359
360 // TODO insert the marker for case continuing [[case_]]
362 m_tokens[pos].m_flags = token_flags_t(m_tokens[pos].m_flags | token_flags_t::sentence_brk);
363 [[fallthrough]];
364
365 case segm_tag_t::E:
366 if (0 == m_tokens[pos].m_len
367 && (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_PARAGRAPH_SEPARATOR
368 || gen_cat == U_LINE_SEPARATOR || gen_cat == U_CONTROL_CHAR
369 || gen_cat == U_FORMAT_CHAR))
370 {
371 m_tokens[pos].m_offset += m_len[from];
372 }
373 else
374 {
375 consome_character(pos, from, *pch);
376 save_current_token(pos, temp_token_len, start);
377 }
378 break;
379
381 save_current_token(pos, temp_token_len, start);
382
383 if (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_PARAGRAPH_SEPARATOR
384 || gen_cat == U_LINE_SEPARATOR || gen_cat == U_CONTROL_CHAR
385 || gen_cat == U_FORMAT_CHAR)
386 {
387 m_tokens[pos].m_offset += m_len[from];
388 }
389 else
390 {
391 assert(0 == m_tokens[pos].m_len);
392 m_tokens[pos].m_pch = *pch;
393 m_tokens[pos].m_len += m_len[from];
394 m_tokens[pos].m_flags = token_flags_t(m_tokens[pos].m_flags | token_flags_t::sentence_brk);
395 save_current_token(pos, temp_token_len, start);
396 }
397 break;
398
399 case segm_tag_t::S:
400 save_current_token(pos, temp_token_len, start);
401
402 if (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_PARAGRAPH_SEPARATOR
403 || gen_cat == U_LINE_SEPARATOR || gen_cat == U_CONTROL_CHAR
404 || gen_cat == U_FORMAT_CHAR)
405 {
406 m_tokens[pos].m_offset += m_len[from];
407 }
408 else
409 {
410 assert(0 == m_tokens[pos].m_len);
411 m_tokens[pos].m_pch = *pch;
412 m_tokens[pos].m_len += m_len[from];
413 save_current_token(pos, temp_token_len, start);
414 }
415 break;
416
417 // Multiword-token end tags: behave exactly like their base tag
418 // (E / E_EOS / S / S_EOS) but additionally mark the finished surface
419 // token with token_flags_t::multiword so the MwtExpander expands it.
421 m_tokens[pos].m_flags = token_flags_t(m_tokens[pos].m_flags | token_flags_t::sentence_brk);
422 [[fallthrough]];
423
425 if (0 == m_tokens[pos].m_len
426 && (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_PARAGRAPH_SEPARATOR
427 || gen_cat == U_LINE_SEPARATOR || gen_cat == U_CONTROL_CHAR
428 || gen_cat == U_FORMAT_CHAR))
429 {
430 m_tokens[pos].m_offset += m_len[from];
431 }
432 else
433 {
434 m_tokens[pos].m_flags = token_flags_t(m_tokens[pos].m_flags | token_flags_t::multiword);
435 consome_character(pos, from, *pch);
436 save_current_token(pos, temp_token_len, start);
437 }
438 break;
439
441 save_current_token(pos, temp_token_len, start);
442
443 if (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_PARAGRAPH_SEPARATOR
444 || gen_cat == U_LINE_SEPARATOR || gen_cat == U_CONTROL_CHAR
445 || gen_cat == U_FORMAT_CHAR)
446 {
447 m_tokens[pos].m_offset += m_len[from];
448 }
449 else
450 {
451 assert(0 == m_tokens[pos].m_len);
452 m_tokens[pos].m_pch = *pch;
453 m_tokens[pos].m_len += m_len[from];
454 m_tokens[pos].m_flags = token_flags_t(m_tokens[pos].m_flags
457 save_current_token(pos, temp_token_len, start);
458 }
459 break;
460
462 save_current_token(pos, temp_token_len, start);
463
464 if (gen_cat == U_SPACE_SEPARATOR || gen_cat == U_PARAGRAPH_SEPARATOR
465 || gen_cat == U_LINE_SEPARATOR || gen_cat == U_CONTROL_CHAR
466 || gen_cat == U_FORMAT_CHAR)
467 {
468 m_tokens[pos].m_offset += m_len[from];
469 }
470 else
471 {
472 assert(0 == m_tokens[pos].m_len);
473 m_tokens[pos].m_pch = *pch;
474 m_tokens[pos].m_len += m_len[from];
475 m_tokens[pos].m_flags = token_flags_t(m_tokens[pos].m_flags | token_flags_t::multiword);
476 save_current_token(pos, temp_token_len, start);
477 }
478 break;
479
480 default:
481 throw std::runtime_error("Unknown code in output.");
482 }
483
484 if (m_tokens[pos].too_long())
485 {
486 // This is a workaround to handle garbage in the input data
487 // very long (and meaningless) tokens are artificially splitted
488 // into several parts.
489 // TODO: the same type of handling is required for very long
490 // sequence of spaces: an empty token must be generated
491 // to avoid overflow of token_pos::m_offset field.
492 save_current_token(pos, temp_token_len, start);
493 }
494
495 *pch += m_len[from]; // next byte
496 from++; // next character
497 }
498
499 if (pos > 0)
500 {
501 m_callback(m_tokens, pos);
502 }
503 else
504 {
505 if (m_tokens[0].m_pch == (const char*)m_temp_text.data())
506 {
507 if (m_temp_text.size() < size_t(m_tokens[0].m_len + 1))
508 {
509 m_temp_text.resize(m_tokens[0].m_len + 1);
510 }
511 assert(0 == pos);
512 assert(m_tokens[pos].m_len >= temp_token_len);
513 memcpy(m_temp_text.data() + temp_token_len, start, m_tokens[pos].m_len - temp_token_len);
514 m_tokens[pos].m_pch = (const char*)(m_temp_text.data());
515 }
516 // std::cerr << std::endl;
517 }
518
519 if (!m_tokens[pos].empty())
520 {
521 if (pos > 0)
522 {
523 m_tokens[0] = m_tokens[pos];
524 }
525 if (m_temp_text.size() < size_t(m_tokens[0].m_len + 1))
526 {
527 m_temp_text.resize(m_tokens[0].m_len + 1);
528 }
529 if ((const char*)m_temp_text.data() != m_tokens[0].m_pch)
530 {
531 if (m_tokens[0].m_pch != nullptr)
532 {
533 memcpy(m_temp_text.data(), m_tokens[0].m_pch, m_tokens[0].m_len);
534 }
535 m_tokens[0].m_pch = (const char*)(m_temp_text.data());
536 }
537 }
538 else
539 {
540 m_tokens[0].clear();
541 }
542
543 return from;
544 }
545
546protected:
547 // input
548 std::shared_ptr< StdMatrix<uint8_t> > m_out;
549 const std::vector<uint8_t>& m_len;
550
551 // output
552 std::vector<token_pos> m_tokens;
553
554 // callback
556
557 // temp buffers
558 std::vector<uint8_t> m_temp_text;
559};
560
561} // namespace impl
562} // namespace segmentation
563} // namespace deeplima
564
565#endif
bool start_reading(const char **pch, const char *end, uint8_t len, std::vector< uint8_t > &dst, token_pos &tmp, bool inside)
bool read(const char **pch, const char *end, uint8_t len)
void consome_character(size_t &pos, uint64_t from, const char *pch)
SegmentationDecoder(std::shared_ptr< StdMatrix< uint8_t > > out, const std::vector< uint8_t > &len)
uint64_t decode(const char **pch, uint32_t max, uint64_t from, uint64_t to)
void save_current_token(size_t &pos, size_t &temp_token_len, const char *start)
std::shared_ptr< StdMatrix< uint8_t > > m_out
void register_handler(const segmentation_callback_t fn)
std::function< void(const std::vector< token_pos > &tokens, uint32_t len) > segmentation_callback_t
Edit rules for lemmatizing words absent from the lemma dictionary.
@ sentence_brk
Definition token_type.h:21