tesseract v5.3.3.20231005
unicharset_extractor.cpp
Go to the documentation of this file.
1
2// File: unicharset_extractor.cpp
3// Description: Unicode character/ligature set extractor.
4// Author: Thomas Kielbus
5//
6// (C) Copyright 2006, Google Inc.
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License at
10// http://www.apache.org/licenses/LICENSE-2.0
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
18
19// Given a list of box files or text files on the command line, this program
20// normalizes the text according to command-line options and generates
21// a unicharset.
22
23#include <cstdlib>
24#include <filesystem>
25#include "boxread.h"
26#include "commandlineflags.h"
27#include "commontraining.h" // CheckSharedLibraryVersion
28#include "lang_model_helpers.h"
29#include "normstrngs.h"
30#include "unicharset.h"
32
33using namespace tesseract;
34
35static STRING_PARAM_FLAG(output_unicharset, "unicharset", "Output file path");
36static INT_PARAM_FLAG(norm_mode, 1,
37 "Normalization mode: 1=Combine graphemes, "
38 "2=Split graphemes, 3=Pure unicode");
39
40namespace tesseract {
41
42// Helper normalizes and segments the given strings according to norm_mode, and
43// adds the segmented parts to unicharset.
44static void AddStringsToUnicharset(const std::vector<std::string> &strings, int norm_mode,
45 UNICHARSET *unicharset) {
46 for (const auto &string : strings) {
47 std::vector<std::string> normalized;
49 static_cast<GraphemeNormMode>(norm_mode),
50 /*report_errors*/ true, string.c_str(), &normalized)) {
51 for (const std::string &normed : normalized) {
52 // normed is a UTF-8 encoded string
53 if (normed.empty() || IsUTF8Whitespace(normed.c_str())) {
54 continue;
55 }
56 unicharset->unichar_insert(normed.c_str());
57 }
58 } else {
59 tprintf("Normalization failed for string '%s'\n", string.c_str());
60 }
61 }
62}
63
64static int Main(int argc, char **argv) {
65 UNICHARSET unicharset;
66 // Load input files
67 for (int arg = 1; arg < argc; ++arg) {
68 std::filesystem::path filePath = argv[arg];
69 std::string file_data = tesseract::ReadFile(argv[arg]);
70 if (file_data.empty()) {
71 continue;
72 }
73 std::vector<std::string> texts;
74 if (filePath.extension() == ".box") {
75 tprintf("Extracting unicharset from box file %s\n", argv[arg]);
76 bool res = ReadMemBoxes(-1, /*skip_blanks*/ true, &file_data[0],
77 /*continue_on_failure*/ false, /*boxes*/ nullptr, &texts,
78 /*box_texts*/ nullptr, /*pages*/ nullptr);
79 if (!res) {
80 tprintf("Cannot read box data from '%s'\n", argv[arg]);
81 return EXIT_FAILURE;
82 }
83 } else {
84 tprintf("Extracting unicharset from plain text file %s\n", argv[arg]);
85 texts.clear();
86 texts = split(file_data, '\n');
87 }
88 AddStringsToUnicharset(texts, FLAGS_norm_mode, &unicharset);
89 }
90 SetupBasicProperties(/*report_errors*/ true, /*decompose*/ false, &unicharset);
91 // Write unicharset file.
92 if (unicharset.save_to_file(FLAGS_output_unicharset.c_str())) {
93 tprintf("Wrote unicharset file %s\n", FLAGS_output_unicharset.c_str());
94 } else {
95 tprintf("Cannot save unicharset file %s\n", FLAGS_output_unicharset.c_str());
96 return EXIT_FAILURE;
97 }
98 return EXIT_SUCCESS;
99}
100
101} // namespace tesseract
102
103int main(int argc, char **argv) {
104 tesseract::CheckSharedLibraryVersion();
105 if (argc > 1) {
106 tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);
107 }
108 if (argc < 2) {
109 tprintf(
110 "Usage: %s [--output_unicharset filename] [--norm_mode mode]"
111 " box_or_text_file [...]\n",
112 argv[0]);
113 tprintf("Where mode means:\n");
114 tprintf(" 1=combine graphemes (use for Latin and other simple scripts)\n");
115 tprintf(" 2=split graphemes (use for Indic/Khmer/Myanmar)\n");
116 tprintf(" 3=pure unicode (use for Arabic/Hebrew/Thai/Tibetan)\n");
117 tprintf("Reads box or plain text files to extract the unicharset.\n");
118 return EXIT_FAILURE;
119 }
120 return tesseract::Main(argc, argv);
121}
#define INT_PARAM_FLAG(name, val, comment)
#define STRING_PARAM_FLAG(name, val, comment)
int main(int argc, char **argv)
GraphemeNormMode
Definition: validator.h:36
bool ReadMemBoxes(int target_page, bool skip_blanks, const char *box_data, bool continue_on_failure, std::vector< TBOX > *boxes, std::vector< std::string > *texts, std::vector< std::string > *box_texts, std::vector< int > *pages)
Definition: boxread.cpp:97
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void SetupBasicProperties(bool report_errors, bool decompose, UNICHARSET *unicharset)
bool NormalizeCleanAndSegmentUTF8(UnicodeNormMode u_mode, OCRNorm ocr_normalize, GraphemeNormMode g_mode, bool report_errors, const char *str8, std::vector< std::string > *graphemes)
Definition: normstrngs.cpp:179
std::string ReadFile(const std::string &filename, FileReader reader)
const std::vector< std::string > split(const std::string &s, char c)
Definition: helpers.h:43
bool IsUTF8Whitespace(const char *text)
Definition: normstrngs.cpp:233
void unichar_insert(const char *const unichar_repr, OldUncleanUnichars old_style)
Definition: unicharset.cpp:654
bool save_to_file(const char *const filename) const
Definition: unicharset.h:361