Open Chinese Convert  0.4.3
A project for conversion between Traditional and Simplified Chinese
 All Data Structures Files Functions Variables Groups Pages
dict.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 "dict.h"
20 #include "dictionary/datrie.h"
21 #include "dictionary/text.h"
22 
23 Dict* dict_new(const char* filename, opencc_dictionary_type type) {
24  Dict* dictionary = (Dict*)malloc(sizeof(Dict));
25  dictionary->type = type;
26  switch (type) {
27  case OPENCC_DICTIONARY_TYPE_TEXT:
28  dictionary->dict = dict_text_new(filename);
29  break;
30  case OPENCC_DICTIONARY_TYPE_DATRIE:
31  dictionary->dict = dict_datrie_new(filename);
32  break;
33  default:
34  free(dictionary);
35  dictionary = (Dict*)-1; /* TODO:辭典格式不支持 */
36  }
37  return dictionary;
38 }
39 
40 void dict_delete(Dict* dict) {
41  switch (dict->type) {
42  case OPENCC_DICTIONARY_TYPE_TEXT:
43  dict_text_delete(dict->dict);
44  break;
45  case OPENCC_DICTIONARY_TYPE_DATRIE:
46  dict_datrie_delete(dict->dict);
47  break;
48  default:
49  debug_should_not_be_here();
50  }
51  free(dict);
52 }
53 
54 const ucs4_t* const* dict_match_longest(Dict* dict,
55  const ucs4_t* word,
56  size_t maxlen,
57  size_t* match_length) {
58  switch (dict->type) {
59  case OPENCC_DICTIONARY_TYPE_TEXT:
60  return dict_text_match_longest(dict->dict,
61  word,
62  maxlen,
63  match_length);
64  break;
65  case OPENCC_DICTIONARY_TYPE_DATRIE:
66  return dict_datrie_match_longest(dict->dict,
67  word,
68  maxlen,
69  match_length);
70  break;
71  default:
72  debug_should_not_be_here();
73  }
74  return (const ucs4_t* const*)-1;
75 }
76 
77 size_t dict_get_all_match_lengths(Dict* dict,
78  const ucs4_t* word,
79  size_t* match_length) {
80  switch (dict->type) {
81  case OPENCC_DICTIONARY_TYPE_TEXT:
82  return dict_text_get_all_match_lengths(dict->dict,
83  word,
84  match_length);
85  break;
86  case OPENCC_DICTIONARY_TYPE_DATRIE:
87  return dict_datrie_get_all_match_lengths(dict->dict,
88  word,
89  match_length);
90  break;
91  default:
92  debug_should_not_be_here();
93  }
94  return (size_t)-1;
95 }