Open Chinese Convert  0.4.3
A project for conversion between Traditional and Simplified Chinese
/usr/src/RPM/BUILD/opencc-0.4.3/src/wrapper/python/opencc.py
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 from ctypes import cast, cdll, c_char_p, c_int, c_size_t, c_void_p
00005 from ctypes.util import find_library
00006 import sys
00007 
00008 class ConvertError(Exception):
00009   pass
00010 
00011 class DictType:
00012   TEXT,DATRIE = 0,1
00013 
00014 ## @defgroup python_api Python API
00015 #  API in python language
00016 
00017 ## OpenCC Python language binding
00018 #  @ingroup python_api
00019 class OpenCC:
00020 
00021   ## Constructor
00022   #  @param self The object pointer.
00023   #  @param config Filename of config.
00024   #  @param verbose Specifies whether error information is printed.
00025   #  @ingroup python_api
00026   def __init__(self, config=None, verbose=True):
00027     self.libopencc = cdll.LoadLibrary(find_library('opencc'))
00028     self.libopencc.opencc_open.restype = c_void_p
00029     self.libopencc.opencc_convert_utf8.argtypes = [c_void_p, c_char_p, c_size_t]
00030     # for checking for the returned '-1' pointer in case opencc_convert() fails.
00031     # c_char_p always tries to convert the returned (char *) to a Python string,
00032     self.libopencc.opencc_convert_utf8.restype = c_void_p
00033     self.libopencc.opencc_close.argtypes = [c_void_p]
00034     self.libopencc.opencc_perror.argtypes = [c_char_p]
00035     self.libopencc.opencc_dict_load.argtypes = [c_void_p, c_char_p, c_int]
00036 
00037     self.libc = cdll.LoadLibrary(find_library('c'))
00038     self.libc.free.argtypes = [c_void_p]
00039 
00040     self.config = config
00041     self.verbose = verbose
00042     self.od = None
00043   
00044   ## @deprecated
00045   def __enter__(self):
00046     if self.config is None:
00047       self.od = self.libopencc.opencc_open(0)
00048     else:
00049       self.od = self.libopencc.opencc_open(c_char_p(self.config))
00050     return self
00051 
00052   ## @deprecated
00053   def __exit__(self, type, value, traceback):
00054     self.libopencc.opencc_close(self.od)
00055     self.od = None
00056 
00057   def __perror(self, message):
00058     if self.verbose:
00059       self.libopencc.opencc_perror(message)
00060 
00061   ## Converts text.
00062   #  @param self The object pointer.
00063   #  @param text Input text.
00064   #  @return Converted text.
00065   #  @ingroup python_api
00066   def convert(self, text):
00067     retv_c = self.libopencc.opencc_convert_utf8(self.od, text, len(text))
00068     if retv_c == -1:
00069       self.__perror('OpenCC error:')
00070       raise ConvertError()
00071     retv_c = cast(retv_c, c_char_p)
00072     str_buffer = retv_c.value
00073     self.libc.free(retv_c);
00074     return str_buffer
00075   
00076   ## @deprecated
00077   def dict_load(self, filename, dicttype):
00078     retv = self.libopencc.opencc_dict_load(self.od, filename, dicttype)
00079     if retv == -1:
00080       self.__perror('OpenCC error:')
00081     return retv
00082 
00083 if __name__ == "__main__":
00084   with sys.stdin as fp:
00085     text = fp.read()
00086   with OpenCC() as converter:
00087     for path in ['simp_to_trad_characters.ocd',
00088            'simp_to_trad_phrases.ocd']:
00089       converter.dict_load(path, DictType.DATRIE)
00090     print converter.convert(text)
 All Data Structures Files Functions Variables Defines