6#ifndef DEEPLIMA_SRC_STATIC_GRAPH_DICT_H
7#define DEEPLIMA_SRC_STATIC_GRAPH_DICT_H
10#include <unordered_map>
29 typedef typename std::vector<T>::size_type
key_t;
31 Dict(
const std::vector<T>& v1,
const std::vector<T>& v2 = {})
33 i2v.reserve(v1.size() + v2.size());
46 Dict(
const T& UNK,
const std::vector<T>& v1,
const std::vector<T>& v2 = {})
48 i2v.reserve(v1.size() + v2.size() + 1);
62 template <
class InputIt>
63 explicit Dict(InputIt begin, InputIt end)
64 :
Dict(begin, end, [](uint64_t ){
return true; })
67 template<
class InputIt,
typename F>
68 Dict(
const T& UNK, InputIt begin, InputIt end, F f)
71 for (
auto it = begin; it != end; ++it)
81 for (
auto it = begin; it != end; ++it)
90 template<
class InputIt,
typename F>
91 Dict(
const T& UNK,
const T& EOS, InputIt begin, InputIt end, F f)
94 for (
auto it = begin; it != end; ++it)
105 for (
auto it = begin; it != end; ++it)
114 template<
class InputIt,
typename F>
115 Dict(
const T& UNK,
const T& EOS,
const T& OTHER, InputIt begin, InputIt end, F f)
118 for (
auto it = begin; it != end; ++it)
130 for (
auto it = begin; it != end; ++it)
139 template<
class InputIt,
typename F>
140 Dict(InputIt begin, InputIt end, F f)
143 for (
auto it = begin; it != end; ++it)
152 for (
auto it = begin; it != end; ++it)
165 if (idx >=
i2v.size())
167 throw std::range_error(
"Index too big in Dict::get_value.");
174 typename std::unordered_map<T, key_t>::const_iterator i =
v2i.find(val);
194 std::stringstream ss;
196 for (
size_t i = 0; i <
i2v.size(); i++)
198 ss <<
i2v[i] << std::endl;
204 const std::unordered_map<T, key_t>&
get_v2i()
const
217 char32_t v[2] = { ch, 0 };
218 return std::u32string(v);
221 static inline void from_wstring(
const std::wstring& src, std::wstring& k)
228 if (src.length() < 1)
230 throw std::runtime_error(
"Dict::from_wstring empty input.");
251 inline void add(
const T& val)
254 if (
v2i.cend() !=
v2i.find(val))
256 throw std::logic_error(
"Elements must be unique");
258 v2i[val] =
i2v.size() - 1;
261 std::unordered_map<T, key_t>
v2i;
const T & get_value(key_t idx) const
virtual uint64_t size() const
virtual const std::string & get_class_id() const
Dict(const T &UNK, const std::vector< T > &v1, const std::vector< T > &v2={})
Dict(const std::vector< T > &v1, const std::vector< T > &v2={})
std::vector< T >::size_type key_t
virtual void fromIValue(const c10::IValue &v)
static std::wstring to_wstring(const std::wstring &s)
const std::unordered_map< T, key_t > & get_v2i() const
void add(const std::wstring &val)
static void from_wstring(const std::wstring &src, wchar_t &k)
Dict(const T &UNK, InputIt begin, InputIt end, F f)
Dict(InputIt begin, InputIt end, F f)
static std::u32string to_wstring(char32_t ch)
std::vector< std::wstring > i2v
Dict(const T &UNK, const T &EOS, InputIt begin, InputIt end, F f)
static const std::string & class_id()
Dict(InputIt begin, InputIt end)
Dict(const T &UNK, const T &EOS, const T &OTHER, InputIt begin, InputIt end, F f)
static const std::string m_class_id
virtual c10::IValue toIValue() const
static void from_wstring(const std::wstring &src, std::wstring &k)
key_t get_idx(const T &val) const
std::unordered_map< std::wstring, key_t > v2i
Edit rules for lemmatizing words absent from the lemma dictionary.
Dict< char32_t > Char32Dict
Dict< uint64_t > UInt64Dict
Dict< std::wstring > WstringDict
Dict< std::string > StringDict