37 static int RadicalPreHash(
const std::vector<int>& rs) {
39 for (
int radical : rs) {
47 typedef std::unordered_map<int, std::unique_ptr<std::vector<int>>>
RSMap;
51 static bool DecodeRadicalLine(
STRING* radical_data_line, RSMap* radical_map) {
52 if (radical_data_line->
length() == 0 || (*radical_data_line)[0] ==
'#')
55 radical_data_line->
split(
' ', &entries);
56 if (entries.
size() < 2)
return false;
58 int unicode = strtol(&entries[0][0], &end, 10);
59 if (*end !=
'\0')
return false;
60 std::unique_ptr<std::vector<int>> radicals(
new std::vector<int>);
61 for (
int i = 1; i < entries.
size(); ++i) {
62 int radical = strtol(&entries[i][0], &end, 10);
63 if (*end !=
'\0')
return false;
64 radicals->push_back(radical);
66 (*radical_map)[unicode] = std::move(radicals);
74 static bool DecodeRadicalTable(
STRING* radical_data, RSMap* radical_map) {
76 radical_data->
split(
'\n', &lines);
77 for (
int i = 0; i < lines.
size(); ++i) {
78 if (!DecodeRadicalLine(&lines[i], radical_map)) {
79 tprintf(
"Invalid format in radical table at line %d: %s\n", i,
92 encoder_ = src.encoder_;
93 code_range_ = src.code_range_;
103 STRING* radical_stroke_table) {
105 if (radical_stroke_table !=
nullptr &&
106 !DecodeRadicalTable(radical_stroke_table, &radical_map))
118 RSCounts radical_counts;
121 int hangul_offset = unicharset.
size();
127 int han_offset = hangul_offset + kTotalJamos;
128 int max_num_strokes = -1;
129 for (
int u = 0; u <= unicharset.
size(); ++u) {
132 if (u == unicharset.
size() && u != null_id)
break;
135 std::vector<char32> unicodes;
137 if (u < unicharset.
size())
139 if (u < unicharset.
size() &&
142 int unicode = unicodes[0];
143 int leading, vowel, trailing;
144 auto it = radical_map.find(unicode);
145 if (it != radical_map.end()) {
147 int num_radicals = it->second->size();
148 for (
int c = 0; c < num_radicals; ++c) {
149 code.
Set(c, han_offset + (*it->second)[c]);
151 int pre_hash = RadicalPreHash(*it->second);
152 int num_samples = radical_counts[pre_hash]++;
154 code.
Set(num_radicals, han_offset + num_samples + kRadicalRadix);
158 code.
Set3(leading + hangul_offset, vowel +
kLCount + hangul_offset,
173 for (
int i = 0; i < unicodes.size(); ++i) {
174 int position = code.
length();
176 tprintf(
"Unichar %d=%s is too long to encode!!\n", u,
180 int uni = unicodes[i];
187 if (direct_set.
size() >
190 tprintf(
"Code space expanded from original unicharset!!\n");
196 encoder_.push_back(code);
206 for (
int u = 0; u < unicharset.
size(); ++u) {
208 if (code->
length() <= i)
continue;
209 max_offset = std::max(max_offset, (*code)(i)-han_offset);
210 code->
Set(i, (*code)(i) + code_offset);
212 if (max_offset == 0)
break;
213 code_offset += max_offset + 1;
215 DefragmentCodeValues(null_id >= 0 ? 1 : -1);
224 for (
int u = 0; u < unicharset.
size(); ++u) {
231 code.
Set(0, unicharset.
size());
246 void UnicharCompress::DefragmentCodeValues(
int encoded_null) {
254 for (
int c = 0; c < encoder_.size(); ++c) {
256 for (
int i = 0; i < code.
length(); ++i) {
257 offsets[code(i)] = 1;
262 for (
int i = 0; i < offsets.
size(); ++i) {
265 if (offsets[i] == 0 || i == encoded_null) {
271 if (encoded_null >= 0) {
274 offsets[encoded_null] = offsets.
size() + offsets.
back() - encoded_null;
277 for (
int c = 0; c < encoder_.size(); ++c) {
279 for (
int i = 0; i < code->
length(); ++i) {
280 int value = (*code)(i);
281 code->
Set(i, value + offsets[value]);
290 if (unichar_id < 0 || unichar_id >= encoder_.size())
return 0;
291 *code = encoder_[unichar_id];
300 auto it = decoder_.find(code);
301 if (it == decoder_.end())
return INVALID_UNICHAR_ID;
307 return encoder_.SerializeClasses(fp);
312 if (!encoder_.DeSerializeClasses(fp))
return false;
328 for (
int c = 0; c < encoder_.size(); ++c) {
335 for (
int i = 1; i < code.
length(); ++i) {
360 *leading = offset / kNCount;
361 *vowel = (offset % kNCount) / kTCount;
367 void UnicharCompress::ComputeCodeRange() {
369 for (
int c = 0; c < encoder_.size(); ++c) {
371 for (
int i = 0; i < code.
length(); ++i) {
372 if (code(i) > code_range_) code_range_ = code(i);
379 void UnicharCompress::SetupDecoder() {
382 for (
int c = 0; c < encoder_.size(); ++c) {
385 is_valid_start_[code(0)] =
true;
387 int len = code.
length() - 1;
389 auto final_it = final_codes_.find(prefix);
390 if (final_it == final_codes_.end()) {
393 final_codes_[prefix] = code_list;
396 auto next_it = next_codes_.find(prefix);
397 if (next_it == next_codes_.end()) {
400 next_codes_[prefix] = code_list;
404 if (!next_it->second->contains(code(len)))
410 if (!final_it->second->contains(code(len)))
411 final_it->second->push_back(code(len));
417 void UnicharCompress::Cleanup() {
419 is_valid_start_.
clear();
420 for (
auto it = next_codes_.begin(); it != next_codes_.end(); ++it) {
423 for (
auto it = final_codes_.begin(); it != final_codes_.end(); ++it) {
427 final_codes_.clear();
static bool DecomposeHangul(int unicode, int *leading, int *vowel, int *trailing)
STRING GetEncodingAsString(const UNICHARSET &unicharset) const
static const int kFirstHangul
void add_str_int(const char *str, int number)
std::unordered_map< int, std::unique_ptr< std::vector< int > > > RSMap
UnicharCompress & operator=(const UnicharCompress &src)
static const int kMaxCodeLen
static string CleanupString(const char *utf8_str)
void SetupDirect(const GenericVector< RecodedCharID > &codes)
bool ComputeEncoding(const UNICHARSET &unicharset, int null_id, STRING *radical_stroke_table)
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
bool contains_unichar(const char *const unichar_repr) const
bool has_special_codes() const
void unichar_insert(const char *const unichar_repr, OldUncleanUnichars old_style)
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
static const int kNumHangul
void Set(int index, int value)
static std::vector< char32 > UTF8ToUTF32(const char *utf8_str)
void Truncate(int length)
std::unordered_map< int, int > RSCounts
bool Serialize(TFile *fp) const
void SetupPassThrough(const UNICHARSET &unicharset)
void Set3(int code0, int code1, int code2)
void init_to_size(int size, T t)
bool DeSerialize(TFile *fp)
void split(const char c, GenericVector< STRING > *splited)
int DecodeUnichar(const RecodedCharID &code) const
const char * id_to_unichar(UNICHAR_ID id) const