OpenCC made with Python
OpenCC-python-reimplemented is a pure Python implementation of the Open Chinese Convert (OpenCC) library. It facilitates conversions between Traditional and Simplified Chinese, supporting character-level and phrase-level conversion, as well as handling character variants and regional idioms among Mainland China, Taiwan, and Hong Kong. The current version is 0.1.7, released in February 2023, indicating an infrequent release cadence.
Warnings
- gotcha There are multiple Python packages related to OpenCC (e.g., `opencc`, `opencc-py`, and `opencc-python-reimplemented`). This specific package (`opencc-python-reimplemented`) is a pure Python implementation. Other packages might be C++ bindings, which can lead to `Segmentation Fault` errors if the underlying C library is not correctly installed or compatible with the Python binding. Ensure you install `opencc-python-reimplemented` if you want the pure Python version and its associated benefits.
- deprecated The project's 'Development Status' classifier is '3 - Alpha'. This indicates that the library is still in an early stage of development, may not be stable, and its API could change without strict adherence to semantic versioning, although no explicit breaking changes are documented between minor versions.
- gotcha The package `opencc-python` (not `opencc-python-reimplemented`) requires the OpenCC command-line tool to be installed separately, acting as a wrapper. `opencc-python-reimplemented` is a pure Python implementation and does not have this external dependency, avoiding potential setup complexities related to native binaries.
Install
-
pip install opencc-python-reimplemented
Imports
- OpenCC
from opencc import OpenCC
Quickstart
from opencc import OpenCC
cc = OpenCC('s2t') # Initialize converter: Simplified Chinese to Traditional Chinese
to_convert = '你好,世界!开放中文转换。'
converted_text = cc.convert(to_convert)
print(f"Original: {to_convert}")
print(f"Converted (s2t): {converted_text}")
# Example with another conversion mode (Traditional Chinese to Simplified Chinese)
cc_t2s = OpenCC('t2s')
to_convert_t = '你好,世界!開放中文轉換。'
converted_text_s = cc_t2s.convert(to_convert_t)
print(f"Original: {to_convert_t}")
print(f"Converted (t2s): {converted_text_s}")