3 * Library to deal with pinyin.
5 * Copyright (C) 2010 Peng Wu
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.
24 #include "pinyin_internal.h"
25 #include "utils_helper.h"
27 static const gchar * table_dir = ".";
29 static GOptionEntry entries[] =
31 {"table-dir", 0, 0, G_OPTION_ARG_FILENAME, &table_dir, "table directory", NULL},
35 int main(int argc, char * argv[]){
36 setlocale(LC_ALL, "");
38 GError * error = NULL;
39 GOptionContext * context;
41 context = g_option_context_new("- generate binary files");
42 g_option_context_add_main_entries(context, entries, NULL);
43 if (!g_option_context_parse(context, &argc, &argv, &error)) {
44 g_print("option parsing failed:%s\n", error->message);
48 SystemTableInfo system_table_info;
50 bool retval = system_table_info.load("table.conf");
52 fprintf(stderr, "load table.conf failed.\n");
56 /* generate pinyin index*/
57 pinyin_option_t options = USE_TONE;
58 ChewingLargeTable chewing_table(options);
59 PhraseLargeTable2 phrase_table;
61 /* generate phrase index */
62 FacadePhraseIndex phrase_index;
64 const pinyin_table_info_t * phrase_files =
65 system_table_info.get_table_info();
67 for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
68 const pinyin_table_info_t * table_info = phrase_files + i;
69 assert(table_info->m_dict_index == i);
71 if (SYSTEM_FILE != table_info->m_file_type &&
72 DICTIONARY != table_info->m_file_type)
75 const char * tablename = table_info->m_table_filename;
77 gchar * filename = g_build_filename(table_dir, tablename, NULL);
78 FILE * tablefile = fopen(filename, "r");
80 if (NULL == tablefile) {
81 fprintf(stderr, "open %s failed!\n", tablename);
85 chewing_table.load_text(tablefile);
86 fseek(tablefile, 0L, SEEK_SET);
87 phrase_table.load_text(tablefile);
88 fseek(tablefile, 0L, SEEK_SET);
89 phrase_index.load_text(i, tablefile);
94 MemoryChunk * new_chunk = new MemoryChunk;
95 chewing_table.store(new_chunk);
96 new_chunk->save("pinyin_index.bin");
97 chewing_table.load(new_chunk);
99 new_chunk = new MemoryChunk;
100 phrase_table.store(new_chunk);
101 new_chunk->save("phrase_index.bin");
102 phrase_table.load(new_chunk);
104 phrase_index.compact();
106 if (!save_phrase_index(phrase_files, &phrase_index))
109 if (!save_dictionary(phrase_files, &phrase_index))