6#ifndef SRC_INCLUDE_SEGMENTATION_TRAIN_CHAR_DICT_BUILDER_H
7#define SRC_INCLUDE_SEGMENTATION_TRAIN_CHAR_DICT_BUILDER_H
11#include <unordered_map>
26template <
class InputEncoder>
51 uint64_t& char_counter)
53 std::vector<std::unordered_map<uint64_t, uint64_t>> temp_dicts;
60 dicts.resize(temp_dicts.size());
61 for (
size_t i = 0; i < temp_dicts.size(); i++)
65 dicts[i] = std::make_shared<UInt64Dict>(
m_unk,
66 temp_dicts[i].begin(), temp_dicts[i].end(),
67 [char_counter, min_ipm](uint64_t c){
68 return ipm(c, char_counter) > min_ipm;
73 dicts[i] = std::make_shared<UInt64Dict>(temp_dicts[i].begin(), temp_dicts[i].end());
83 void process(
const std::wstring& text, uint32_t min_ipm)
85 std::vector<std::unordered_map<uint64_t, uint64_t>> temp_dicts;
88 std::unordered_map<uint64_t, uint64_t> char_dict;
89 for (
size_t i = 0; i < text.size(); i++)
91 uint64_t v = (uint64_t)(text[i]);
92 auto it = char_dict.find(v);
93 if (char_dict.end() == it)
103 uint64_t total = text.size();
105 dicts.resize(temp_dicts.size());
106 dicts[0] = std::make_shared<UInt64Dict>(
m_unk, char_dict,
107 [total, min_ipm](uint64_t c){
108 return ipm(c, total) > min_ipm;
116 inline static float ipm(uint64_t count, uint64_t total)
118 return float(count * 1000000) / total;
122 uint64_t& char_counter,
123 std::vector<std::unordered_map<uint64_t, uint64_t>>& temp_dicts)
130 m_input_encoder.warmup((
const uint8_t*)text.data(), &pos, text.size());
133 while (
size_t(pos) < text.size())
135 if (
m_input_encoder.parse((
const uint8_t*)text.data(), &pos, text.size()) > 0)
141 char final_spaces[] =
" ";
151 throw std::runtime_error(
"Something wrong.");
157 std::vector<std::unordered_map<uint64_t, uint64_t>>& temp_dicts)
163 auto it = temp_dicts[i].find(v);
164 if (temp_dicts[i].end() == it)
166 temp_dicts[i][v] = 1;
174 if (char_counter == std::numeric_limits<uint64_t>::max())
176 throw std::overflow_error(
"Too much characters in training set.");
Word list for the segmenter's lexicon feature (ngram_descr_t::lexicon_ngram).
DictsHolder process(const std::string &text, uint32_t min_ipm, uint64_t &char_counter)
DictionaryBuilderImpl(const std::vector< impl::ngram_descr_t > &ngram_descr)
uint64_t get_num_features() const
void process(const std::wstring &text, uint32_t min_ipm)
void handle_timepoint(uint64_t &char_counter, std::vector< std::unordered_map< uint64_t, uint64_t > > &temp_dicts)
static float ipm(uint64_t count, uint64_t total)
void set_lexicon(const impl::SegmLexicon *lexicon)
void read_string(const std::string &text, uint64_t &char_counter, std::vector< std::unordered_map< uint64_t, uint64_t > > &temp_dicts)
CharNgramEncoder< Utf8Reader<> > m_input_encoder
Edit rules for lemmatizing words absent from the lemma dictionary.
DictsHolderImpl DictsHolder