LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
dict.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
2//
3// SPDX-License-Identifier: MIT
4
5#include "dict.h"
6
7using namespace std;
8
9namespace deeplima
10{
11
12#define DEFINE_DICT_CLASS_ID(x) \
13 template <> const std::string x::m_class_id = #x;
14
15DEFINE_DICT_CLASS_ID(Dict<wstring>)
16DEFINE_DICT_CLASS_ID(Dict<string>)
17DEFINE_DICT_CLASS_ID(Dict<uint64_t>)
18DEFINE_DICT_CLASS_ID(Dict<char32_t>)
19
20template <class T>
21c10::IValue Dict<T>::toIValue() const
22{
23 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
24
25 c10::List<std::string> l;
26 l.reserve(i2v.size());
27 for ( const T& v : i2v )
28 {
29 std::string s = converter.to_bytes(to_wstring(v));
30 l.push_back(s);
31 }
32 return l;
33}
34
35template <class T>
36void Dict<T>::fromIValue(const c10::IValue& v)
37{
38 if (!v.isList())
39 {
40 throw std::runtime_error("Dict expects c10::List object.");
41 }
42
43 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
44
45 const c10::List<c10::IValue>& l = v.toList();
46 for (size_t i = 0; i < l.size(); i++)
47 {
48 if (!l.get(i).isString())
49 {
50 throw std::runtime_error("Dict expects list of strings.");
51 }
52 const std::string& str = l.get(i).toStringRef();
53 std::wstring wstr = converter.from_bytes(str);
54 T k;
55 from_wstring(wstr, k);
56 add(k);
57 }
58}
59
60// Dict specialisation for T=wstring
61
62template <>
63void Dict<wstring>::fromIValue(const c10::IValue& v)
64{
65 if (!v.isList())
66 {
67 throw std::runtime_error("Dict expects c10::List object.");
68 }
69
70 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
71
72 const c10::List<c10::IValue>& l = v.toList();
73 for (size_t i = 0; i < l.size(); i++)
74 {
75 if (!l.get(i).isString())
76 {
77 throw std::runtime_error("Dict expects list of strings.");
78 }
79 const std::string& str = l.get(i).toStringRef();
80 std::wstring wstr = converter.from_bytes(str);
81 wstring k;
82 from_wstring(wstr, k);
83 add(k);
84 }
85}
86
87template <>
88c10::IValue Dict<wstring>::toIValue() const
89{
90 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
91
92 c10::List<std::string> l;
93 l.reserve(i2v.size());
94 for ( const wstring& v : i2v )
95 {
96 std::string s = converter.to_bytes(to_wstring(v));
97 l.push_back(s);
98 }
99 return l;
100}
101
102// Dict specialisation for T=string
103
104template <>
105void Dict<string>::fromIValue(const c10::IValue& v)
106{
107 if (!v.isList())
108 {
109 throw std::runtime_error("Dict expects c10::List object.");
110 }
111
112 const c10::List<c10::IValue>& l = v.toList();
113 for (size_t i = 0; i < l.size(); i++)
114 {
115 if (!l.get(i).isString())
116 {
117 throw std::runtime_error("Dict expects list of strings.");
118 }
119 const std::string& k = l.get(i).toStringRef();
120 add(k);
121 }
122}
123
124template <>
125c10::IValue Dict<string>::toIValue() const
126{
127 c10::List<std::string> l;
128 l.reserve(i2v.size());
129 for ( const string& v : i2v )
130 {
131 l.push_back(v);
132 }
133 return l;
134}
135
136// Dict specialisation for T=char32_t
137
138template <>
139void Dict<char32_t>::fromIValue(const c10::IValue& v)
140{
141 std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
142
143 if (!v.isList())
144 {
145 throw std::runtime_error("Dict expects c10::List object.");
146 }
147
148 const c10::List<c10::IValue>& l = v.toList();
149 for (size_t i = 0; i < l.size(); i++)
150 {
151 if (!l.get(i).isString())
152 {
153 throw std::runtime_error("Dict expects list of strings.");
154 }
155 const std::string& k = l.get(i).toStringRef();
156 //assert((0 == i && 0 == k.size()) || 1 == k.size());
157 std::u32string u32str = converter.from_bytes(k);
158 assert((0 == i && 0 == u32str.size()) || 1 == u32str.size());
159 add(u32str[0]);
160 }
161}
162
163template <>
164c10::IValue Dict<char32_t>::toIValue() const
165{
166 std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
167
168 c10::List<std::string> l;
169 l.reserve(i2v.size());
170 for ( const char32_t v : i2v )
171 {
172 std::string s = converter.to_bytes(to_wstring(v));
173 l.push_back(s);
174 }
175 return l;
176}
177
178// Dict specialisation for T=uint64_t
179
180template <>
181c10::IValue Dict<uint64_t>::toIValue() const
182{
183 c10::List<int64_t> l;
184
185 l.reserve(i2v.size());
186 for ( const uint64_t v : i2v )
187 {
188 l.push_back(int64_t(v));
189 }
190 return l;
191}
192
193template <>
194void Dict<uint64_t>::fromIValue(const c10::IValue& v)
195{
196 if (!v.isList())
197 {
198 throw std::runtime_error("Dict expects c10::List object.");
199 }
200
201 const c10::List<int64_t>& l = v.toIntList();
202 for (size_t i = 0; i < l.size(); i++)
203 {
204 add(l.get(i));
205 }
206}
207
208} // namespace deeplima
209
virtual void fromIValue(const c10::IValue &v)
Definition dict.cpp:36
virtual c10::IValue toIValue() const
Definition dict.cpp:21
#define DEFINE_DICT_CLASS_ID(x)
Definition dict.cpp:12
STL namespace.