Pure Python port of Will 保哥's C# OpenCC implementation.
Project description
opencc-py
opencc-py 是依照 Will 保哥的 C# OpenCC 實作移植的純 Python OpenCC 函式庫。核心行為保留原實作的內嵌詞庫、locale preset、Trie 最長匹配與多階段轉換流程。
功能
- 純 Python 3.11+,無 runtime dependencies。
- 內建
cn、hk、tw、tw2、twp、jplocale。 - 支援
full、cn2t、t2cnpreset。 - 支援自訂字典與多個字典群組串接。
- Unicode code point 層級的 Trie 最長匹配。
- XML-compatible HTML 轉換與還原。
- 提供
opencc-pyCLI。
安裝
pip install opencc-py-tw2
基本用法
from opencc_py import converter
convert = converter("cn", "tw2")
print(convert("汉语")) # 漢語
print(convert.convert("默认用户界面支持数据库和网络请求。"))
Locale 與 preset
預設 opencc_py.converter(from_, to) 使用 full preset:
| locale | 說明 |
|---|---|
cn |
中國大陸簡體 |
hk |
香港繁體異體字 |
tw |
台灣繁體異體字 |
tw2 |
台灣繁體常用詞 |
twp |
台灣繁體含 IT、姓名與其他詞彙 |
jp |
日本新字體/異體字 |
t |
passthrough,不載入該階段字典 |
方向限定 preset:
from opencc_py import presets
cn_to_tw = presets.cn2t.converter("cn", "tw2")
tw_to_cn = presets.t2cn.converter("tw2", "cn")
自訂字典
字串格式與 C# 實作相同:每筆 來源 目標,筆與筆之間以 | 分隔;若詞條內含空白,可用 tab 分隔來源與目標。
from opencc_py import DictEntry, custom_converter
convert = custom_converter("香蕉 banana|蘋果 apple|Web 平台庫\tWeb 平台函式庫")
print(convert("香蕉 蘋果 Web 平台庫"))
convert2 = custom_converter([
DictEntry("“", "「"),
DictEntry("”", "」"),
])
進階組合
converter_factory() 會依序套用每個 DictGroup,等同 C# 版本先處理 from 群組,再處理 to 群組。
from opencc_py import DictEntry, DictGroup, converter_factory
first = DictGroup.from_entries([DictEntry("a", "b")])
second = DictGroup.from_entries([DictEntry("b", "c")])
convert = converter_factory(first, second)
print(convert("a")) # c
XML-compatible HTML 轉換
此功能使用 Python 標準庫 xml.etree.ElementTree,適用於可被 XML parser 解析的 HTML/XML。它會轉換符合 lang 範圍內的文字、meta[name=description|keywords] 的 content、img alt、input[type=button] value,並略過 script、style 與 ignore-opencc class。
from opencc_py import HtmlConverter, converter
convert = converter("hk", "cn")
html = HtmlConverter.from_xml_string(
convert,
"<html lang='zh-HK'><body><p lang='zh-HK'>漢語</p></body></html>",
"zh-HK",
"zh-CN",
)
html.convert()
print(html.to_xml_string())
html.restore()
CLI
opencc-py input.txt cn tw2
opencc-py input.txt cn tw2 -o output.txt
opencc-py input.txt cn tw2 --in-place
-o/--output 與 -i/--in-place 不能同時使用。輸入與輸出皆使用 UTF-8。
開發
python -m venv .venv
source .venv/bin/activate
python -m unittest discover -s tests
python -m pip install build
python -m build
如果 C# OpenCC source 與本專案位於同一個父目錄,可重新產生內嵌詞庫:
python tools/generate_dict_data.py
python -m unittest discover -s tests
發布
PyPI 發布流程請見 PUBLISHING.md。
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opencc_py_tw2-0.1.0.tar.gz.
File metadata
- Download URL: opencc_py_tw2-0.1.0.tar.gz
- Upload date:
- Size: 486.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e87cda5490b9ff682d5ec7edcb2c77270ee193c857984604f24ffc3f4b163861
|
|
| MD5 |
c487b2162954daacb69b0eed614caa09
|
|
| BLAKE2b-256 |
f51ebeaa67eb2d1dc1781e569432a82887cf02451458a60f23a3a023a455779d
|
Provenance
The following attestation bundles were made for opencc_py_tw2-0.1.0.tar.gz:
Publisher:
publish.yml on doggy8088/opencc-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
opencc_py_tw2-0.1.0.tar.gz -
Subject digest:
e87cda5490b9ff682d5ec7edcb2c77270ee193c857984604f24ffc3f4b163861 - Sigstore transparency entry: 1548682230
- Sigstore integration time:
-
Permalink:
doggy8088/opencc-py@81424ffc310784845d88b56c1f3b08a54b855cb6 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/doggy8088
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@81424ffc310784845d88b56c1f3b08a54b855cb6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file opencc_py_tw2-0.1.0-py3-none-any.whl.
File metadata
- Download URL: opencc_py_tw2-0.1.0-py3-none-any.whl
- Upload date:
- Size: 482.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ded5fe68f9462eeacdfe9a95ad4bc46e8881406d454d052cbbe5ec8c173bab8d
|
|
| MD5 |
89cfd49f1366944ce9dc2d4ebe3e7745
|
|
| BLAKE2b-256 |
cf048c18341baf5598410145a07ff446ff0347bc2dfc749cb5293ebb88fd8120
|
Provenance
The following attestation bundles were made for opencc_py_tw2-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on doggy8088/opencc-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
opencc_py_tw2-0.1.0-py3-none-any.whl -
Subject digest:
ded5fe68f9462eeacdfe9a95ad4bc46e8881406d454d052cbbe5ec8c173bab8d - Sigstore transparency entry: 1548682255
- Sigstore integration time:
-
Permalink:
doggy8088/opencc-py@81424ffc310784845d88b56c1f3b08a54b855cb6 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/doggy8088
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@81424ffc310784845d88b56c1f3b08a54b855cb6 -
Trigger Event:
release
-
Statement type: