tesseract  5.0.0-alpha-619-ge9db
tesseract::Validator Class Referenceabstract

#include <validator.h>

Inheritance diagram for tesseract::Validator:
tesseract::ValidateGrapheme tesseract::ValidateIndic tesseract::ValidateJavanese tesseract::ValidateKhmer tesseract::ValidateMyanmar

Public Member Functions

virtual ~Validator ()
 

Static Public Member Functions

static bool ValidateCleanAndSegment (GraphemeNormMode g_mode, bool report_errors, const std::vector< char32 > &src, std::vector< std::vector< char32 >> *dest)
 
static bool IsZeroWidthMark (char32 ch)
 

Static Public Attributes

static const char32 kZeroWidthSpace = 0x200B
 
static const char32 kZeroWidthNonJoiner = 0x200C
 
static const char32 kZeroWidthJoiner = 0x200D
 
static const char32 kLeftToRightMark = 0x200E
 
static const char32 kRightToLeftMark = 0x200F
 
static const char32 kInvalid = 0xfffd
 

Protected Types

enum  CharClass {
  CharClass::kConsonant = 'C', CharClass::kVowel = 'V', CharClass::kVirama = 'H', CharClass::kMatra = 'M',
  CharClass::kMatraPiece = 'P', CharClass::kVowelModifier = 'D', CharClass::kZeroWidthNonJoiner = 'z', CharClass::kZeroWidthJoiner = 'Z',
  CharClass::kVedicMark = 'v', CharClass::kNukta = 'N', CharClass::kRobat = 'R', CharClass::kOther = 'O',
  CharClass::kWhitespace = ' ', CharClass::kCombiner = 'c'
}
 
using IndicPair = std::pair< CharClass, char32 >
 

Protected Member Functions

 Validator (ViramaScript script, bool report_errors)
 
bool ValidateCleanAndSegmentInternal (GraphemeNormMode g_mode, const std::vector< char32 > &src, std::vector< std::vector< char32 >> *dest)
 
void MoveResultsToDest (GraphemeNormMode g_mode, std::vector< std::vector< char32 >> *dest)
 
bool IsSubscriptScript () const
 
bool CodeOnlyToOutput ()
 
void MultiCodePart (unsigned length)
 
bool UseMultiCode (unsigned length)
 
virtual bool ConsumeGraphemeIfValid ()=0
 
void ComputeClassCodes (const std::vector< char32 > &text)
 
virtual CharClass UnicodeToCharClass (char32 ch) const =0
 
void Clear ()
 

Static Protected Member Functions

static std::unique_ptr< ValidatorScriptValidator (ViramaScript script, bool report_errors)
 
static ViramaScript MostFrequentViramaScript (const std::vector< char32 > &utf32)
 
static bool IsVirama (char32 unicode)
 
static bool IsVedicAccent (char32 unicode)
 

Protected Attributes

ViramaScript script_
 
std::vector< IndicPaircodes_
 
std::vector< std::vector< char32 > > parts_
 
std::vector< char32output_
 
unsigned codes_used_
 
unsigned output_used_
 
bool report_errors_
 

Static Protected Attributes

static const int kIndicCodePageSize = 128
 
static const char32 kMinIndicUnicode = 0x900
 
static const char32 kMaxSinhalaUnicode = 0xdff
 
static const char32 kMaxViramaScriptUnicode = 0x17ff
 
static const char32 kSinhalaVirama = 0xdca
 
static const char32 kMyanmarVirama = 0x1039
 
static const char32 kKhmerVirama = 0x17d2
 
static const char32 kJavaneseVirama = 0xa9c0
 
static const char32 kMaxJavaneseUnicode = 0xa9df
 

Detailed Description

Definition at line 86 of file validator.h.

Member Typedef Documentation

◆ IndicPair

using tesseract::Validator::IndicPair = std::pair<CharClass, char32>
protected

Definition at line 148 of file validator.h.

Member Enumeration Documentation

◆ CharClass

