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]
47 self.
od = self.libopencc.opencc_open(0)
49 self.
od = self.libopencc.opencc_open(c_char_p(self.
config))
54 self.libopencc.opencc_close(self.
od)
57 def __perror(self, message):
59 self.libopencc.opencc_perror(message)
67 retv_c = self.libopencc.opencc_convert_utf8(self.
od, text, len(text))
71 retv_c = cast(retv_c, c_char_p)
72 str_buffer = retv_c.value
73 self.libc.free(retv_c);
78 retv = self.libopencc.opencc_dict_load(self.
od, filename, dicttype)
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)