LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
char_dict_builder.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 SRC_INCLUDE_SEGMENTATION_TRAIN_CHAR_DICT_BUILDER_H
7#define SRC_INCLUDE_SEGMENTATION_TRAIN_CHAR_DICT_BUILDER_H
8
9#include <string>
10#include <vector>
11#include <unordered_map>
12#include <memory>
13
15#include "static_graph/dict.h"
18
19namespace deeplima
20{
21namespace segmentation
22{
23namespace train
24{
25
26template <class InputEncoder>
28{
29public:
30 DictionaryBuilderImpl(const std::vector<impl::ngram_descr_t>& ngram_descr)
31 : m_input_encoder(ngram_descr),
32 m_unk(~0)
33 {
34 assert(sizeof(uint64_t) >= sizeof(m_input_encoder.get_feat(0)));
35 }
36
37 // The word list of the lexicon feature (not owned), before process
38 void set_lexicon(const impl::SegmLexicon* lexicon)
39 {
40 m_input_encoder.set_lexicon(lexicon);
41 }
42
43 inline uint64_t get_num_features() const
44 {
45 assert(m_input_encoder.size() < std::numeric_limits<int64_t>::max());
46 return m_input_encoder.size();
47 }
48
49 DictsHolder process(const std::string& text,
50 uint32_t min_ipm,
51 uint64_t& char_counter)
52 {
53 std::vector<std::unordered_map<uint64_t, uint64_t>> temp_dicts;
54 temp_dicts.resize(m_input_encoder.size());
55
56 char_counter = 0;
57 read_string(text, char_counter, temp_dicts);
58
59 DictsHolder dicts;
60 dicts.resize(temp_dicts.size());
61 for (size_t i = 0; i < temp_dicts.size(); i++)
62 {
63 if (m_input_encoder.allow_unk(i))
64 {
65 dicts[i] = std::make_shared<UInt64Dict>(m_unk,
66 temp_dicts[i].begin(), temp_dicts[i].end(),
67 [char_counter, min_ipm](uint64_t c){
68 return ipm(c, char_counter) > min_ipm;
69 });
70 }
71 else
72 {
73 dicts[i] = std::make_shared<UInt64Dict>(temp_dicts[i].begin(), temp_dicts[i].end());
74 }
75 }
76
77 // std::cerr << "dicts[0].size() == " << dicts[0]->size() << std::endl;
78 //std::cout << dicts[0].to_string();
79
80 return dicts;
81 }
82
83 void process(const std::wstring& text, uint32_t min_ipm)
84 {
85 std::vector<std::unordered_map<uint64_t, uint64_t>> temp_dicts;
86 temp_dicts.resize(m_input_encoder.size());
87
88 std::unordered_map<uint64_t, uint64_t> char_dict;
89 for (size_t i = 0; i < text.size(); i++)
90 {
91 uint64_t v = (uint64_t)(text[i]);
92 auto it = char_dict.find(v);
93 if (char_dict.end() == it)
94 {
95 char_dict[v] = 1;
96 }
97 else
98 {
99 it->second++;
100 }
101 }
102
103 uint64_t total = text.size();
104 DictsHolder dicts;
105 dicts.resize(temp_dicts.size());
106 dicts[0] = std::make_shared<UInt64Dict>(m_unk, char_dict,
107 [total, min_ipm](uint64_t c){
108 return ipm(c, total) > min_ipm;
109 });
110
111 // std::cerr << "dicts[0].size() == " << dicts[0]->size() << std::endl;
112 }
113
114protected:
115
116 inline static float ipm(uint64_t count, uint64_t total)
117 {
118 return float(count * 1000000) / total;
119 }
120
121 void read_string(const std::string& text,
122 uint64_t& char_counter,
123 std::vector<std::unordered_map<uint64_t, uint64_t>>& temp_dicts)
124 {
125 int32_t pos = 0;
126 char_counter = 0;
127
128 while (! m_input_encoder.ready_to_generate())
129 {
130 m_input_encoder.warmup((const uint8_t*)text.data(), &pos, text.size());
131 }
132
133 while (size_t(pos) < text.size())
134 {
135 if (m_input_encoder.parse((const uint8_t*)text.data(), &pos, text.size()) > 0)
136 {
137 handle_timepoint(char_counter, temp_dicts);
138 }
139 }
140
141 char final_spaces[] = " ";
142 for (size_t i = 0; i < m_input_encoder.get_lookahead(); i++)
143 {
144 int32_t pos = 0;
145 if (m_input_encoder.parse((uint8_t*)final_spaces, &pos, 1) > 0)
146 {
147 handle_timepoint(char_counter, temp_dicts);
148 }
149 else
150 {
151 throw std::runtime_error("Something wrong.");
152 }
153 }
154 }
155
156 inline void handle_timepoint(uint64_t& char_counter,
157 std::vector<std::unordered_map<uint64_t, uint64_t>>& temp_dicts)
158 {
159 for (size_t i = 0; i < m_input_encoder.size(); i++)
160 {
161 uint64_t v = m_input_encoder.get_feat(i);
162 assert(v != m_unk);
163 auto it = temp_dicts[i].find(v);
164 if (temp_dicts[i].end() == it)
165 {
166 temp_dicts[i][v] = 1;
167 }
168 else
169 {
170 it->second++;
171 }
172 }
173
174 if (char_counter == std::numeric_limits<uint64_t>::max())
175 {
176 throw std::overflow_error("Too much characters in training set.");
177 }
178
179 char_counter++;
180 }
181
182 InputEncoder m_input_encoder;
183 uint64_t m_unk;
184};
185
186} // namespace train
187} // namespace segmentation
188} // namespace deeplima
189
190#endif
Word list for the segmenter's lexicon feature (ngram_descr_t::lexicon_ngram).
DictsHolder process(const std::string &text, uint32_t min_ipm, uint64_t &char_counter)
DictionaryBuilderImpl(const std::vector< impl::ngram_descr_t > &ngram_descr)
void process(const std::wstring &text, uint32_t min_ipm)
void handle_timepoint(uint64_t &char_counter, std::vector< std::unordered_map< uint64_t, uint64_t > > &temp_dicts)
static float ipm(uint64_t count, uint64_t total)
void set_lexicon(const impl::SegmLexicon *lexicon)
void read_string(const std::string &text, uint64_t &char_counter, std::vector< std::unordered_map< uint64_t, uint64_t > > &temp_dicts)
Edit rules for lemmatizing words absent from the lemma dictionary.
DictsHolderImpl DictsHolder
Definition dict_base.h:42