Open Chinese Convert  0.4.1
A project for conversion between Traditional and Simplified Chinese
 All Data Structures Files Functions Variables Groups Pages
opencc_dict.t.c
1 /*
2  * Open Chinese Convert
3  *
4  * Copyright 2010-2013 BYVoid <byvoid@byvoid.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "../dictionary/text.h"
20 #include "../dict_group.h"
21 #include "../encoding.h"
22 #include <unistd.h>
23 #include <trie.h>
24 
25 #ifdef ENABLE_GETTEXT
26 #include <locale.h>
27 #endif
28 
29 #ifndef VERSION
30 #define VERSION ""
31 #endif
32 
33 #define FILENAME_BUFFER_SIZE 4096
34 
35 void add_to_value_list(ucs4_t** values) {
36  /*
37  for (; *values != 0; values++) {
38  ucs4_t* value = *values;
39  }*/
40 }
41 
42 Trie* build_datrie(Dict* dict) {
43  TextDict* text_dict = (TextDict*)dict->dict;
44 
45  AlphaMap* alphabet = alpha_map_new();
46  AlphaChar begin = 0;
47  AlphaChar end = 0x100000;
48  alpha_map_add_range(alphabet, begin, end);
49  Trie* trie = trie_new(alphabet);
50 
51  // Insert entries into trie
52  size_t i;
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);
56  char* key = "";
57  printf("%ld ", i);
58  key = ucs4_to_utf8(entry->key, 0);
59  printf("'%s'\n", key);
60  //free(key);
61  if (i == 2) {
62  free(key);
63  }
64  //ucs4_t* key_ucs4 = utf8_to_ucs4(key, 0);
65  trie_store(trie, (AlphaChar *)entry->key, (TrieData)i);
66  }
67 
68  //alpha_map_free(alphabet);
69  return trie;
70 }
71 
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"));
78  exit(1);
79  }
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"));
84  exit(1);
85  }
86  return dict;
87 }
88 
89 void write_file(Trie* trie, const char* file_name) {
90  FILE* fp = fopen(file_name, "wb");
91  if (!fp) {
92  fprintf(stderr, _("Can not write file: %s\n"), file_name);
93  exit(1);
94  }
95  fclose(fp);
96  trie_save(trie, file_name);
97 }
98 
99 void show_version() {
100  printf(_("\nOpen Chinese Convert (OpenCC) Dictionary Tool\n"
101  "Version %s\n\n"),
102  VERSION);
103 }
104 
105 void show_usage() {
106  show_version();
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"));
113  printf(_("\n"));
114  printf(_("\n"));
115 }
116 
117 int main(int argc, char** argv) {
118  static int oc;
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;
122 
123 #ifdef ENABLE_GETTEXT
124  setlocale(LC_ALL, "");
125  bindtextdomain(PACKAGE_NAME, LOCALEDIR);
126 #endif
127  while ((oc = getopt(argc, argv, "vh-:i:o:")) != -1) {
128  switch (oc) {
129  case 'v':
130  show_version();
131  return 0;
132  case 'h':
133  case '?':
134  show_usage();
135  return 0;
136  case '-':
137  if (strcmp(optarg, "version") == 0) {
138  show_version();
139  } else if (strcmp(optarg, "help") == 0) {
140  show_usage();
141  } else {
142  show_usage();
143  }
144  return 0;
145  case 'i':
146  strcpy(input_file, optarg);
147  input_file_specified = 1;
148  break;
149  case 'o':
150  strcpy(output_file, optarg);
151  output_file_specified = 1;
152  break;
153  }
154  }
155  if (!input_file_specified) {
156  fprintf(stderr, _("Please specify input file using -i.\n"));
157  show_usage();
158  return 1;
159  }
160  if (!output_file_specified) {
161  fprintf(stderr, _("Please specify output file using -o.\n"));
162  show_usage();
163  return 1;
164  }
165  Dict* dict = init(input_file);
166  Trie* trie = build_datrie(dict);
167  write_file(trie, output_file);
168  trie_free(trie);
169  return 0;
170 }