#include <word_unigrams.h>
Definition at line 34 of file word_unigrams.h.
tesseract::WordUnigrams::WordUnigrams |
( |
| ) |
|
tesseract::WordUnigrams::~WordUnigrams |
( |
| ) |
|
Split input into space-separated tokens, strip trailing punctuation from each, determine case properties, call UTF-8 flavor of cost function on each word, and aggregate all into single mean word cost.
Definition at line 154 of file word_unigrams.cpp.
162 vector<string> words;
166 if (words.size() <= 0) {
172 for (
int word_idx = 0; word_idx < words.size(); word_idx++) {
182 while (clean_len > 0 &&
183 lang_mod->IsTrailingPunc(str32.c_str()[clean_len - 1])) {
193 if (clean_len == 0 || !trunc) {
196 clean_str32 =
new char_32[clean_len + 1];
197 for (
int i = 0; i < clean_len; ++i) {
198 clean_str32[i] = str32[i];
200 clean_str32[clean_len] =
'\0';
210 if (clean_len >= kMinLengthNumOrCaseInvariant &&
228 if (clean_len >= kMinLengthNumOrCaseInvariant) {
230 bool is_numeric =
true;
231 for (
int i = 0; i < clean_len; ++i) {
232 if (!lang_mod->IsDigit(clean_str32[i]))
238 delete [] clean_str32;
243 return static_cast<int>(cost /
static_cast<double>(words.size()));
basic_string< char_32 > string_32
static char_32 * ToLower(const char_32 *str32, CharSet *char_set)
static void UTF8ToUTF32(const char *utf8_str, string_32 *str32)
int CostInternal(const char *str) const
static int StrLen(const char_32 *str)
static void UTF32ToUTF8(const char_32 *utf32_str, string *str)
static void SplitStringUsing(const string &str, const string &delims, vector< string > *str_vec)
static char_32 * ToUpper(const char_32 *str32, CharSet *char_set)
static bool IsCaseInvariant(const char_32 *str32, CharSet *char_set)
static char_32 * StrDup(const char_32 *str)
int tesseract::WordUnigrams::CostInternal |
( |
const char * |
key_str | ) |
const |
|
protected |
Search for UTF-8 string using binary search of sorted words_ array.
Definition at line 249 of file word_unigrams.cpp.
250 if (strlen(key_str) == 0)
251 return not_in_list_cost_;
252 int hi = word_cnt_ - 1;
255 int current = (hi + lo) / 2;
256 int comp = strcmp(key_str, words_[current]);
259 return costs_[current];
269 return not_in_list_cost_;
WordUnigrams * tesseract::WordUnigrams::Create |
( |
const string & |
data_file_path, |
|
|
const string & |
lang |
|
) |
| |
|
static |
Load the word-list and unigrams from file and create an object The word list is assumed to be sorted in lexicographic order.
Definition at line 57 of file word_unigrams.cpp.
62 file_name = data_file_path +
lang;
63 file_name +=
".cube.word-freq";
71 vector<string> str_vec;
73 if (str_vec.size() < 2) {
79 if (word_unigrams_obj ==
NULL) {
80 fprintf(stderr,
"Cube ERROR (WordUnigrams::Create): could not create "
81 "word unigrams object.\n");
85 int full_len = str.length();
86 int word_cnt = str_vec.size() / 2;
87 word_unigrams_obj->words_ =
new char*[word_cnt];
88 word_unigrams_obj->costs_ =
new int[word_cnt];
90 if (word_unigrams_obj->words_ ==
NULL ||
91 word_unigrams_obj->costs_ ==
NULL) {
92 fprintf(stderr,
"Cube ERROR (WordUnigrams::Create): error allocating "
93 "word unigram fields.\n");
94 delete word_unigrams_obj;
98 word_unigrams_obj->words_[0] =
new char[full_len];
99 if (word_unigrams_obj->words_[0] ==
NULL) {
100 fprintf(stderr,
"Cube ERROR (WordUnigrams::Create): error allocating "
101 "word unigram fields.\n");
102 delete word_unigrams_obj;
107 word_unigrams_obj->word_cnt_ = 0;
108 char *char_buff = word_unigrams_obj->words_[0];
112 for (
int wrd = 0; wrd < str_vec.size(); wrd += 2) {
113 word_unigrams_obj->words_[word_cnt] = char_buff;
115 strcpy(char_buff, str_vec[wrd].c_str());
116 char_buff += (str_vec[wrd].length() + 1);
118 if (sscanf(str_vec[wrd + 1].c_str(),
"%d",
119 word_unigrams_obj->costs_ + word_cnt) != 1) {
120 fprintf(stderr,
"Cube ERROR (WordUnigrams::Create): error reading "
121 "word unigram data.\n");
122 delete word_unigrams_obj;
126 max_cost =
MAX(max_cost, word_unigrams_obj->costs_[word_cnt]);
129 word_unigrams_obj->word_cnt_ = word_cnt;
142 word_unigrams_obj->not_in_list_cost_ = max_cost +
145 return word_unigrams_obj;
static int Prob2Cost(double prob_val)
static bool ReadFileToString(const string &file_name, string *str)
static void SplitStringUsing(const string &str, const string &delims, vector< string > *str_vec)
The documentation for this class was generated from the following files: