19 #include "../dictionary/text.h"
20 #include "../dict_group.h"
21 #include "../encoding.h"
33 #define FILENAME_BUFFER_SIZE 4096
35 void add_to_value_list(ucs4_t** values) {
42 Trie* build_datrie(
Dict* dict) {
45 AlphaMap* alphabet = alpha_map_new();
47 AlphaChar end = 0x100000;
48 alpha_map_add_range(alphabet, begin, end);
49 Trie* trie = trie_new(alphabet);
53 for (i = 0; i < text_dict->entry_count; i++) {
54 const TextEntry* entry = &text_dict->lexicon[i];
55 add_to_value_list(entry->value);
58 key = ucs4_to_utf8(entry->key, 0);
59 printf(
"'%s'\n", key);
65 trie_store(trie, (AlphaChar *)entry->key, (TrieData)i);
72 Dict* init(
const char* filename) {
73 DictGroup* dict_group = dict_group_new(NULL);
74 if (dict_group_load(dict_group, filename,
75 OPENCC_DICTIONARY_TYPE_TEXT) == -1) {
76 dictionary_perror(
"Dictionary loading error");
77 fprintf(stderr, _(
"\n"));
80 Dict* dict = dict_group_get_dict(dict_group, 0);
81 if (dict == (
Dict*)-1) {
82 dictionary_perror(
"Dictionary loading error");
83 fprintf(stderr, _(
"\n"));
89 void write_file(Trie* trie,
const char* file_name) {
90 FILE* fp = fopen(file_name,
"wb");
92 fprintf(stderr, _(
"Can not write file: %s\n"), file_name);
96 trie_save(trie, file_name);
100 printf(_(
"\nOpen Chinese Convert (OpenCC) Dictionary Tool\n"
107 printf(_(
"Usage:\n"));
108 printf(_(
" opencc_dict -i input_file -o output_file\n\n"));
109 printf(_(
" -i input_file\n"));
110 printf(_(
" Read data from input_file.\n"));
111 printf(_(
" -o output_file\n"));
112 printf(_(
" Write converted data to output_file.\n"));
117 int main(
int argc,
char** argv) {
119 static char input_file[FILENAME_BUFFER_SIZE];
120 static char output_file[FILENAME_BUFFER_SIZE];
121 int input_file_specified = 0, output_file_specified = 0;
123 #ifdef ENABLE_GETTEXT
124 setlocale(LC_ALL,
"");
125 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
127 while ((oc = getopt(argc, argv,
"vh-:i:o:")) != -1) {
137 if (strcmp(optarg,
"version") == 0) {
139 }
else if (strcmp(optarg,
"help") == 0) {
146 strcpy(input_file, optarg);
147 input_file_specified = 1;
150 strcpy(output_file, optarg);
151 output_file_specified = 1;
155 if (!input_file_specified) {
156 fprintf(stderr, _(
"Please specify input file using -i.\n"));
160 if (!output_file_specified) {
161 fprintf(stderr, _(
"Please specify output file using -o.\n"));
165 Dict* dict = init(input_file);
166 Trie* trie = build_datrie(dict);
167 write_file(trie, output_file);