tesseract v5.3.3.20231005
ambiguous_words.cpp
Go to the documentation of this file.
1
2// File: ambiguous_words.cpp
3// Description: A program that takes a text file with a list of words as
4// input (one per line) and outputs a file with the words
5// that were found in the dictionary followed by the words
6// that are ambiguous to them.
7// Author: Rika Antonova
8//
9// (C) Copyright 2011, Google Inc.
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13// http://www.apache.org/licenses/LICENSE-2.0
14// Unless required by applicable law or agreed to in writing, software
15// distributed under the License is distributed on an "AS IS" BASIS,
16// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17// See the License for the specific language governing permissions and
18// limitations under the License.
19//
21//
22
23#include "commontraining.h" // CheckSharedLibraryVersion
24#include "dict.h"
25#include "tesseractclass.h"
26
27#include <tesseract/baseapi.h>
28#include "helpers.h"
29
30int main(int argc, char **argv) {
31 tesseract::CheckSharedLibraryVersion();
32
33 // Parse input arguments.
34 if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
35 printf("%s\n", tesseract::TessBaseAPI::Version());
36 return EXIT_SUCCESS;
37 } else if (argc != 4 && (argc != 6 || strcmp(argv[1], "-l") != 0)) {
38 printf(
39 "Usage: %s -v | --version | %s [-l lang] tessdata_dir wordlist_file"
40 " output_ambiguous_wordlist_file\n",
41 argv[0], argv[0]);
42 return EXIT_FAILURE;
43 }
44 int argv_offset = 0;
45 std::string lang;
46 if (argc == 6) {
47 lang = argv[2];
48 argv_offset = 2;
49 } else {
50 lang = "eng";
51 }
52 const char *tessdata_dir = argv[++argv_offset];
53 const char *input_file_str = argv[++argv_offset];
54 const char *output_file_str = argv[++argv_offset];
55
56 // Initialize Tesseract.
58 std::vector<std::string> vars_vec;
59 std::vector<std::string> vars_values;
60 vars_vec.emplace_back("output_ambig_words_file");
61 vars_values.emplace_back(output_file_str);
62 api.Init(tessdata_dir, lang.c_str(), tesseract::OEM_TESSERACT_ONLY, nullptr, 0, &vars_vec,
63 &vars_values, false);
64 tesseract::Dict &dict = api.tesseract()->getDict();
65 FILE *input_file = fopen(input_file_str, "rb");
66 if (input_file == nullptr) {
67 tesseract::tprintf("Failed to open input wordlist file %s\n", input_file_str);
68 return EXIT_FAILURE;
69 }
70 char str[CHARS_PER_LINE];
71
72 // Read word list and call Dict::NoDangerousAmbig() for each word
73 // to record ambiguities in the output file.
74 while (fgets(str, CHARS_PER_LINE, input_file) != nullptr) {
75 tesseract::chomp_string(str); // remove newline
76 tesseract::WERD_CHOICE word(str, dict.getUnicharset());
77 dict.NoDangerousAmbig(&word, nullptr, false, nullptr);
78 }
79 // Clean up.
80 fclose(input_file);
81 return EXIT_SUCCESS;
82}
#define CHARS_PER_LINE
Definition: dict.h:44
int main(int argc, char **argv)
@ OEM_TESSERACT_ONLY
Definition: publictypes.h:264
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void chomp_string(char *str)
Definition: helpers.h:91
static const char * Version()
Definition: baseapi.cpp:241
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const std::vector< std::string > *vars_vec, const std::vector< std::string > *vars_values, bool set_only_non_debug_params)
Definition: baseapi.cpp:368
Tesseract * tesseract() const
Definition: baseapi.h:711
Dict & getDict() override
bool NoDangerousAmbig(WERD_CHOICE *BestChoice, DANGERR *fixpt, bool fix_replaceable, MATRIX *ratings)
Definition: stopper.cpp:158
const UNICHARSET & getUnicharset() const
Definition: dict.h:104