4 from ctypes
import cast, cdll, c_char_p, c_int, c_size_t, c_void_p
5 from ctypes.util
import find_library
27 self.
libopencc = cdll.LoadLibrary(find_library(
'opencc'))
28 self.
libopencc.opencc_open.restype = c_void_p
29 self.
libopencc.opencc_convert_utf8.argtypes = [c_void_p, c_char_p, c_size_t]
32 self.
libopencc.opencc_convert_utf8.restype = c_void_p
33 self.
libopencc.opencc_close.argtypes = [c_void_p]
34 self.
libopencc.opencc_perror.argtypes = [c_char_p]
35 self.
libopencc.opencc_dict_load.argtypes = [c_void_p, c_char_p, c_int]
37 self.
libc = cdll.LoadLibrary(find_library(
'c'))
38 self.
libc.free.argtypes = [c_void_p]
57 def __perror(self, message):
71 retv_c = cast(retv_c, c_char_p)
72 str_buffer = retv_c.value
73 self.
libc.free(retv_c);
83 if __name__ ==
"__main__":
86 with
OpenCC()
as converter:
87 for path
in [
'simp_to_trad_characters.ocd',
88 'simp_to_trad_phrases.ocd']:
89 converter.dict_load(path, DictType.DATRIE)
90 print converter.convert(text)
def __init__(self, config=None, verbose=True)
Constructor.
char * opencc_convert_utf8(opencc_t t_opencc, const char *inbuf, size_t length)
Converts UTF-8 string from inbuf.
opencc_t opencc_open(const char *config_file)
Makes an instance of opencc.
def convert(self, text)
Converts text.
int opencc_dict_load(opencc_t t_opencc, const char *dict_filename, opencc_dictionary_type dict_type)
Loads a dictionary to default dictionary chain.
int opencc_close(opencc_t t_opencc)
Destroys an instance of opencc.
void opencc_perror(const char *spec)
Prints the error message to stderr.
OpenCC Python language binding.
def __exit__(self, type, value, traceback)
def __perror(self, message)
def dict_load(self, filename, dicttype)