enum tesseract::Validator::CharClass
strongprotected
Enumerator
kConsonant 
kVowel 
kVirama 
kMatra 
kMatraPiece 
kVowelModifier 
kZeroWidthNonJoiner 
kZeroWidthJoiner 
kVedicMark 
kNukta 
kRobat 
kOther 
kWhitespace 
kCombiner 

Definition at line 126 of file validator.h.

136  : script_(script),
137  codes_used_(0),
138  output_used_(0),
139  report_errors_(report_errors) {}
140 
141  // Factory method that understands how to map script to the right subclass.
142  static std::unique_ptr<Validator> ScriptValidator(ViramaScript script,
143  bool report_errors);
144 
145  // Internal version of the public static ValidateCleanAndSegment.
146  // Validates and cleans the src vector of unicodes to the *dest, according to
147  // its type and the given g_mode.

Constructor & Destructor Documentation

◆ ~Validator()

tesseract::Validator::~Validator ( )
virtualdefault

◆ Validator()

tesseract::Validator::Validator ( ViramaScript  script,
bool  report_errors 
)
inlineprotected

Definition at line 150 of file validator.h.

Member Function Documentation

◆ Clear()

void tesseract::Validator::Clear ( )
protected

Definition at line 214 of file validator.cpp.

214  {
215  codes_.clear();
216  parts_.clear();
217  output_.clear();
218  codes_used_ = 0;
219  output_used_ = 0;
220 }

◆ CodeOnlyToOutput()

bool tesseract::Validator::CodeOnlyToOutput ( )
inlineprotected

Definition at line 186 of file validator.h.

186  {output_[output_used_]});
187  while (++output_used_ < output_.size()) {
188  parts_.back().push_back(output_[output_used_]);
189  }

◆ ComputeClassCodes()

void tesseract::Validator::ComputeClassCodes ( const std::vector< char32 > &  text)
protected

Definition at line 206 of file validator.cpp.

206  {
207  codes_.reserve(text.size());
208  for (char32 c : text) {
209  codes_.push_back(std::make_pair(UnicodeToCharClass(c), c));
210  }
211 }

◆ ConsumeGraphemeIfValid()

virtual bool tesseract::Validator::ConsumeGraphemeIfValid ( )
protectedpure virtual

◆ IsSubscriptScript()

bool tesseract::Validator::IsSubscriptScript ( ) const
protected

Definition at line 198 of file validator.cpp.

◆ IsVedicAccent()

bool tesseract::Validator::IsVedicAccent ( char32  unicode)
staticprotected

Definition at line 191 of file validator.cpp.

191  {
192  return (0x1cd0 <= unicode && unicode < 0x1d00) ||
193  (0xa8e0 <= unicode && unicode <= 0xa8f7) ||
194  (0x951 <= unicode && unicode <= 0x954);
195 }

◆ IsVirama()

bool tesseract::Validator::IsVirama ( char32  unicode)
staticprotected

Definition at line 180 of file validator.cpp.

180  {
181  return (kMinIndicUnicode <= unicode && unicode <= kMaxSinhalaUnicode &&
182  (unicode & 0x7f) == 0x4d) ||
183  unicode == kSinhalaVirama ||
184  unicode == kJavaneseVirama ||
185  unicode == kMyanmarVirama ||
186  unicode == kKhmerVirama;
187 }

◆ IsZeroWidthMark()

static bool tesseract::Validator::IsZeroWidthMark ( char32  ch)
inlinestatic

Definition at line 102 of file validator.h.

