2019-01-22 04:32:05 +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.
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2019-01-22 04:32:05 +08:00
|
|
|
#include "include_gunit.h"
|
|
|
|
#include "normstrngs.h"
|
|
|
|
#include "normstrngs_test.h"
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, MultipleSyllablesAreNotASingleGrapheme) {
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string str = "\u0c15\u0c3f\u0c15\u0c0e"; // KA - dep I - KA - ind E.
|
2019-01-22 04:32:05 +08:00
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
// It made 3 graphemes.
|
|
|
|
EXPECT_EQ(glyphs.size(), 3);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[0], std::string("\u0c15\u0c3f"));
|
|
|
|
EXPECT_EQ(glyphs[1], std::string("\u0c15"));
|
|
|
|
EXPECT_EQ(glyphs[2], std::string("\u0c0e"));
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, SingleConsonantOK) {
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string str = "\u0cb9"; // HA
|
2019-01-22 04:32:05 +08:00
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 1);
|
|
|
|
EXPECT_EQ(glyphs[0], str);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, SimpleCV) {
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string str = "\u0cb9\u0cbf"; // HA I
|
2019-01-22 04:32:05 +08:00
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 1);
|
|
|
|
EXPECT_EQ(glyphs[0], str);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, SubscriptConjunct) {
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string str = "\u0cb9\u0ccd\u0c95\u0cbf"; // HA Virama KA I
|
2019-01-22 04:32:05 +08:00
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 1);
|
|
|
|
EXPECT_EQ(glyphs[0], str);
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kGlyphSplit, true, str.c_str(),
|
|
|
|
&glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 3);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[1], std::string("\u0ccd\u0c95"));
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, HalfFormJoiner) {
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string str = "\u0d15\u0d4d\u200d\u0d24"; // KA Virama ZWJ Ta
|
2019-01-22 04:32:05 +08:00
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 1);
|
|
|
|
EXPECT_EQ(glyphs[0], str);
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kGlyphSplit, true, str.c_str(),
|
|
|
|
&glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 2) << PrintStringVectorWithUnicodes(glyphs);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[0], std::string("\u0d15\u0d4d\u200d"));
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, TraditionalConjunctJoiner) {
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string str = "\u0d15\u200d\u0d4d\u0d24"; // KA ZWI Virama Ta
|
2019-01-22 04:32:05 +08:00
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 1);
|
|
|
|
EXPECT_EQ(glyphs[0], str);
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kGlyphSplit, true, str.c_str(),
|
|
|
|
&glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 3);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[1], std::string("\u200d\u0d4d"));
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, OpenConjunctNonJoiner) {
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string str = "\u0d15\u200c\u0d4d\u0d24"; // KA ZWNJ Virama Ta
|
2019-01-22 04:32:05 +08:00
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 1);
|
|
|
|
EXPECT_EQ(glyphs[0], str);
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kGlyphSplit, true, str.c_str(),
|
|
|
|
&glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 3);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[1], std::string("\u200c\u0d4d"));
|
2018-08-24 21:07:48 +08:00
|
|
|
// Malaylam only, so not allowed in Telugu.
|
2021-03-13 05:06:34 +08:00
|
|
|
str = "\u0c15\u200c\u0c4d\u0c24"; // KA ZWNJ Virama Ta
|
|
|
|
EXPECT_FALSE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(),
|
|
|
|
&glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, ExplicitViramaNonJoiner) {
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string str = "\u0d15\u0d4d\u200c\u0d24"; // KA Virama ZWNJ Ta
|
2019-01-22 04:32:05 +08:00
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 2);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[1], std::string("\u0d24"));
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kGlyphSplit, true, str.c_str(),
|
|
|
|
&glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 3);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[1], std::string("\u0d4d\u200c"));
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, ThaiGraphemes) {
|
|
|
|
// This is a single grapheme unless in glyph split mode
|
2019-01-22 04:32:05 +08:00
|
|
|
std::string str = "\u0e14\u0e38\u0e4a";
|
|
|
|
std::vector<std::string> glyphs;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 1);
|
|
|
|
EXPECT_EQ(glyphs[0], str);
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kGlyphSplit, true, str.c_str(),
|
|
|
|
&glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 3);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[0], std::string("\u0e14"));
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ValidateGraphemeTest, NoLonelyJoinersQuote) {
|
2019-01-22 04:32:05 +08:00
|
|
|
std::string str = "'\u0d24\u0d23\u0d32\u0d4d'\u200d";
|
|
|
|
std::vector<std::string> glyphs;
|
2018-08-24 21:07:48 +08:00
|
|
|
// Returns true, but the joiner is gone.
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
|
|
|
|
GraphemeNormMode::kCombined, true, str.c_str(), &glyphs))
|
2018-08-24 21:07:48 +08:00
|
|
|
<< PrintString32WithUnicodes(str);
|
|
|
|
EXPECT_EQ(glyphs.size(), 5);
|
2019-01-22 04:32:05 +08:00
|
|
|
EXPECT_EQ(glyphs[0], std::string("'"));
|
|
|
|
EXPECT_EQ(glyphs[1], std::string("\u0d24"));
|
|
|
|
EXPECT_EQ(glyphs[2], std::string("\u0d23"));
|
|
|
|
EXPECT_EQ(glyphs[3], std::string("\u0d32\u0d4d\u200c"));
|
|
|
|
EXPECT_EQ(glyphs[4], std::string("'"));
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|