Open Chinese Convert
0.4.3
A project for conversion between Traditional and Simplified Chinese
|
00001 /* 00002 * Open Chinese Convert 00003 * 00004 * Copyright 2010-2013 BYVoid <byvoid@byvoid.com> 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 00019 #include "dict.h" 00020 #include "dictionary/datrie.h" 00021 #include "dictionary/text.h" 00022 00023 Dict* dict_new(const char* filename, opencc_dictionary_type type) { 00024 Dict* dictionary = (Dict*)malloc(sizeof(Dict)); 00025 dictionary->type = type; 00026 switch (type) { 00027 case OPENCC_DICTIONARY_TYPE_TEXT: 00028 dictionary->dict = dict_text_new(filename); 00029 break; 00030 case OPENCC_DICTIONARY_TYPE_DATRIE: 00031 dictionary->dict = dict_datrie_new(filename); 00032 break; 00033 default: 00034 free(dictionary); 00035 dictionary = (Dict*)-1; /* TODO:辭典格式不支持 */ 00036 } 00037 return dictionary; 00038 } 00039 00040 void dict_delete(Dict* dict) { 00041 switch (dict->type) { 00042 case OPENCC_DICTIONARY_TYPE_TEXT: 00043 dict_text_delete(dict->dict); 00044 break; 00045 case OPENCC_DICTIONARY_TYPE_DATRIE: 00046 dict_datrie_delete(dict->dict); 00047 break; 00048 default: 00049 debug_should_not_be_here(); 00050 } 00051 free(dict); 00052 } 00053 00054 const ucs4_t* const* dict_match_longest(Dict* dict, 00055 const ucs4_t* word, 00056 size_t maxlen, 00057 size_t* match_length) { 00058 switch (dict->type) { 00059 case OPENCC_DICTIONARY_TYPE_TEXT: 00060 return dict_text_match_longest(dict->dict, 00061 word, 00062 maxlen, 00063 match_length); 00064 break; 00065 case OPENCC_DICTIONARY_TYPE_DATRIE: 00066 return dict_datrie_match_longest(dict->dict, 00067 word, 00068 maxlen, 00069 match_length); 00070 break; 00071 default: 00072 debug_should_not_be_here(); 00073 } 00074 return (const ucs4_t* const*)-1; 00075 } 00076 00077 size_t dict_get_all_match_lengths(Dict* dict, 00078 const ucs4_t* word, 00079 size_t* match_length) { 00080 switch (dict->type) { 00081 case OPENCC_DICTIONARY_TYPE_TEXT: 00082 return dict_text_get_all_match_lengths(dict->dict, 00083 word, 00084 match_length); 00085 break; 00086 case OPENCC_DICTIONARY_TYPE_DATRIE: 00087 return dict_datrie_get_all_match_lengths(dict->dict, 00088 word, 00089 match_length); 00090 break; 00091 default: 00092 debug_should_not_be_here(); 00093 } 00094 return (size_t)-1; 00095 }