tesseract  5.0.0-alpha-619-ge9db
ambiguous_words.cpp
Go to the documentation of this file.
1 // 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 <tesseract/baseapi.h>
24 #include "commontraining.h" // CheckSharedLibraryVersion
25 #include <tesseract/helpers.h>
26 #include <tesseract/strngs.h>
27 #include "dict.h"
28 #include "tesseractclass.h"
29 
30 int 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 0;
37  } else if (argc != 4 && (argc != 6 || strcmp(argv[1], "-l") != 0)) {
38  printf("Usage: %s -v | --version | %s [-l lang] tessdata_dir wordlist_file"
39  " output_ambiguous_wordlist_file\n", argv[0], argv[0]);
40  return 1;
41  }
42  int argv_offset = 0;
43  STRING lang;
44  if (argc == 6) {
45  lang = argv[2];
46  argv_offset = 2;
47  } else {
48  lang = "eng";
49  }
50  const char *tessdata_dir = argv[++argv_offset];
51  const char *input_file_str = argv[++argv_offset];
52  const char *output_file_str = argv[++argv_offset];
53 
54  // Initialize Tesseract.
56  GenericVector<STRING> vars_vec;
57  GenericVector<STRING> vars_values;
58  vars_vec.push_back("output_ambig_words_file");
59  vars_values.push_back(output_file_str);
60  api.Init(tessdata_dir, lang.c_str(), tesseract::OEM_TESSERACT_ONLY, nullptr,
61  0, &vars_vec, &vars_values, false);
62  tesseract::Dict &dict = api.tesseract()->getDict();
63  FILE *input_file = fopen(input_file_str, "rb");
64  if (input_file == nullptr) {
65  tprintf("Failed to open input wordlist file %s\n", input_file_str);
66  exit(1);
67  }
68  char str[CHARS_PER_LINE];
69 
70  // Read word list and call Dict::NoDangerousAmbig() for each word
71  // to record ambiguities in the output file.
72  while (fgets(str, CHARS_PER_LINE, input_file) != nullptr) {
73  chomp_string(str); // remove newline
74  WERD_CHOICE word(str, dict.getUnicharset());
75  dict.NoDangerousAmbig(&word, nullptr, false, nullptr);
76  }
77  // Clean up.
78  fclose(input_file);
79 }
strngs.h
commontraining.h
dict.h
WERD_CHOICE
Definition: ratngs.h:261
tesseractclass.h
STRING
Definition: strngs.h:45
tesseract::TessBaseAPI::Init
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_non_debug_params)
Definition: baseapi.cpp:337
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
baseapi.h
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
chomp_string
void chomp_string(char *str)
Definition: helpers.h:75
tesseract::TessBaseAPI
Definition: baseapi.h:98
tesseract::Tesseract::getDict
Dict & getDict() override
Definition: tesseractclass.cpp:564
tesseract::TessBaseAPI::Version
static const char * Version()
Definition: baseapi.cpp:233
helpers.h
main
int main(int argc, char **argv)
Definition: ambiguous_words.cpp:30
GenericVector< STRING >
tesseract::Dict
Definition: dict.h:91
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::Dict::getUnicharset
const UNICHARSET & getUnicharset() const
Definition: dict.h:101
tesseract::Dict::NoDangerousAmbig
bool NoDangerousAmbig(WERD_CHOICE *BestChoice, DANGERR *fixpt, bool fix_replaceable, MATRIX *ratings)
Definition: stopper.cpp:158
tesseract::TessBaseAPI::tesseract
Tesseract * tesseract() const
Definition: baseapi.h:798
CHARS_PER_LINE
#define CHARS_PER_LINE
Definition: dict.h:38
tesseract::OEM_TESSERACT_ONLY
Definition: publictypes.h:266