LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
utf8_reader.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_UTF8_DECODER_H
7#define DEEPLIMA_APPS_UTF8_DECODER_H
8
9#include <cassert>
10#include <limits>
11#include <stdexcept>
12
13#include <string.h>
14#include <unicode/uchar.h>
15#include <unicode/uscript.h>
16
17#include "segm_lexicon.h"
18
19namespace deeplima
20{
21namespace segmentation
22{
23namespace impl
24{
25
26#define UTF8_DECODER_CHAR_HISTORY_MASK 0x7FFFFFFFFFFFFFFFull
27// 21 * 3 characters = 63 bits accepted
28
37
38const uint8_t bits_per_position_table[] = {
39 21, // Unicode code point
40 8, // Unicode class
41 3, // UTF-8 character len in bytes (1 .. 6)
42 1, // Script changed since prev code point (Yes / No)
43 SegmLexicon::BITS // Lexicon words ending here, by length (SegmLexicon)
44};
45
46template <class TBufferType = uint64_t, int NUM_CHANNELS = 5, int BYTES_BUFFER_SIZE = 8>
48{
49protected:
50 typedef TBufferType buffer_t;
51
52private:
53 uint8_t m_bytes_buffer[BYTES_BUFFER_SIZE];
54 int32_t m_bytes_left_to_parse;
55
56 buffer_t m_buffer[NUM_CHANNELS];
57
58 uint8_t m_prev_script_code;
59
60 // Lexicon feature: the word list (none: the channel stays 0) and the last
61 // characters read, oldest first
62 const SegmLexicon* m_lexicon = nullptr;
63 char32_t m_history[SegmLexicon::MAX_LEN];
64 uint8_t m_history_len = 0;
65
66 inline void push_value(uc_channel_t ch, uint32_t value)
67 {
68 m_buffer[ch] = (m_buffer[ch] << bits_per_position(ch)) | buffer_t(value);
69 }
70
71public:
72
74 : m_bytes_left_to_parse(0),
75 m_prev_script_code(USCRIPT_COMMON)
76 {
77 assert(USCRIPT_CODE_LIMIT < std::numeric_limits<uint8_t>::max());
78
79 reset();
80 }
81
82 void reset()
83 {
84 m_bytes_left_to_parse = 0;
85 m_prev_script_code = USCRIPT_COMMON;
86 memset(m_bytes_buffer, 0, BYTES_BUFFER_SIZE);
87 memset(m_buffer, 0, sizeof(buffer_t) * NUM_CHANNELS);
88 m_history_len = 0;
89 }
90
91 // The word list of the lexicon channel (nullptr: no lexicon). Not owned.
92 void set_lexicon(const SegmLexicon* lexicon)
93 {
94 m_lexicon = lexicon;
95 }
96
97 inline uint32_t parse_start(const uint8_t* str, int32_t* pos, int32_t len)
98 {
99 assert(nullptr != str);
100 assert(0 == *pos);
101 assert(len > 0);
102 if (m_bytes_left_to_parse > 0)
103 {
104 uint8_t bytes_to_copy = len > 4 ? 4 : len;
105 memcpy(m_bytes_buffer + m_bytes_left_to_parse, str, bytes_to_copy);
106
107 [[maybe_unused]] uint8_t char_len = parse(m_bytes_buffer, pos, m_bytes_left_to_parse + bytes_to_copy);
108 assert(char_len > 0);
109
110 *pos = *pos - m_bytes_left_to_parse;
111 uint32_t rv = m_bytes_left_to_parse;
112 m_bytes_left_to_parse = 0;
113
114 return rv;
115 }
116
117 return 0;
118 }
119
120 inline uint8_t parse(const uint8_t* str, int32_t* pos, int32_t len)
121 {
122 UChar32 uch = 0;
123 int32_t prev_pos = *pos;
124 // * @param s const uint8_t * string
125 // * @param i int32_t string offset, must be i<length
126 // * @param length int32_t string length
127 // * @param c output UChar32 variable, set to <0 in case of an error
128 // uint32_t uuch;
129 // int32_t offset = *pos;
130 // int32_t length = len;
131 // U8_NEXT(str, offset, length, uuch);
132 // U8_NEXT(str, int32_t(*pos), int32_t(len), uuch);
133 // U8_NEXT(str, *pos, len, uint32_t(uch));
134 U8_NEXT(str, *pos, len, uch);
135 // uch = uuch;
136 if (uch < 0)
137 {
138 uint32_t bytes_left = len - prev_pos;
139 if (bytes_left < 4)
140 {
141 m_bytes_left_to_parse = bytes_left;
142 memcpy(m_bytes_buffer, str + prev_pos, m_bytes_left_to_parse);
143 return 0;
144 // incomplete sequence
145 }
146 throw std::runtime_error("Incorrect UTF-8 sequence.");
147 }
148
149 push_value(channel_char, uch);
150
151 push_value(channel_type, u_charType(uch));
152
153 UErrorCode err = U_ZERO_ERROR;
154#ifdef NDEBUG
155 uint8_t script_code = uscript_getScript(uch, &err);
156#else
157 UScriptCode raw_script_code = uscript_getScript(uch, &err);
158 assert(err == U_ZERO_ERROR);
159 assert(raw_script_code >= 0);
160 assert(raw_script_code < USCRIPT_CODE_LIMIT);
161 uint8_t script_code = raw_script_code;
162#endif
163 push_value(channel_script_change, m_prev_script_code == script_code ? 0x00 : 0x01);
164 m_prev_script_code = script_code;
165
166 uint8_t char_len = *pos - prev_pos;
167 assert(char_len > 0);
168 assert(char_len <= 6);
169 push_value(channel_len, char_len);
170
171 if (NUM_CHANNELS > channel_lexicon)
172 {
173 uint32_t mask = 0;
174 if (nullptr != m_lexicon)
175 {
176 if (m_history_len == SegmLexicon::MAX_LEN)
177 {
178 memmove(m_history, m_history + 1, sizeof(char32_t) * (SegmLexicon::MAX_LEN - 1));
179 m_history_len--;
180 }
181 m_history[m_history_len++] = char32_t(uch);
182 mask = m_lexicon->mask(m_history, m_history_len);
183 }
184 push_value(channel_lexicon, mask);
185 }
186
187 return char_len;
188 }
189
190 inline const buffer_t& get_buffer(uint8_t channel_idx) const
191 {
192 assert(channel_idx < NUM_CHANNELS);
193 return m_buffer[channel_idx];
194 }
195
196 inline uint8_t get_len(uint8_t idx) const
197 {
198 return (m_buffer[channel_len] >> (bits_per_position(channel_len) * idx)) & 0x07;
199 }
200
201 inline static uint8_t bits_per_position(uint8_t channel_idx)
202 {
203 assert(channel_idx < NUM_CHANNELS);
204 return bits_per_position_table[channel_idx];
205 }
206};
207
208} // namespace impl
209} // namespace segmentation
210} // namespace deeplima
211
212#endif
Word list for the segmenter's lexicon feature (ngram_descr_t::lexicon_ngram).
uint8_t get_len(uint8_t idx) const
void set_lexicon(const SegmLexicon *lexicon)
Definition utf8_reader.h:92
uint32_t parse_start(const uint8_t *str, int32_t *pos, int32_t len)
Definition utf8_reader.h:97
static uint8_t bits_per_position(uint8_t channel_idx)
const buffer_t & get_buffer(uint8_t channel_idx) const
uint8_t parse(const uint8_t *str, int32_t *pos, int32_t len)
const uint8_t bits_per_position_table[]
Definition utf8_reader.h:38
Edit rules for lemmatizing words absent from the lemma dictionary.