2007-07-18 09:15:07 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// File: wordlist2dawg.cpp
|
|
|
|
// Description: Program to generate a DAWG from a word list file
|
|
|
|
// Author: Thomas Kielbus
|
|
|
|
// Created: Thu May 10 18:11:42 PDT 2007
|
|
|
|
//
|
|
|
|
// (C) Copyright 2006, Google Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Given a file that contains a list of words (one word per line) this program
|
|
|
|
// generates the corresponding squished DAWG file.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2009-07-11 10:44:07 +08:00
|
|
|
#include "classify.h"
|
2007-07-18 09:15:07 +08:00
|
|
|
#include "dawg.h"
|
2010-11-24 02:34:14 +08:00
|
|
|
#include "dict.h"
|
2007-07-18 09:15:07 +08:00
|
|
|
#include "emalloc.h"
|
2009-07-11 10:44:07 +08:00
|
|
|
#include "freelist.h"
|
2010-11-24 02:34:14 +08:00
|
|
|
#include "helpers.h"
|
|
|
|
#include "serialis.h"
|
2009-07-11 10:44:07 +08:00
|
|
|
#include "trie.h"
|
|
|
|
#include "unicharset.h"
|
2007-07-18 09:15:07 +08:00
|
|
|
|
2009-07-11 10:44:07 +08:00
|
|
|
static const int kMaxNumEdges = 10000000;
|
2007-07-18 09:15:07 +08:00
|
|
|
|
2009-07-11 10:44:07 +08:00
|
|
|
int main(int argc, char** argv) {
|
2010-11-24 02:34:14 +08:00
|
|
|
int min_word_length;
|
|
|
|
int max_word_length;
|
|
|
|
if (!(argc == 4 || (argc == 5 && strcmp(argv[1], "-t") == 0) ||
|
|
|
|
(argc == 7 && strcmp(argv[1], "-l") == 0 &&
|
|
|
|
sscanf(argv[2], "%d", &min_word_length) == 1 &&
|
|
|
|
sscanf(argv[3], "%d", &max_word_length) == 1))) {
|
|
|
|
printf("Usage: %s [-t | -l min_len max_len] word_list_file"
|
|
|
|
" dawg_file unicharset_file", argv[0]);
|
2009-07-11 10:44:07 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
tesseract::Classify *classify = new tesseract::Classify();
|
2009-07-11 10:44:07 +08:00
|
|
|
int argv_index = 0;
|
|
|
|
if (argc == 5) ++argv_index;
|
2010-11-24 02:34:14 +08:00
|
|
|
if (argc == 7) argv_index += 3;
|
2009-07-11 10:44:07 +08:00
|
|
|
const char* wordlist_filename = argv[++argv_index];
|
|
|
|
const char* dawg_filename = argv[++argv_index];
|
|
|
|
const char* unicharset_file = argv[++argv_index];
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Loading unicharset from '%s'\n", unicharset_file);
|
2010-11-24 02:34:14 +08:00
|
|
|
if (!classify->getDict().getUnicharset().load_from_file(unicharset_file)) {
|
2009-07-11 10:44:07 +08:00
|
|
|
tprintf("Failed to load unicharset from '%s'\n", unicharset_file);
|
2010-11-24 02:34:14 +08:00
|
|
|
delete classify;
|
2009-07-11 10:44:07 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
const UNICHARSET &unicharset = classify->getDict().getUnicharset();
|
2009-07-11 10:44:07 +08:00
|
|
|
if (argc == 4) {
|
|
|
|
tesseract::Trie trie(
|
|
|
|
// the first 3 arguments are not used in this case
|
|
|
|
tesseract::DAWG_TYPE_WORD, "", SYSTEM_DAWG_PERM,
|
2010-11-24 02:34:14 +08:00
|
|
|
kMaxNumEdges, unicharset.size(),
|
|
|
|
classify->getDict().dawg_debug_level);
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Reading word list from '%s'\n", wordlist_filename);
|
2009-07-11 10:44:07 +08:00
|
|
|
if (!trie.read_word_list(wordlist_filename, unicharset)) {
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Failed to read word list from '%s'\n", wordlist_filename);
|
2008-04-22 08:39:51 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Reducing Trie to SquishedDawg\n");
|
2009-07-11 10:44:07 +08:00
|
|
|
tesseract::SquishedDawg *dawg = trie.trie_to_dawg();
|
2011-03-22 05:48:58 +08:00
|
|
|
if (dawg != NULL && dawg->NumEdges() > 0) {
|
|
|
|
tprintf("Writing squished DAWG to '%s'\n", dawg_filename);
|
2010-11-30 09:01:13 +08:00
|
|
|
dawg->write_squished_dawg(dawg_filename);
|
|
|
|
} else {
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Dawg is empty, skip producing the output file\n");
|
2010-11-30 09:01:13 +08:00
|
|
|
}
|
2009-07-11 10:44:07 +08:00
|
|
|
delete dawg;
|
2010-11-24 02:34:14 +08:00
|
|
|
} else if (argc == 5) {
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Loading dawg DAWG from '%s'\n", dawg_filename);
|
2009-07-11 10:44:07 +08:00
|
|
|
tesseract::SquishedDawg words(
|
|
|
|
dawg_filename,
|
|
|
|
// these 3 arguments are not used in this case
|
2010-11-24 02:34:14 +08:00
|
|
|
tesseract::DAWG_TYPE_WORD, "", SYSTEM_DAWG_PERM,
|
|
|
|
classify->getDict().dawg_debug_level);
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Checking word list from '%s'\n", wordlist_filename);
|
2009-07-11 10:44:07 +08:00
|
|
|
words.check_for_words(wordlist_filename, unicharset, true);
|
2010-11-24 02:34:14 +08:00
|
|
|
} else if (argc == 7) {
|
|
|
|
// Place words of different lengths in separate Dawgs.
|
|
|
|
char str[CHARS_PER_LINE];
|
|
|
|
FILE *word_file = fopen(wordlist_filename, "r");
|
|
|
|
if (word_file == NULL) {
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Failed to open wordlist file %s\n", wordlist_filename);
|
2010-11-24 02:34:14 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
FILE *dawg_file = fopen(dawg_filename, "wb");
|
|
|
|
if (dawg_file == NULL) {
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Failed to open dawg output file %s\n", dawg_filename);
|
2010-11-24 02:34:14 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Reading word list from '%s'\n", wordlist_filename);
|
2010-11-24 02:34:14 +08:00
|
|
|
GenericVector<tesseract::Trie *> trie_vec;
|
|
|
|
int i;
|
|
|
|
for (i = min_word_length; i <= max_word_length; ++i) {
|
|
|
|
trie_vec.push_back(new tesseract::Trie(
|
|
|
|
// the first 3 arguments are not used in this case
|
|
|
|
tesseract::DAWG_TYPE_WORD, "", SYSTEM_DAWG_PERM,
|
|
|
|
kMaxNumEdges, unicharset.size(),
|
|
|
|
classify->getDict().dawg_debug_level));
|
|
|
|
}
|
|
|
|
while (fgets(str, CHARS_PER_LINE, word_file) != NULL) {
|
|
|
|
chomp_string(str); // remove newline
|
|
|
|
WERD_CHOICE word(str, unicharset);
|
|
|
|
if (word.length() >= min_word_length &&
|
|
|
|
word.length() <= max_word_length &&
|
|
|
|
!word.contains_unichar_id(INVALID_UNICHAR_ID)) {
|
|
|
|
tesseract::Trie *curr_trie = trie_vec[word.length()-min_word_length];
|
|
|
|
if (!curr_trie->word_in_dawg(word)) {
|
|
|
|
curr_trie->add_word_to_dawg(word);
|
|
|
|
if (classify->getDict().dawg_debug_level > 1) {
|
|
|
|
tprintf("Added word %s of length %d\n", str, word.length());
|
|
|
|
}
|
|
|
|
if (!curr_trie->word_in_dawg(word)) {
|
|
|
|
tprintf("Error: word '%s' not in DAWG after adding it\n", str);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(word_file);
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Writing fixed length dawgs to '%s'\n", dawg_filename);
|
2010-11-24 02:34:14 +08:00
|
|
|
GenericVector<tesseract::SquishedDawg *> dawg_vec;
|
|
|
|
for (i = 0; i <= max_word_length; ++i) {
|
|
|
|
dawg_vec.push_back(i < min_word_length ? NULL :
|
|
|
|
trie_vec[i-min_word_length]->trie_to_dawg());
|
|
|
|
}
|
|
|
|
tesseract::Dict::WriteFixedLengthDawgs(
|
|
|
|
dawg_vec, max_word_length - min_word_length + 1,
|
|
|
|
classify->getDict().dawg_debug_level, dawg_file);
|
|
|
|
fclose(dawg_file);
|
|
|
|
dawg_vec.delete_data_pointers();
|
|
|
|
trie_vec.delete_data_pointers();
|
|
|
|
} else { // should never get here
|
2011-03-22 05:48:58 +08:00
|
|
|
tprintf("Invalid command-line options\n");
|
2010-11-24 02:34:14 +08:00
|
|
|
exit(1);
|
2008-04-22 08:39:51 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
delete classify;
|
2009-07-11 10:44:07 +08:00
|
|
|
return 0;
|
2007-07-18 09:15:07 +08:00
|
|
|
}
|