Skip to main content

Pure Python port of Will 保哥's C# OpenCC implementation.

Project description

opencc-py

CI PyPI License: MIT

opencc-py 是依照 Will 保哥的 C# OpenCC 實作移植的純 Python OpenCC 函式庫。核心行為保留原實作的內嵌詞庫、locale preset、Trie 最長匹配與多階段轉換流程。

功能

  • 純 Python 3.11+,無 runtime dependencies。
  • 內建 cnhktwtw2twpjp locale。
  • 支援 fullcn2tt2cn preset。
  • 支援自訂字典與多個字典群組串接。
  • Unicode code point 層級的 Trie 最長匹配。
  • XML-compatible HTML 轉換與還原。
  • 提供 opencc-py CLI。

安裝

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]contentimg altinput[type=button] value,並略過 scriptstyleignore-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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opencc_py_tw2-0.1.0.tar.gz (486.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opencc_py_tw2-0.1.0-py3-none-any.whl (482.9 kB view details)

Uploaded Python 3

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

Hashes for opencc_py_tw2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e87cda5490b9ff682d5ec7edcb2c77270ee193c857984604f24ffc3f4b163861
MD5 c487b2162954daacb69b0eed614caa09
BLAKE2b-256 f51ebeaa67eb2d1dc1781e569432a82887cf02451458a60f23a3a023a455779d

See more details on using hashes here.

Provenance

The following attestation bundles were made for opencc_py_tw2-0.1.0.tar.gz:

Publisher: publish.yml on doggy8088/opencc-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for opencc_py_tw2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ded5fe68f9462eeacdfe9a95ad4bc46e8881406d454d052cbbe5ec8c173bab8d
MD5 89cfd49f1366944ce9dc2d4ebe3e7745
BLAKE2b-256 cf048c18341baf5598410145a07ff446ff0347bc2dfc749cb5293ebb88fd8120

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page