3 * Library to deal with pinyin.
5 * Copyright (C) 2013 Peng Wu <alexepico@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "table_info.h"
28 using namespace pinyin;
31 static const pinyin_table_info_t reserved_tables[] = {
32 {RESERVED, NULL, NULL, NULL, NOT_USED},
33 {GB_DICTIONARY, "gb_char.table", "gb_char.bin", "gb_char.dbin", SYSTEM_FILE},
34 {GBK_DICTIONARY, "gbk_char.table", "gbk_char.bin", "gbk_char.dbin", SYSTEM_FILE},
36 {MERGED_DICTIONARY, "merged.table", "merged.bin", "merged.dbin", SYSTEM_FILE},
38 {USER_DICTIONARY, NULL, NULL, "user.bin", USER_FILE}
42 SystemTableInfo::SystemTableInfo() {
43 m_binary_format_version = 0;
44 m_model_data_version = 0;
48 for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
49 pinyin_table_info_t * table_info = &m_table_info[i];
51 table_info->m_dict_index = i;
52 table_info->m_table_filename = NULL;
53 table_info->m_system_filename = NULL;
54 table_info->m_user_filename = NULL;
55 table_info->m_file_type = NOT_USED;
59 SystemTableInfo::~SystemTableInfo() {
63 void SystemTableInfo::reset() {
64 m_binary_format_version = 0;
65 m_model_data_version = 0;
69 for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
70 pinyin_table_info_t * table_info = &m_table_info[i];
72 g_free((gchar *)table_info->m_table_filename);
73 table_info->m_table_filename = NULL;
74 g_free((gchar *)table_info->m_system_filename);
75 table_info->m_system_filename = NULL;
76 g_free((gchar *)table_info->m_user_filename);
77 table_info->m_user_filename = NULL;
79 table_info->m_file_type = NOT_USED;
83 void SystemTableInfo::postfix_tables() {
85 for (i = 0; i < G_N_ELEMENTS(reserved_tables); ++i) {
86 const pinyin_table_info_t * postfix = &reserved_tables[i];
88 guint8 index = postfix->m_dict_index;
89 pinyin_table_info_t * table_info = &m_table_info[index];
90 assert(table_info->m_dict_index == index);
92 table_info->m_table_filename = g_strdup(postfix->m_table_filename);
93 table_info->m_system_filename = g_strdup(postfix->m_system_filename);
94 table_info->m_user_filename = g_strdup(postfix->m_user_filename);
95 table_info->m_file_type = postfix->m_file_type;
99 static gchar * to_string(const char * str) {
100 if (0 == strcmp(str, "NULL"))
103 return g_strdup(str);
106 static PHRASE_FILE_TYPE to_file_type(const char * str) {
107 #define HANDLE(x) { \
108 if (0 == strcmp(str, #x)) \
122 bool SystemTableInfo::load(const char * filename) {
125 char * locale = setlocale(LC_NUMERIC, "C");
127 FILE * input = fopen(filename, "r");
129 fprintf(stderr, "open %s failed.\n", filename);
133 int binver = 0, modelver = 0;
136 int num = fscanf(input, "binary format version:%d\n", &binver);
142 num = fscanf(input, "model data version:%d\n", &modelver);
148 num = fscanf(input, "lambda parameter:%f\n", &lambda);
155 printf("binver:%d modelver:%d lambda:%f\n", binver, modelver, lambda);
158 m_binary_format_version = binver;
159 m_model_data_version = modelver;
163 char tablefile[256], sysfile[256], userfile[256], filetype[256];
164 while (!feof(input)) {
165 num = fscanf(input, "%d %s %s %s %s\n",
166 &index, tablefile, sysfile, userfile, filetype);
171 if (!(0 <= index && index < PHRASE_INDEX_LIBRARY_COUNT))
174 /* save into m_table_info. */
175 pinyin_table_info_t * table_info = &m_table_info[index];
176 assert(index == table_info->m_dict_index);
178 table_info->m_table_filename = to_string(tablefile);
179 table_info->m_system_filename = to_string(sysfile);
180 table_info->m_user_filename = to_string(userfile);
182 table_info->m_file_type = to_file_type(filetype);
187 /* postfix reserved tables. */
190 setlocale(LC_NUMERIC, locale);
195 const pinyin_table_info_t * SystemTableInfo::get_table_info() {
199 gfloat SystemTableInfo::get_lambda() {
204 UserTableInfo::UserTableInfo() {
205 m_binary_format_version = 0;
206 m_model_data_version = 0;
209 void UserTableInfo::reset() {
210 m_binary_format_version = 0;
211 m_model_data_version = 0;
214 bool UserTableInfo::load(const char * filename) {
217 char * locale = setlocale(LC_NUMERIC, "C");
219 FILE * input = fopen(filename, "r");
221 fprintf(stderr, "open %s failed.", filename);
225 int binver = 0, modelver = 0;
227 int num = fscanf(input, "binary format version:%d\n", &binver);
233 num = fscanf(input, "model data version:%d\n", &modelver);
240 printf("binver:%d modelver:%d\n", binver, modelver);
243 m_binary_format_version = binver;
244 m_model_data_version = modelver;
248 setlocale(LC_NUMERIC, locale);
253 bool UserTableInfo::save(const char * filename) {
254 char * locale = setlocale(LC_NUMERIC, "C");
256 FILE * output = fopen(filename, "w");
257 if (NULL == output) {
258 fprintf(stderr, "write %s failed.\n", filename);
262 fprintf(output, "binary format version:%d\n", m_binary_format_version);
263 fprintf(output, "model data version:%d\n", m_model_data_version);
267 setlocale(LC_NUMERIC, locale);
272 bool UserTableInfo::is_conform(const SystemTableInfo * sysinfo) {
273 if (sysinfo->m_binary_format_version != m_binary_format_version)
276 if (sysinfo->m_model_data_version != m_model_data_version)
282 bool UserTableInfo::make_conform(const SystemTableInfo * sysinfo) {
283 m_binary_format_version = sysinfo->m_binary_format_version;
284 m_model_data_version = sysinfo->m_model_data_version;