111  {

◆ MostFrequentViramaScript()

ViramaScript tesseract::Validator::MostFrequentViramaScript ( const std::vector< char32 > &  utf32)
staticprotected

Definition at line 143 of file validator.cpp.

144  {
145  std::unordered_map<int, int> histogram;
146  for (char32 ch : utf32) {
147  // Determine the codepage base. For the Indic scripts, Khmer and Javanese, it is
148  // sufficient to divide by kIndicCodePageSize but Myanmar is all over the
149  // unicode code space, so use its script id.
150  int base = ch / kIndicCodePageSize;
151  IcuErrorCode err;
152  UScriptCode script_code = uscript_getScript(ch, err);
153  if ((kMinIndicUnicode <= ch && ch <= kMaxJavaneseUnicode &&
154  script_code != USCRIPT_COMMON) ||
155  script_code == USCRIPT_MYANMAR) {
156  if (script_code == USCRIPT_MYANMAR)
157  base = static_cast<char32>(ViramaScript::kMyanmar) / kIndicCodePageSize;
158  ++histogram[base];
159  }
160  }
161  if (!histogram.empty()) {
162  int base =
163  std::max_element(histogram.begin(), histogram.end(), CmpPairSecond)
164  ->first;
165  char32 codebase = static_cast<char32>(base * kIndicCodePageSize);
166  // Check for validity.
167  if (codebase == static_cast<char32>(ViramaScript::kMyanmar) ||
168  codebase == static_cast<char32>(ViramaScript::kJavanese) ||
169  codebase == static_cast<char32>(ViramaScript::kKhmer) ||
170  (static_cast<char32>(ViramaScript::kDevanagari) <= codebase &&
171  codebase <= static_cast<char32>(ViramaScript::kSinhala))) {
172  return static_cast<ViramaScript>(codebase);
173  }
174  }
176 }

◆ MoveResultsToDest()

void tesseract::Validator::MoveResultsToDest ( GraphemeNormMode  g_mode,
std::vector< std::vector< char32 >> *  dest 
)
protected

Definition at line 114 of file validator.cpp.

115  {
117  // Append each element of the combined output_ that we made as a new vector
118  // in dest.
119  dest->reserve(dest->size() + output_.size());
120  for (char32 ch : output_) dest->push_back({ch});
121  } else if (g_mode == GraphemeNormMode::kGlyphSplit) {
122  // Append all the parts_ that we made onto dest.
123  std::move(parts_.begin(), parts_.end(), std::back_inserter(*dest));
124  } else if (g_mode == GraphemeNormMode::kCombined || dest->empty()) {
125  // Append the combined output_ that we made onto dest as one new vector.
126  dest->push_back(std::vector<char32>());
127  output_.swap(dest->back());
128  } else { // kNone.
129  // Append the combined output_ that we made onto the last existing element
130  // of dest.
131  dest->back().insert(dest->back().end(), output_.begin(), output_.end());
132  }
133 }

◆ MultiCodePart()

void tesseract::Validator::MultiCodePart ( unsigned  length)
inlineprotected

Definition at line 196 of file validator.h.

◆ ScriptValidator()

std::unique_ptr< Validator > tesseract::Validator::ScriptValidator ( ViramaScript  script,
bool  report_errors 
)
staticprotected

Definition at line 71 of file validator.cpp.

72  {
73  switch (script) {
75  return std::unique_ptr<Validator>(
76  new ValidateGrapheme(script, report_errors));
78  return std::unique_ptr<Validator>(
79  new ValidateJavanese(script, report_errors));
81  return std::unique_ptr<Validator>(
82  new ValidateMyanmar(script, report_errors));
84  return std::unique_ptr<Validator>(
85  new ValidateKhmer(script, report_errors));
86  default:
87  return std::unique_ptr<Validator>(
88  new ValidateIndic(script, report_errors));
89  }
90 }

◆ UnicodeToCharClass()

virtual CharClass tesseract::Validator::UnicodeToCharClass ( char32  ch) const
protectedpure virtual

◆ UseMultiCode()

bool tesseract::Validator::UseMultiCode ( unsigned  length)
inlineprotected

Definition at line 210 of file validator.h.

◆ ValidateCleanAndSegment()

bool tesseract::Validator::ValidateCleanAndSegment ( GraphemeNormMode  g_mode,
bool  report_errors,
const std::vector< char32 > &  src,
std::vector< std::vector< char32 >> *  dest 
)
static

Definition at line 40 of file validator.cpp.

