2017-08-04 06:51:18 +08:00
|
|
|
// (C) Copyright 2017, Google Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2019-01-19 13:13:03 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
#include <allheaders.h>
|
2019-01-19 13:13:03 +08:00
|
|
|
|
|
|
|
#include "include_gunit.h"
|
2021-03-13 05:06:34 +08:00
|
|
|
#include "log.h" // for LOG
|
2020-12-29 18:28:50 +08:00
|
|
|
#include "serialis.h"
|
2019-01-19 13:13:03 +08:00
|
|
|
#include "tprintf.h"
|
|
|
|
#include "unicharcompress.h"
|
2017-08-04 06:51:18 +08:00
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
|
|
|
|
class UnicharcompressTest : public ::testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
2021-03-22 15:26:05 +08:00
|
|
|
void SetUp() override {
|
2019-05-17 00:12:06 +08:00
|
|
|
std::locale::global(std::locale(""));
|
2020-12-31 01:17:58 +08:00
|
|
|
file::MakeTmpdir();
|
2019-05-17 00:12:06 +08:00
|
|
|
}
|
|
|
|
|
2017-08-04 06:51:18 +08:00
|
|
|
// Loads and compresses the given unicharset.
|
2021-03-13 05:06:34 +08:00
|
|
|
void LoadUnicharset(const std::string &unicharset_name) {
|
|
|
|
std::string radical_stroke_file = file::JoinPath(LANGDATA_DIR, "radical-stroke.txt");
|
|
|
|
std::string unicharset_file = file::JoinPath(TESTDATA_DIR, unicharset_name);
|
2019-01-19 13:13:03 +08:00
|
|
|
std::string radical_data;
|
2021-03-13 05:06:34 +08:00
|
|
|
CHECK_OK(file::GetContents(radical_stroke_file, &radical_data, file::Defaults()));
|
2019-08-12 19:07:15 +08:00
|
|
|
CHECK(unicharset_.load_from_file(unicharset_file.c_str()));
|
2021-03-15 05:52:52 +08:00
|
|
|
std::string radical_str(radical_data.c_str());
|
2021-03-13 05:06:34 +08:00
|
|
|
null_char_ = unicharset_.has_special_codes() ? UNICHAR_BROKEN : unicharset_.size();
|
2017-08-04 06:51:18 +08:00
|
|
|
compressed_.ComputeEncoding(unicharset_, null_char_, &radical_str);
|
|
|
|
// Get the encoding of the null char.
|
|
|
|
RecodedCharID code;
|
|
|
|
compressed_.EncodeUnichar(null_char_, &code);
|
|
|
|
encoded_null_char_ = code(0);
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string output_name =
|
2021-08-06 17:38:05 +08:00
|
|
|
file::JoinPath(FLAGS_test_tmpdir, unicharset_name) + ".encoding.txt";
|
2021-03-15 05:52:52 +08:00
|
|
|
std::string encoding = compressed_.GetEncodingAsString(unicharset_);
|
2019-01-19 13:13:03 +08:00
|
|
|
std::string encoding_str(&encoding[0], encoding.size());
|
2017-08-04 06:51:18 +08:00
|
|
|
CHECK_OK(file::SetContents(output_name, encoding_str, file::Defaults()));
|
|
|
|
LOG(INFO) << "Wrote encoding to:" << output_name;
|
|
|
|
}
|
|
|
|
// Serializes and de-serializes compressed_ over itself.
|
|
|
|
void SerializeAndUndo() {
|
2020-12-29 20:02:36 +08:00
|
|
|
std::vector<char> data;
|
2017-08-04 06:51:18 +08:00
|
|
|
TFile wfp;
|
|
|
|
wfp.OpenWrite(&data);
|
|
|
|
EXPECT_TRUE(compressed_.Serialize(&wfp));
|
|
|
|
TFile rfp;
|
|
|
|
rfp.Open(&data[0], data.size());
|
|
|
|
EXPECT_TRUE(compressed_.DeSerialize(&rfp));
|
|
|
|
}
|
|
|
|
// Returns true if the lang is in CJK.
|
2021-03-13 05:06:34 +08:00
|
|
|
bool IsCJKLang(const std::string &lang) {
|
|
|
|
return lang == "chi_sim" || lang == "chi_tra" || lang == "kor" || lang == "jpn";
|
2017-08-04 06:51:18 +08:00
|
|
|
}
|
|
|
|
// Returns true if the lang is Indic.
|
2021-03-13 05:06:34 +08:00
|
|
|
bool IsIndicLang(const std::string &lang) {
|
|
|
|
return lang == "asm" || lang == "ben" || lang == "bih" || lang == "hin" || lang == "mar" ||
|
|
|
|
lang == "nep" || lang == "san" || lang == "bod" || lang == "dzo" || lang == "guj" ||
|
|
|
|
lang == "kan" || lang == "mal" || lang == "ori" || lang == "pan" || lang == "sin" ||
|
|
|
|
lang == "tam" || lang == "tel";
|
2017-08-04 06:51:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expects the appropriate results from the compressed_ unicharset_.
|
2021-03-13 05:06:34 +08:00
|
|
|
void ExpectCorrect(const std::string &lang) {
|
2017-08-04 06:51:18 +08:00
|
|
|
// Count the number of times each code is used in each element of
|
|
|
|
// RecodedCharID.
|
|
|
|
RecodedCharID zeros;
|
2021-03-22 15:48:50 +08:00
|
|
|
for (int i = 0; i < RecodedCharID::kMaxCodeLen; ++i) {
|
2021-03-13 05:06:34 +08:00
|
|
|
zeros.Set(i, 0);
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2017-08-04 06:51:18 +08:00
|
|
|
int code_range = compressed_.code_range();
|
2021-03-13 05:06:34 +08:00
|
|
|
std::vector<RecodedCharID> times_seen(code_range, zeros);
|
2017-08-04 06:51:18 +08:00
|
|
|
for (int u = 0; u <= unicharset_.size(); ++u) {
|
|
|
|
if (u != UNICHAR_SPACE && u != null_char_ &&
|
2021-03-13 05:06:34 +08:00
|
|
|
(u == unicharset_.size() ||
|
|
|
|
(unicharset_.has_special_codes() && u < SPECIAL_UNICHAR_CODES_COUNT))) {
|
|
|
|
continue; // Not used so not encoded.
|
2017-08-04 06:51:18 +08:00
|
|
|
}
|
|
|
|
RecodedCharID code;
|
|
|
|
int len = compressed_.EncodeUnichar(u, &code);
|
|
|
|
// Check round-trip encoding.
|
|
|
|
int unichar_id;
|
2021-03-15 16:01:55 +08:00
|
|
|
std::vector<UNICHAR_ID> normed_ids;
|
2017-08-04 06:51:18 +08:00
|
|
|
if (u == null_char_ || u == unicharset_.size()) {
|
|
|
|
unichar_id = null_char_;
|
|
|
|
} else {
|
|
|
|
unichar_id = u;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(unichar_id, compressed_.DecodeUnichar(code));
|
|
|
|
// Check that the codes are valid.
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
int code_val = code(i);
|
|
|
|
EXPECT_GE(code_val, 0);
|
|
|
|
EXPECT_LT(code_val, code_range);
|
|
|
|
times_seen[code_val].Set(i, times_seen[code_val](i) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that each code is used in at least one position.
|
|
|
|
for (int c = 0; c < code_range; ++c) {
|
|
|
|
int num_used = 0;
|
|
|
|
for (int i = 0; i < RecodedCharID::kMaxCodeLen; ++i) {
|
2021-03-22 15:48:50 +08:00
|
|
|
if (times_seen[c](i) != 0) {
|
2021-03-13 05:06:34 +08:00
|
|
|
++num_used;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2017-08-04 06:51:18 +08:00
|
|
|
}
|
|
|
|
EXPECT_GE(num_used, 1) << "c=" << c << "/" << code_range;
|
|
|
|
}
|
|
|
|
// Check that GetNextCodes/GetFinalCodes lists match the times_seen,
|
|
|
|
// and create valid codes.
|
|
|
|
RecodedCharID code;
|
|
|
|
CheckCodeExtensions(code, times_seen);
|
|
|
|
// Finally, we achieved all that using a codebook < 10% of the size of
|
|
|
|
// the original unicharset, for CK or Indic, and 20% with J, but just
|
|
|
|
// no bigger for all others.
|
|
|
|
if (IsCJKLang(lang) || IsIndicLang(lang)) {
|
|
|
|
EXPECT_LT(code_range, unicharset_.size() / (lang == "jpn" ? 5 : 10));
|
|
|
|
} else {
|
|
|
|
EXPECT_LE(code_range, unicharset_.size() + 1);
|
|
|
|
}
|
2021-03-13 05:06:34 +08:00
|
|
|
LOG(INFO) << "Compressed unicharset of " << unicharset_.size() << " to " << code_range;
|
2017-08-04 06:51:18 +08:00
|
|
|
}
|
|
|
|
// Checks for extensions of the current code that either finish a code, or
|
|
|
|
// extend it and checks those extensions recursively.
|
2021-03-13 05:06:34 +08:00
|
|
|
void CheckCodeExtensions(const RecodedCharID &code,
|
|
|
|
const std::vector<RecodedCharID> ×_seen) {
|
2017-08-04 06:51:18 +08:00
|
|
|
RecodedCharID extended = code;
|
|
|
|
int length = code.length();
|
2021-03-15 16:01:55 +08:00
|
|
|
const std::vector<int> *final_codes = compressed_.GetFinalCodes(code);
|
2018-04-22 23:15:22 +08:00
|
|
|
if (final_codes != nullptr) {
|
2021-03-22 15:32:09 +08:00
|
|
|
for (int ending : *final_codes) {
|
2017-08-04 06:51:18 +08:00
|
|
|
EXPECT_GT(times_seen[ending](length), 0);
|
|
|
|
extended.Set(length, ending);
|
|
|
|
int unichar_id = compressed_.DecodeUnichar(extended);
|
|
|
|
EXPECT_NE(INVALID_UNICHAR_ID, unichar_id);
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 16:01:55 +08:00
|
|
|
const std::vector<int> *next_codes = compressed_.GetNextCodes(code);
|
2018-04-22 23:15:22 +08:00
|
|
|
if (next_codes != nullptr) {
|
2021-03-22 15:32:09 +08:00
|
|
|
for (int extension : *next_codes) {
|
2017-08-04 06:51:18 +08:00
|
|
|
EXPECT_GT(times_seen[extension](length), 0);
|
|
|
|
extended.Set(length, extension);
|
|
|
|
CheckCodeExtensions(extended, times_seen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicharCompress compressed_;
|
|
|
|
UNICHARSET unicharset_;
|
|
|
|
int null_char_;
|
|
|
|
// The encoding of the null_char_.
|
|
|
|
int encoded_null_char_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(UnicharcompressTest, DoesChinese) {
|
|
|
|
LOG(INFO) << "Testing chi_tra";
|
|
|
|
LoadUnicharset("chi_tra.unicharset");
|
|
|
|
ExpectCorrect("chi_tra");
|
|
|
|
LOG(INFO) << "Testing chi_sim";
|
|
|
|
LoadUnicharset("chi_sim.unicharset");
|
|
|
|
ExpectCorrect("chi_sim");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UnicharcompressTest, DoesJapanese) {
|
|
|
|
LOG(INFO) << "Testing jpn";
|
|
|
|
LoadUnicharset("jpn.unicharset");
|
|
|
|
ExpectCorrect("jpn");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UnicharcompressTest, DoesKorean) {
|
|
|
|
LOG(INFO) << "Testing kor";
|
|
|
|
LoadUnicharset("kor.unicharset");
|
|
|
|
ExpectCorrect("kor");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UnicharcompressTest, DoesKannada) {
|
|
|
|
LOG(INFO) << "Testing kan";
|
|
|
|
LoadUnicharset("kan.unicharset");
|
|
|
|
ExpectCorrect("kan");
|
|
|
|
SerializeAndUndo();
|
|
|
|
ExpectCorrect("kan");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UnicharcompressTest, DoesMarathi) {
|
|
|
|
LOG(INFO) << "Testing mar";
|
|
|
|
LoadUnicharset("mar.unicharset");
|
|
|
|
ExpectCorrect("mar");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UnicharcompressTest, DoesEnglish) {
|
|
|
|
LOG(INFO) << "Testing eng";
|
|
|
|
LoadUnicharset("eng.unicharset");
|
|
|
|
ExpectCorrect("eng");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a unicharset that contains double-letter ligatures (eg ff) has
|
|
|
|
// no null char in the encoding at all.
|
|
|
|
TEST_F(UnicharcompressTest, DoesLigaturesWithDoubles) {
|
|
|
|
LOG(INFO) << "Testing por with ligatures";
|
|
|
|
LoadUnicharset("por.unicharset");
|
|
|
|
ExpectCorrect("por");
|
|
|
|
// Check that any unichar-id that is encoded with multiple codes has the
|
2024-02-20 23:45:32 +08:00
|
|
|
// correct encoded_null_char_ in between.
|
2017-08-04 06:51:18 +08:00
|
|
|
for (int u = 0; u <= unicharset_.size(); ++u) {
|
|
|
|
RecodedCharID code;
|
|
|
|
int len = compressed_.EncodeUnichar(u, &code);
|
|
|
|
if (len > 1) {
|
|
|
|
// The should not be any null char in the code.
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
EXPECT_NE(encoded_null_char_, code(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that GetEncodingAsString returns the right result for a trivial
|
|
|
|
// unicharset.
|
|
|
|
TEST_F(UnicharcompressTest, GetEncodingAsString) {
|
|
|
|
LoadUnicharset("trivial.unicharset");
|
|
|
|
ExpectCorrect("trivial");
|
2021-03-15 05:52:52 +08:00
|
|
|
std::string encoding = compressed_.GetEncodingAsString(unicharset_);
|
2019-01-19 13:13:03 +08:00
|
|
|
std::string encoding_str(&encoding[0], encoding.length());
|
2021-08-07 01:45:22 +08:00
|
|
|
std::vector<std::string> lines = split(encoding_str, '\n');
|
2017-08-04 06:51:18 +08:00
|
|
|
EXPECT_EQ(5, lines.size());
|
|
|
|
// The first line is always space.
|
|
|
|
EXPECT_EQ("0\t ", lines[0]);
|
|
|
|
// Next we have i.
|
|
|
|
EXPECT_EQ("1\ti", lines[1]);
|
|
|
|
// Next we have f.
|
|
|
|
EXPECT_EQ("2\tf", lines[2]);
|
|
|
|
// Next we have the fi ligature: fi. There are no nulls in it, as there are no
|
|
|
|
// repeated letter ligatures in this unicharset, unlike por.unicharset above.
|
|
|
|
EXPECT_EQ("2,1\tfi", lines[3]);
|
|
|
|
// Finally the null character.
|
|
|
|
EXPECT_EQ("3\t<nul>", lines[4]);
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|