40 if (words_[0] != NULL) {
62 file_name = data_file_path +
lang;
63 file_name +=
".cube.word-freq";
71 vector<string> str_vec;
73 if (str_vec.size() < 2) {
80 int full_len = str.length();
81 int word_cnt = str_vec.size() / 2;
82 word_unigrams_obj->words_ =
new char*[word_cnt];
83 word_unigrams_obj->costs_ =
new int[word_cnt];
85 word_unigrams_obj->words_[0] =
new char[full_len];
88 word_unigrams_obj->word_cnt_ = 0;
89 char *char_buff = word_unigrams_obj->words_[0];
93 for (
int wrd = 0; wrd < str_vec.size(); wrd += 2) {
94 word_unigrams_obj->words_[word_cnt] = char_buff;
96 strcpy(char_buff, str_vec[wrd].c_str());
97 char_buff += (str_vec[wrd].length() + 1);
99 if (sscanf(str_vec[wrd + 1].c_str(),
"%d",
100 word_unigrams_obj->costs_ + word_cnt) != 1) {
101 fprintf(stderr,
"Cube ERROR (WordUnigrams::Create): error reading " 102 "word unigram data.\n");
103 delete word_unigrams_obj;
107 max_cost =
MAX(max_cost, word_unigrams_obj->costs_[word_cnt]);
110 word_unigrams_obj->word_cnt_ = word_cnt;
123 word_unigrams_obj->not_in_list_cost_ = max_cost +
126 return word_unigrams_obj;
143 vector<string> words;
153 for (
int word_idx = 0; word_idx < words.size(); word_idx++) {
163 while (clean_len > 0 &&
174 if (clean_len == 0 || !trunc) {
177 clean_str32 =
new char_32[clean_len + 1];
178 for (
int i = 0; i < clean_len; ++i) {
179 clean_str32[i] = str32[i];
181 clean_str32[clean_len] =
'\0';
191 if (clean_len >= kMinLengthNumOrCaseInvariant &&
209 if (clean_len >= kMinLengthNumOrCaseInvariant) {
211 bool is_numeric =
true;
212 for (
int i = 0; i < clean_len; ++i) {
213 if (!lang_mod->
IsDigit(clean_str32[i]))
219 delete [] clean_str32;
224 return static_cast<int>(cost /
static_cast<double>(words.size()));
231 if (strlen(key_str) == 0)
232 return not_in_list_cost_;
233 int hi = word_cnt_ - 1;
236 int current = (hi + lo) / 2;
237 int comp = strcmp(key_str, words_[current]);
240 return costs_[current];
250 return not_in_list_cost_;
int CostInternal(const char *str) const
static char_32 * ToLower(const char_32 *str32, CharSet *char_set)
static bool ReadFileToString(const string &file_name, string *str)
static char_32 * ToUpper(const char_32 *str32, CharSet *char_set)
static WordUnigrams * Create(const string &data_file_path, const string &lang)
static int Prob2Cost(double prob_val)
static void UTF32ToUTF8(const char_32 *utf32_str, string *str)
static int StrLen(const char_32 *str)
static void SplitStringUsing(const string &str, const string &delims, vector< string > *str_vec)
static bool IsCaseInvariant(const char_32 *str32, CharSet *char_set)
virtual bool IsTrailingPunc(char_32 ch)=0
basic_string< char_32 > string_32
virtual bool IsDigit(char_32 ch)=0
static void UTF8ToUTF32(const char *utf8_str, string_32 *str32)
int Cost(const char_32 *str32, LangModel *lang_mod, CharSet *char_set) const
static char_32 * StrDup(const char_32 *str)