42  {
43  ValidateGrapheme g_validator(ViramaScript::kNonVirama, report_errors);
44  std::vector<std::vector<char32>> graphemes;
46  bool success = true;
47  if (script == ViramaScript::kNonVirama) {
48  // The grapheme segmenter's maximum segmentation is the grapheme unit, so
49  // up the mode by 1 to get the desired effect.
50  if (g_mode == GraphemeNormMode::kCombined)
52  else if (g_mode == GraphemeNormMode::kGlyphSplit)
54  // Just do grapheme segmentation.
55  success = g_validator.ValidateCleanAndSegmentInternal(g_mode, src, dest);
56  } else {
57  success = g_validator.ValidateCleanAndSegmentInternal(
58  GraphemeNormMode::kGlyphSplit, src, &graphemes);
59  std::unique_ptr<Validator> validator(
60  ScriptValidator(script, report_errors));
61  for (const auto& grapheme : graphemes) {
62  if (!validator->ValidateCleanAndSegmentInternal(g_mode, grapheme, dest)) {
63  success = false;
64  }
65  }
66  }
67  return success;
68 }

◆ ValidateCleanAndSegmentInternal()

bool tesseract::Validator::ValidateCleanAndSegmentInternal ( GraphemeNormMode  g_mode,
const std::vector< char32 > &  src,
std::vector< std::vector< char32 >> *  dest 
)
protected

Definition at line 97 of file validator.cpp.

99  {
100  Clear();
101  ComputeClassCodes(src);
102  bool success = true;
103  for (codes_used_ = 0; codes_used_ < codes_.size();) {
104  if (!ConsumeGraphemeIfValid()) {
105  success = false;
106  ++codes_used_;
107  }
108  }
109  MoveResultsToDest(g_mode, dest);
110  return success;
111 }

Member Data Documentation

◆ codes_

std::vector<IndicPair> tesseract::Validator::codes_
protected

Definition at line 246 of file validator.h.

◆ codes_used_

unsigned tesseract::Validator::codes_used_
protected

Definition at line 252 of file validator.h.

◆ kIndicCodePageSize

const int tesseract::Validator::kIndicCodePageSize = 128
staticprotected

Definition at line 228 of file validator.h.

◆ kInvalid

const char32 tesseract::Validator::kInvalid = 0xfffd
static

Definition at line 114 of file validator.h.

◆ kJavaneseVirama

const char32 tesseract::Validator::kJavaneseVirama = 0xa9c0
staticprotected

Definition at line 240 of file validator.h.

◆ kKhmerVirama

const char32 tesseract::Validator::kKhmerVirama = 0x17d2
staticprotected

Definition at line 238 of file validator.h.

◆ kLeftToRightMark

const char32 tesseract::Validator::kLeftToRightMark = 0x200E
static

Definition at line 112 of file validator.h.

◆ kMaxJavaneseUnicode

const char32 tesseract::Validator::kMaxJavaneseUnicode = 0xa9df
staticprotected

Definition at line 241 of file validator.h.

◆ kMaxSinhalaUnicode

const char32 tesseract::Validator::kMaxSinhalaUnicode = 0xdff
staticprotected

Definition at line 232 of file validator.h.

◆ kMaxViramaScriptUnicode

const char32 tesseract::Validator::kMaxViramaScriptUnicode = 0x17ff
staticprotected

Definition at line 234 of file validator.h.

◆ kMinIndicUnicode

const char32 tesseract::Validator::kMinIndicUnicode = 0x900
staticprotected

Definition at line 230 of file validator.h.

◆ kMyanmarVirama

const char32 tesseract::Validator::kMyanmarVirama = 0x1039
staticprotected

Definition at line 237 of file validator.h.

◆ kRightToLeftMark

const char32 tesseract::Validator::kRightToLeftMark = 0x200F
static

Definition at line 113 of file validator.h.

◆ kSinhalaVirama

const char32 tesseract::Validator::kSinhalaVirama = 0xdca
staticprotected

Definition at line 236 of file validator.h.

◆ kZeroWidthJoiner

const char32 tesseract::Validator::kZeroWidthJoiner = 0x200D
static

Definition at line 111 of file validator.h.

◆ kZeroWidthNonJoiner

const char32 tesseract::Validator::kZeroWidthNonJoiner = 0x200C
static

Definition at line 110 of file validator.h.

◆ kZeroWidthSpace

