LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
dict.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_SRC_STATIC_GRAPH_DICT_H
7#define DEEPLIMA_SRC_STATIC_GRAPH_DICT_H
8
9#include <vector>
10#include <unordered_map>
11#include <string>
12#include <codecvt>
13
14#include "dict_base.h"
15
16namespace deeplima
17{
18
19// static_graph is a static library: Dict must not be declared dllimport by its
20// users (there is no DLL to import it from), so the macro is empty everywhere.
21#define DICT_EXPORT
22
23
24template <class T>
26{
27public:
28 typedef T value_t;
29 typedef typename std::vector<T>::size_type key_t;
30
31 Dict(const std::vector<T>& v1, const std::vector<T>& v2 = {})
32 {
33 i2v.reserve(v1.size() + v2.size());
34
35 for (const T& v : v1)
36 {
37 add(v);
38 }
39
40 for (const T& v : v2)
41 {
42 add(v);
43 }
44 }
45
46 Dict(const T& UNK, const std::vector<T>& v1, const std::vector<T>& v2 = {})
47 {
48 i2v.reserve(v1.size() + v2.size() + 1);
49 add(UNK);
50
51 for (const T& v : v1)
52 {
53 add(v);
54 }
55
56 for (const T& v : v2)
57 {
58 add(v);
59 }
60 }
61
62 template <class InputIt>
63 explicit Dict(InputIt begin, InputIt end)
64 : Dict(begin, end, [](uint64_t /*c*/){ return true; })
65 { }
66
67 template<class InputIt, typename F>
68 Dict(const T& UNK, InputIt begin, InputIt end, F f)
69 {
70 size_t count = 1;
71 for (auto it = begin; it != end; ++it)
72 {
73 if (f(it->second))
74 {
75 count++;
76 }
77 }
78
79 i2v.reserve(count);
80 add(UNK);
81 for (auto it = begin; it != end; ++it)
82 {
83 if (f(it->second))
84 {
85 add(it->first);
86 }
87 }
88 }
89
90 template<class InputIt, typename F>
91 Dict(const T& UNK, const T& EOS, InputIt begin, InputIt end, F f)
92 {
93 size_t count = 2;
94 for (auto it = begin; it != end; ++it)
95 {
96 if (f(it->second))
97 {
98 count++;
99 }
100 }
101
102 i2v.reserve(count);
103 add(UNK);
104 add(EOS);
105 for (auto it = begin; it != end; ++it)
106 {
107 if (f(it->second))
108 {
109 add(it->first);
110 }
111 }
112 }
113
114 template<class InputIt, typename F>
115 Dict(const T& UNK, const T& EOS, const T& OTHER, InputIt begin, InputIt end, F f)
116 {
117 size_t count = 3;
118 for (auto it = begin; it != end; ++it)
119 {
120 if (f(it->second))
121 {
122 count++;
123 }
124 }
125
126 i2v.reserve(count);
127 add(UNK);
128 add(EOS);
129 add(OTHER);
130 for (auto it = begin; it != end; ++it)
131 {
132 if (f(it->second))
133 {
134 add(it->first);
135 }
136 }
137 }
138
139 template<class InputIt, typename F>
140 Dict(InputIt begin, InputIt end, F f)
141 {
142 size_t count = 0;
143 for (auto it = begin; it != end; ++it)
144 {
145 if (f(it->second))
146 {
147 count++;
148 }
149 }
150
151 i2v.reserve(count);
152 for (auto it = begin; it != end; ++it)
153 {
154 if (f(it->second))
155 {
156 add(it->first);
157 }
158 }
159 }
160
161 Dict() {}
162
163 inline const T& get_value(key_t idx) const
164 {
165 if (idx >= i2v.size())
166 {
167 throw std::range_error("Index too big in Dict::get_value.");
168 }
169 return i2v[idx];
170 }
171
172 inline key_t get_idx(const T& val) const
173 {
174 typename std::unordered_map<T, key_t>::const_iterator i = v2i.find(val);
175 if (v2i.cend() == i)
176 {
177 return 0;
178 }
179 return i->second;
180 }
181
182 virtual uint64_t size() const
183 {
184 return i2v.size();
185 }
186
187 inline key_t get_unk() const
188 {
189 return 0;
190 }
191
192 std::string to_string()
193 {
194 std::stringstream ss;
195
196 for (size_t i = 0; i < i2v.size(); i++)
197 {
198 ss << i2v[i] << std::endl;
199 }
200
201 return ss.str();
202 }
203
204 const std::unordered_map<T, key_t>& get_v2i() const
205 {
206 return v2i;
207 }
208
209protected:
210 static inline std::wstring to_wstring(const std::wstring& s)
211 {
212 return s;
213 }
214
215 static inline std::u32string to_wstring(char32_t ch)
216 {
217 char32_t v[2] = { ch, 0 };
218 return std::u32string(v);
219 }
220
221 static inline void from_wstring(const std::wstring& src, std::wstring& k)
222 {
223 k = src;
224 }
225
226 static inline void from_wstring(const std::wstring& src, wchar_t& k)
227 {
228 if (src.length() < 1)
229 {
230 throw std::runtime_error("Dict::from_wstring empty input.");
231 }
232 k = src[0];
233 }
234
235public:
236
237 static inline const std::string& class_id()
238 {
239 return m_class_id;
240 }
241
242 virtual const std::string& get_class_id() const
243 {
244 return class_id();
245 }
246
247 virtual c10::IValue toIValue() const;
248 virtual void fromIValue(const c10::IValue& v);
249
250protected:
251 inline void add(const T& val)
252 {
253 i2v.push_back(val);
254 if (v2i.cend() != v2i.find(val))
255 {
256 throw std::logic_error("Elements must be unique");
257 }
258 v2i[val] = i2v.size() - 1;
259 }
260
261 std::unordered_map<T, key_t> v2i;
262 std::vector<T> i2v;
263
264 const static std::string m_class_id;
265};
266
271
272} // namespace deeplima
273
274#endif
const T & get_value(key_t idx) const
Definition dict.h:163
virtual uint64_t size() const
Definition dict.h:182
virtual const std::string & get_class_id() const
Definition dict.h:242
Dict(const T &UNK, const std::vector< T > &v1, const std::vector< T > &v2={})
Definition dict.h:46
Dict(const std::vector< T > &v1, const std::vector< T > &v2={})
Definition dict.h:31
std::vector< T >::size_type key_t
Definition dict.h:29
virtual void fromIValue(const c10::IValue &v)
Definition dict.cpp:36
static std::wstring to_wstring(const std::wstring &s)
Definition dict.h:210
key_t get_unk() const
Definition dict.h:187
const std::unordered_map< T, key_t > & get_v2i() const
Definition dict.h:204
void add(const std::wstring &val)
Definition dict.h:251
static void from_wstring(const std::wstring &src, wchar_t &k)
Definition dict.h:226
Dict(const T &UNK, InputIt begin, InputIt end, F f)
Definition dict.h:68
Dict(InputIt begin, InputIt end, F f)
Definition dict.h:140
static std::u32string to_wstring(char32_t ch)
Definition dict.h:215
std::vector< std::wstring > i2v
Definition dict.h:262
Dict(const T &UNK, const T &EOS, InputIt begin, InputIt end, F f)
Definition dict.h:91
static const std::string & class_id()
Definition dict.h:237
Dict(InputIt begin, InputIt end)
Definition dict.h:63
Dict(const T &UNK, const T &EOS, const T &OTHER, InputIt begin, InputIt end, F f)
Definition dict.h:115
std::string to_string()
Definition dict.h:192
static const std::string m_class_id
Definition dict.h:264
virtual c10::IValue toIValue() const
Definition dict.cpp:21
static void from_wstring(const std::wstring &src, std::wstring &k)
Definition dict.h:221
key_t get_idx(const T &val) const
Definition dict.h:172
std::unordered_map< std::wstring, key_t > v2i
Definition dict.h:261
#define DICT_EXPORT
Definition dict.h:21
Edit rules for lemmatizing words absent from the lemma dictionary.
Dict< char32_t > Char32Dict
Definition dict.h:270
Dict< uint64_t > UInt64Dict
Definition dict.h:269
Dict< std::wstring > WstringDict
Definition dict.h:267
Dict< std::string > StringDict
Definition dict.h:268