6#ifndef DEEPLIMA_LIBS_MORPH_MODEL_H
7#define DEEPLIMA_LIBS_MORPH_MODEL_H
14#include <unordered_map>
44 : m_value(other.m_value) { }
50 assert((m_value & val) == 0);
56 return m_value == other.m_value;
61 return m_value != other.m_value;
66 return m_value < other.m_value;
76 return (m_value & mask) >> offset;
81 return std::hash<feat_base_t>{}(m_value);
85class morph_model_builder;
96 std::unordered_map<std::string, size_t> m_key2id;
97 std::vector<std::string> m_id2key;
100 dict_t(
const std::string& str);
101 static dict_t from_string(
const std::string& str);
105 const std::vector<std::string>& get_vec_ref()
const
110 size_t get_id(
const std::string& name)
const
112 auto it = m_key2id.find(name);
113 assert(m_key2id.end() != it);
121 const std::string& get_key(
size_t id)
const
123 assert(
id < m_id2key.size());
127 size_t add_key(
const std::string& name)
129 if (m_key2id.end() == m_key2id.find(name))
131 m_id2key.push_back(name);
132 m_key2id[name] = m_id2key.size() - 1;
137 bool has(
const std::string& name)
const
139 auto it = m_key2id.find(name);
140 return m_key2id.end() != it;
157 return m_upos_dict.get_vec_ref();
159 return m_feats[feat_id].get_vec_ref();
163 inline bool has_feat(
const std::string& feat_name)
const
165 return m_feat_dict.has(feat_name);
170 return m_feat_dict.get_key(feat_id);
175 return m_feat_dict.get_id(feat_name);
192 size_t feat_mask_id = it->second;
198 return m_upos_dict.get_id(name);
204 return m_upos_dict.add_key(name);
209 return m_feat_dict.get_id(name);
214 return m_feat_dict.add_key(name);
241 bool can_convert(
const std::string& upos,
const std::map<std::string, std::set<std::string>>& feats)
const
243 if (!m_upos_dict.has(upos))
247 const auto& feats2mask =
m_feats2mask[m_upos_dict.get_id(upos)];
248 for (
const auto& kv : feats)
250 if (kv.second.size() != 1 || !m_feat_dict.has(kv.first))
254 const size_t feat_id = m_feat_dict.get_id(kv.first);
255 if (feats2mask.end() == feats2mask.find(feat_id) || !
m_feats[feat_id].has(*kv.second.begin()))
263 morph_feats_t convert(
const std::string& upos,
const std::map<std::string, std::set<std::string>>& feats)
const;
265 template <
typename F>
268 auto upos_idx = m_feat_dict.get_id(
"upos");
269 assert(upos_idx != std::numeric_limits<size_t>::max());
270 auto upos_id = featid2value(upos_idx);
271 const std::map<std::string, std::set<std::string>> feats;
277 for (
const auto& kv : feats2mask )
279 const auto feat_id = kv.first;
280 if (feat_id == upos_idx)
285 size_t mask_id = kv.second;
289 size_t feat_value_id = featid2value(feat_id);
290 if (feat_value_id != std::numeric_limits<size_t>::max())
292 feat_value_id = feat_value_id << offset ;
Encoding on one 64 bits integer of the set of morphological features for one token.
bool operator==(const morph_feats_t other) const
bool operator<(const morph_feats_t other) const
morph_feats_t(feat_base_t val)
feat_base_t subvalue(feat_base_t mask, uint8_t offset) const
bool operator!=(const morph_feats_t other) const
const feat_base_t & toBaseType() const
morph_feats_t(const morph_feats_t &other)
void append(feat_base_t val)
size_t get_feat_idx(const std::string &feat_name) const
bool can_convert(const std::string &upos, const std::map< std::string, std::set< std::string > > &feats) const
True when convert(upos, feats) can encode this UPOS and these features: convert only asserts it,...
friend class morph_model_builder
bool has_feat(const std::string &feat_name) const
const std::string & decode_upos_to_str(const morph_feats_t &feats) const
morph_feats_t convert(F featid2value) const
std::vector< std::map< size_t, size_t > > m_feats2mask
size_t get_feat_id(const std::string &name) const
size_t get_upos_id(const std::string &name) const
morph_feats_t convert(const std::string &upos, const std::map< std::string, std::set< std::string > > &feats) const
std::vector< uint8_t > m_offset
const std::vector< std::string > & get_feat_vec_ref(size_t feat_id) const
std::string to_string() const
const std::string & get_feat_name(size_t feat_id) const
feat_base_t decode_feat(const morph_feats_t &feats, size_t feat_id) const
feat_base_t decode_upos(const morph_feats_t &feats) const
size_t get_feats_count() const
size_t add_upos(const std::string &name)
size_t add_feat(const std::string &name)
std::vector< feat_base_t > m_masks
std::vector< dict_t > m_feats
Edit rules for lemmatizing words absent from the lemma dictionary.
std::string pretty_bits_to_string(const T arg)