const char32 tesseract::Validator::kZeroWidthSpace = 0x200B
static

Definition at line 109 of file validator.h.

◆ output_

std::vector<char32> tesseract::Validator::output_
protected

Definition at line 250 of file validator.h.

◆ output_used_

unsigned tesseract::Validator::output_used_
protected

Definition at line 254 of file validator.h.

◆ parts_

std::vector<std::vector<char32> > tesseract::Validator::parts_
protected

Definition at line 248 of file validator.h.

◆ report_errors_

bool tesseract::Validator::report_errors_
protected

Definition at line 256 of file validator.h.

◆ script_

ViramaScript tesseract::Validator::script_
protected

Definition at line 244 of file validator.h.


The documentation for this class was generated from the following files:
tesseract::Validator::MostFrequentViramaScript
static ViramaScript MostFrequentViramaScript(const std::vector< char32 > &utf32)
Definition: validator.cpp:143
tesseract::Validator::kSinhalaVirama
static const char32 kSinhalaVirama
Definition: validator.h:236
tesseract::Validator::MoveResultsToDest
void MoveResultsToDest(GraphemeNormMode g_mode, std::vector< std::vector< char32 >> *dest)
Definition: validator.cpp:114
tesseract::Validator::kMaxSinhalaUnicode
static const char32 kMaxSinhalaUnicode
Definition: validator.h:232
tesseract::ViramaScript
ViramaScript
Definition: validator.h:67
tesseract::Validator::codes_
std::vector< IndicPair > codes_
Definition: validator.h:246
tesseract::ViramaScript::kTelugu
tesseract::ViramaScript::kJavanese
tesseract::Validator::kIndicCodePageSize
static const int kIndicCodePageSize
Definition: validator.h:228
tesseract::ViramaScript::kNonVirama
tesseract::Validator::kMyanmarVirama
static const char32 kMyanmarVirama
Definition: validator.h:237
tesseract::ViramaScript::kKhmer
tesseract::ViramaScript::kMyanmar
tesseract::Validator::ConsumeGraphemeIfValid
virtual bool ConsumeGraphemeIfValid()=0
tesseract::Validator::output_used_
unsigned output_used_
Definition: validator.h:254
tesseract::GraphemeNormMode::kGlyphSplit
tesseract::Validator::output_
std::vector< char32 > output_
Definition: validator.h:250
tesseract::Validator::kMinIndicUnicode
static const char32 kMinIndicUnicode
Definition: validator.h:230
tesseract::Validator::ScriptValidator
static std::unique_ptr< Validator > ScriptValidator(ViramaScript script, bool report_errors)
Definition: validator.cpp:71
tesseract::ViramaScript::kDevanagari
tesseract::ViramaScript::kKannada
tesseract::Validator::report_errors_
bool report_errors_
Definition: validator.h:256
tesseract::Validator::codes_used_
unsigned codes_used_
Definition: validator.h:252
tesseract::Validator::Clear
void Clear()
Definition: validator.cpp:214
tesseract::Validator::UnicodeToCharClass
virtual CharClass UnicodeToCharClass(char32 ch) const =0
tesseract::Validator::ComputeClassCodes
void ComputeClassCodes(const std::vector< char32 > &text)
Definition: validator.cpp:206
tesstrain_utils.dest
dest
Definition: tesstrain_utils.py:139
tesseract::Validator::kKhmerVirama
static const char32 kKhmerVirama
Definition: validator.h:238
char32
signed int char32
Definition: pango_font_info.h:33
tesseract::Validator::kJavaneseVirama
static const char32 kJavaneseVirama
Definition: validator.h:240
tesseract::GraphemeNormMode::kIndividualUnicodes
tesseract::GraphemeNormMode::kCombined
tesseract::ViramaScript::kSinhala
tesseract::Validator::kMaxJavaneseUnicode
static const char32 kMaxJavaneseUnicode
Definition: validator.h:241
tesseract::Validator::script_
ViramaScript script_
Definition: validator.h:244
tesseract::Validator::parts_
std::vector< std::vector< char32 > > parts_
Definition: validator.h:248