tesseract  4.0.0-1-g2a2b
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 // Created: Fri Oct 21 11:26:43 PDT 2011
9 //
10 // (C) Copyright 2011, Google Inc.
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
22 //
23 
24 #include "baseapi.h"
25 #include "commontraining.h" // CheckSharedLibraryVersion
26 #include "helpers.h"
27 #include "strngs.h"
28 #include "dict.h"
29 #include "tesseractclass.h"
30 
31 int main(int argc, char** argv) {
32  tesseract::CheckSharedLibraryVersion();
33 
34  // Parse input arguments.
35  if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
36  printf("%s\n", tesseract::TessBaseAPI::Version());
37  return 0;
38  } else if (argc != 4 && (argc != 6 || strcmp(argv[1], "-l") != 0)) {
39  printf("Usage: %s -v | --version | %s [-l lang] tessdata_dir wordlist_file"
40  " output_ambiguous_wordlist_file\n", argv[0], argv[0]);
41  return 1;
42  }
43  int argv_offset = 0;
44  STRING lang;
45  if (argc == 6) {
46  lang = argv[2];
47  argv_offset = 2;
48  } else {
49  lang = "eng";
50  }
51  const char *tessdata_dir = argv[++argv_offset];
52  const char *input_file_str = argv[++argv_offset];
53  const char *output_file_str = argv[++argv_offset];
54 
55  // Initialize Tesseract.
57  GenericVector<STRING> vars_vec;
58  GenericVector<STRING> vars_values;
59  vars_vec.push_back("output_ambig_words_file");
60  vars_values.push_back(output_file_str);
61  api.Init(tessdata_dir, lang.string(), tesseract::OEM_TESSERACT_ONLY, nullptr,
62  0, &vars_vec, &vars_values, false);
63  tesseract::Dict &dict = api.tesseract()->getDict();
64  FILE *input_file = fopen(input_file_str, "rb");
65  if (input_file == nullptr) {
66  tprintf("Failed to open input wordlist file %s\n", input_file_str);
67  exit(1);
68  }
69  char str[CHARS_PER_LINE];
70 
71  // Read word list and call Dict::NoDangerousAmbig() for each word
72  // to record ambiguities in the output file.
73  while (fgets(str, CHARS_PER_LINE, input_file) != nullptr) {
74  chomp_string(str); // remove newline
75  WERD_CHOICE word(str, dict.getUnicharset());
76  dict.NoDangerousAmbig(&word, nullptr, false, nullptr);
77  }
78  // Clean up.
79  fclose(input_file);
80 }
Tesseract * tesseract() const
Definition: baseapi.h:783
Dict & getDict() override
#define CHARS_PER_LINE
Definition: dict.h:35
const char * string() const
Definition: strngs.cpp:196
int main(int argc, char **argv)
void chomp_string(char *str)
Definition: helpers.h:83
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int push_back(T object)
Definition: strngs.h:45
const UNICHARSET & getUnicharset() const
Definition: dict.h:98
static const char * Version()
Definition: baseapi.cpp:223
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:353
bool NoDangerousAmbig(WERD_CHOICE *BestChoice, DANGERR *fixpt, bool fix_replaceable, MATRIX *ratings)
Definition: stopper.cpp:142