-
Notifications
You must be signed in to change notification settings - Fork 16
/
opencc.py
executable file
·58 lines (47 loc) · 1.6 KB
/
opencc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ctypes import cast, cdll, c_char_p, c_int, c_size_t, c_void_p
from ctypes.util import find_library
import sys
class ConvertError(Exception):
pass
class DictType:
TEXT,DATRIE = 0,1
class OpenCC:
def __init__(self, config=None, verbose=True):
self.libopencc = cdll.LoadLibrary(find_library('opencc'))
self.libopencc.opencc_open.restype = c_void_p
self.libopencc.opencc_convert_utf8.argtypes = [c_void_p, c_char_p , c_int ]
# for checking for the returned '-1' pointer in case opencc_convert() fails.
# c_char_p always tries to convert the returned (char *) to a Python string,
self.libopencc.opencc_convert_utf8.restype = c_void_p
self.libopencc.opencc_convert_utf8_free.argtypes = [c_char_p]
self.libopencc.opencc_close.argtypes = [c_void_p]
self.config = config
self.verbose = verbose
self.od = None
def __enter__(self):
if self.config is None:
self.od = self.libopencc.opencc_open(0)
else:
self.od = self.libopencc.opencc_open(c_char_p(self.config))
return self
def __exit__(self, type, value, traceback):
self.libopencc.opencc_close(self.od)
self.od = None
def __perror(self, message):
pass
def convert(self, text):
retv_c = self.libopencc.opencc_convert_utf8(self.od, text, len(text))
if retv_c == -1:
self.__perror('OpenCC error:')
raise ConvertError()
retv_c = cast(retv_c, c_char_p)
str_buffer = retv_c.value
self.libopencc.opencc_convert_utf8_free(retv_c);
return str_buffer
if __name__ == "__main__":
with sys.stdin as fp:
text = fp.read()
with OpenCC() as converter:
print converter.convert(text)