Open Chinese Convert  0.4.3
A project for conversion between Traditional and Simplified Chinese
 All Data Structures Files Functions Variables Groups Pages
opencc.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 from ctypes import cast, cdll, c_char_p, c_int, c_size_t, c_void_p
5 from ctypes.util import find_library
6 import sys
7 
8 class ConvertError(Exception):
9  pass
10 
11 class DictType:
12  TEXT,DATRIE = 0,1
13 
14 ## @defgroup python_api Python API
15 # API in python language
16 
17 ## OpenCC Python language binding
18 # @ingroup python_api
19 class OpenCC:
20 
21  ## Constructor
22  # @param self The object pointer.
23  # @param config Filename of config.
24  # @param verbose Specifies whether error information is printed.
25  # @ingroup python_api
26  def __init__(self, config=None, verbose=True):
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]
30  # for checking for the returned '-1' pointer in case opencc_convert() fails.
31  # c_char_p always tries to convert the returned (char *) to a Python string,
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]
36 
37  self.libc = cdll.LoadLibrary(find_library('c'))
38  self.libc.free.argtypes = [c_void_p]
39 
40  self.config = config
41  self.verbose = verbose
42  self.od = None
43 
44  ## @deprecated
45  def __enter__(self):
46  if self.config is None:
47  self.od = self.libopencc.opencc_open(0)
48  else:
49  self.od = self.libopencc.opencc_open(c_char_p(self.config))
50  return self
51 
52  ## @deprecated
53  def __exit__(self, type, value, traceback):
54  self.libopencc.opencc_close(self.od)
55  self.od = None
56 
57  def __perror(self, message):
58  if self.verbose:
59  self.libopencc.opencc_perror(message)
60 
61  ## Converts text.
62  # @param self The object pointer.
63  # @param text Input text.
64  # @return Converted text.
65  # @ingroup python_api
66  def convert(self, text):
67  retv_c = self.libopencc.opencc_convert_utf8(self.od, text, len(text))
68  if retv_c == -1:
69  self.__perror('OpenCC error:')
70  raise ConvertError()
71  retv_c = cast(retv_c, c_char_p)
72  str_buffer = retv_c.value
73  self.libc.free(retv_c);
74  return str_buffer
75 
76  ## @deprecated
77  def dict_load(self, filename, dicttype):
78  retv = self.libopencc.opencc_dict_load(self.od, filename, dicttype)
79  if retv == -1:
80  self.__perror('OpenCC error:')
81  return retv
82 
83 if __name__ == "__main__":
84  with sys.stdin as fp:
85  text = fp.read()
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)