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