Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // 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 <stdio.h>
25
26 #include "baseapi.h"
27 #include "helpers.h"
28 #include "strngs.h"
29 #include "dict.h"
30 #include "tesseractclass.h"
31
32 int main(int argc, char** argv) {
33
34 // Parse input arguments.
35 if (argc != 4 && (argc != 6 || strcmp(argv[1], "-l") != 0)) {
36 printf("Usage: %s [-l lang] tessdata_dir wordlist_file"
37 " output_ambiguious_wordlist_file\n", argv[0]);
38 return 1;
39 }
40 int argv_offset = 0;
41 STRING lang;
42 if (argc == 6) {
43 lang = argv[2];
44 argv_offset = 2;
45 } else {
46 lang = "eng";
47 }
48 const char *tessdata_dir = argv[++argv_offset];
49 const char *input_file_str = argv[++argv_offset];
50 const char *output_file_str = argv[++argv_offset];
51
52 // Initialize Tesseract.
53 tesseract::TessBaseAPI api;
54 GenericVector<STRING> vars_vec;
55 GenericVector<STRING> vars_values;
56 vars_vec.push_back("output_ambig_words_file");
57 vars_values.push_back(output_file_str);
58 api.Init(tessdata_dir, lang.string(), tesseract::OEM_TESSERACT_ONLY,
59 NULL, NULL, &vars_vec, &vars_values, false);
60 tesseract::Dict &dict = api.tesseract()->getDict();
61 FILE *input_file = fopen(input_file_str, "rb");
62 if (input_file == NULL) {
63 tprintf("Failed to open input wordlist file %s\n", input_file_str);
64 exit(1);
65 }
66 char str[CHARS_PER_LINE];
67
68 // Read word list and call Dict::NoDangerousAmbig() for each word
69 // to record ambiguities in the output file.
70 while (fgets(str, CHARS_PER_LINE, input_file) != NULL) {
71 chomp_string(str); // remove newline
72 WERD_CHOICE word(str, dict.getUnicharset());
73 dict.NoDangerousAmbig(&word, NULL, false, NULL, NULL);
74 }
75 // Clean up.
76 fclose(input_file);
77 }