opencc-py-tw2 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opencc_py_tw2-0.1.0/.gitignore +10 -0
- opencc_py_tw2-0.1.0/CHANGELOG.md +6 -0
- opencc_py_tw2-0.1.0/LICENSE +21 -0
- opencc_py_tw2-0.1.0/PKG-INFO +163 -0
- opencc_py_tw2-0.1.0/README.md +139 -0
- opencc_py_tw2-0.1.0/pyproject.toml +57 -0
- opencc_py_tw2-0.1.0/src/opencc_py/__init__.py +40 -0
- opencc_py_tw2-0.1.0/src/opencc_py/cli.py +45 -0
- opencc_py_tw2-0.1.0/src/opencc_py/core.py +275 -0
- opencc_py_tw2-0.1.0/src/opencc_py/dict_data.py +46 -0
- opencc_py_tw2-0.1.0/src/opencc_py/html.py +120 -0
- opencc_py_tw2-0.1.0/src/opencc_py/locale.py +48 -0
- opencc_py_tw2-0.1.0/src/opencc_py/locale_data.py +137 -0
- opencc_py_tw2-0.1.0/src/opencc_py/presets/__init__.py +3 -0
- opencc_py_tw2-0.1.0/src/opencc_py/presets/cn2t.py +16 -0
- opencc_py_tw2-0.1.0/src/opencc_py/presets/full.py +16 -0
- opencc_py_tw2-0.1.0/src/opencc_py/presets/t2cn.py +16 -0
- opencc_py_tw2-0.1.0/tests/test_cli.py +59 -0
- opencc_py_tw2-0.1.0/tests/test_converter.py +156 -0
- opencc_py_tw2-0.1.0/tests/test_dict_data.py +58 -0
- opencc_py_tw2-0.1.0/tests/test_html.py +88 -0
- opencc_py_tw2-0.1.0/tests/test_presets_and_locale.py +30 -0
- opencc_py_tw2-0.1.0/tests/test_trie.py +50 -0
- opencc_py_tw2-0.1.0/tools/generate_dict_data.py +88 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Will 保哥
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: opencc-py-tw2
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pure Python port of Will 保哥's C# OpenCC implementation.
|
|
5
|
+
Project-URL: Homepage, https://github.com/doggy8088/opencc-py
|
|
6
|
+
Project-URL: Repository, https://github.com/doggy8088/opencc-py.git
|
|
7
|
+
Project-URL: Issues, https://github.com/doggy8088/opencc-py/issues
|
|
8
|
+
Author: Will 保哥
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: chinese,conversion,internationalization,opencc,simplified,traditional
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Natural Language :: Chinese (Simplified)
|
|
16
|
+
Classifier: Natural Language :: Chinese (Traditional)
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# opencc-py
|
|
26
|
+
|
|
27
|
+
[](https://github.com/doggy8088/opencc-py/actions/workflows/ci.yml)
|
|
28
|
+
[](https://pypi.org/project/opencc-py-tw2/)
|
|
29
|
+
[](LICENSE)
|
|
30
|
+
|
|
31
|
+
`opencc-py` 是依照 [Will 保哥的 C# OpenCC 實作](https://github.com/doggy8088/OpenCC)移植的純 Python OpenCC 函式庫。核心行為保留原實作的內嵌詞庫、locale preset、Trie 最長匹配與多階段轉換流程。
|
|
32
|
+
|
|
33
|
+
## 功能
|
|
34
|
+
|
|
35
|
+
- 純 Python 3.11+,無 runtime dependencies。
|
|
36
|
+
- 內建 `cn`、`hk`、`tw`、`tw2`、`twp`、`jp` locale。
|
|
37
|
+
- 支援 `full`、`cn2t`、`t2cn` preset。
|
|
38
|
+
- 支援自訂字典與多個字典群組串接。
|
|
39
|
+
- Unicode code point 層級的 Trie 最長匹配。
|
|
40
|
+
- XML-compatible HTML 轉換與還原。
|
|
41
|
+
- 提供 `opencc-py` CLI。
|
|
42
|
+
|
|
43
|
+
## 安裝
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install opencc-py-tw2
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 基本用法
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from opencc_py import converter
|
|
53
|
+
|
|
54
|
+
convert = converter("cn", "tw2")
|
|
55
|
+
print(convert("汉语")) # 漢語
|
|
56
|
+
print(convert.convert("默认用户界面支持数据库和网络请求。"))
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Locale 與 preset
|
|
60
|
+
|
|
61
|
+
預設 `opencc_py.converter(from_, to)` 使用 full preset:
|
|
62
|
+
|
|
63
|
+
| locale | 說明 |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `cn` | 中國大陸簡體 |
|
|
66
|
+
| `hk` | 香港繁體異體字 |
|
|
67
|
+
| `tw` | 台灣繁體異體字 |
|
|
68
|
+
| `tw2` | 台灣繁體常用詞 |
|
|
69
|
+
| `twp` | 台灣繁體含 IT、姓名與其他詞彙 |
|
|
70
|
+
| `jp` | 日本新字體/異體字 |
|
|
71
|
+
| `t` | passthrough,不載入該階段字典 |
|
|
72
|
+
|
|
73
|
+
方向限定 preset:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from opencc_py import presets
|
|
77
|
+
|
|
78
|
+
cn_to_tw = presets.cn2t.converter("cn", "tw2")
|
|
79
|
+
tw_to_cn = presets.t2cn.converter("tw2", "cn")
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## 自訂字典
|
|
83
|
+
|
|
84
|
+
字串格式與 C# 實作相同:每筆 `來源 目標`,筆與筆之間以 `|` 分隔;若詞條內含空白,可用 tab 分隔來源與目標。
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from opencc_py import DictEntry, custom_converter
|
|
88
|
+
|
|
89
|
+
convert = custom_converter("香蕉 banana|蘋果 apple|Web 平台庫\tWeb 平台函式庫")
|
|
90
|
+
print(convert("香蕉 蘋果 Web 平台庫"))
|
|
91
|
+
|
|
92
|
+
convert2 = custom_converter([
|
|
93
|
+
DictEntry("“", "「"),
|
|
94
|
+
DictEntry("”", "」"),
|
|
95
|
+
])
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## 進階組合
|
|
99
|
+
|
|
100
|
+
`converter_factory()` 會依序套用每個 `DictGroup`,等同 C# 版本先處理 `from` 群組,再處理 `to` 群組。
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from opencc_py import DictEntry, DictGroup, converter_factory
|
|
104
|
+
|
|
105
|
+
first = DictGroup.from_entries([DictEntry("a", "b")])
|
|
106
|
+
second = DictGroup.from_entries([DictEntry("b", "c")])
|
|
107
|
+
convert = converter_factory(first, second)
|
|
108
|
+
print(convert("a")) # c
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## XML-compatible HTML 轉換
|
|
112
|
+
|
|
113
|
+
此功能使用 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。
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from opencc_py import HtmlConverter, converter
|
|
117
|
+
|
|
118
|
+
convert = converter("hk", "cn")
|
|
119
|
+
html = HtmlConverter.from_xml_string(
|
|
120
|
+
convert,
|
|
121
|
+
"<html lang='zh-HK'><body><p lang='zh-HK'>漢語</p></body></html>",
|
|
122
|
+
"zh-HK",
|
|
123
|
+
"zh-CN",
|
|
124
|
+
)
|
|
125
|
+
html.convert()
|
|
126
|
+
print(html.to_xml_string())
|
|
127
|
+
html.restore()
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## CLI
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
opencc-py input.txt cn tw2
|
|
134
|
+
opencc-py input.txt cn tw2 -o output.txt
|
|
135
|
+
opencc-py input.txt cn tw2 --in-place
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`-o/--output` 與 `-i/--in-place` 不能同時使用。輸入與輸出皆使用 UTF-8。
|
|
139
|
+
|
|
140
|
+
## 開發
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
python -m venv .venv
|
|
144
|
+
source .venv/bin/activate
|
|
145
|
+
python -m unittest discover -s tests
|
|
146
|
+
python -m pip install build
|
|
147
|
+
python -m build
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
如果 C# OpenCC source 與本專案位於同一個父目錄,可重新產生內嵌詞庫:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
python tools/generate_dict_data.py
|
|
154
|
+
python -m unittest discover -s tests
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## 發布
|
|
158
|
+
|
|
159
|
+
PyPI 發布流程請見 [PUBLISHING.md](PUBLISHING.md)。
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
MIT
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# opencc-py
|
|
2
|
+
|
|
3
|
+
[](https://github.com/doggy8088/opencc-py/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/opencc-py-tw2/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
`opencc-py` 是依照 [Will 保哥的 C# OpenCC 實作](https://github.com/doggy8088/OpenCC)移植的純 Python OpenCC 函式庫。核心行為保留原實作的內嵌詞庫、locale preset、Trie 最長匹配與多階段轉換流程。
|
|
8
|
+
|
|
9
|
+
## 功能
|
|
10
|
+
|
|
11
|
+
- 純 Python 3.11+,無 runtime dependencies。
|
|
12
|
+
- 內建 `cn`、`hk`、`tw`、`tw2`、`twp`、`jp` locale。
|
|
13
|
+
- 支援 `full`、`cn2t`、`t2cn` preset。
|
|
14
|
+
- 支援自訂字典與多個字典群組串接。
|
|
15
|
+
- Unicode code point 層級的 Trie 最長匹配。
|
|
16
|
+
- XML-compatible HTML 轉換與還原。
|
|
17
|
+
- 提供 `opencc-py` CLI。
|
|
18
|
+
|
|
19
|
+
## 安裝
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install opencc-py-tw2
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 基本用法
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from opencc_py import converter
|
|
29
|
+
|
|
30
|
+
convert = converter("cn", "tw2")
|
|
31
|
+
print(convert("汉语")) # 漢語
|
|
32
|
+
print(convert.convert("默认用户界面支持数据库和网络请求。"))
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Locale 與 preset
|
|
36
|
+
|
|
37
|
+
預設 `opencc_py.converter(from_, to)` 使用 full preset:
|
|
38
|
+
|
|
39
|
+
| locale | 說明 |
|
|
40
|
+
| --- | --- |
|
|
41
|
+
| `cn` | 中國大陸簡體 |
|
|
42
|
+
| `hk` | 香港繁體異體字 |
|
|
43
|
+
| `tw` | 台灣繁體異體字 |
|
|
44
|
+
| `tw2` | 台灣繁體常用詞 |
|
|
45
|
+
| `twp` | 台灣繁體含 IT、姓名與其他詞彙 |
|
|
46
|
+
| `jp` | 日本新字體/異體字 |
|
|
47
|
+
| `t` | passthrough,不載入該階段字典 |
|
|
48
|
+
|
|
49
|
+
方向限定 preset:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from opencc_py import presets
|
|
53
|
+
|
|
54
|
+
cn_to_tw = presets.cn2t.converter("cn", "tw2")
|
|
55
|
+
tw_to_cn = presets.t2cn.converter("tw2", "cn")
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## 自訂字典
|
|
59
|
+
|
|
60
|
+
字串格式與 C# 實作相同:每筆 `來源 目標`,筆與筆之間以 `|` 分隔;若詞條內含空白,可用 tab 分隔來源與目標。
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from opencc_py import DictEntry, custom_converter
|
|
64
|
+
|
|
65
|
+
convert = custom_converter("香蕉 banana|蘋果 apple|Web 平台庫\tWeb 平台函式庫")
|
|
66
|
+
print(convert("香蕉 蘋果 Web 平台庫"))
|
|
67
|
+
|
|
68
|
+
convert2 = custom_converter([
|
|
69
|
+
DictEntry("“", "「"),
|
|
70
|
+
DictEntry("”", "」"),
|
|
71
|
+
])
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## 進階組合
|
|
75
|
+
|
|
76
|
+
`converter_factory()` 會依序套用每個 `DictGroup`,等同 C# 版本先處理 `from` 群組,再處理 `to` 群組。
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from opencc_py import DictEntry, DictGroup, converter_factory
|
|
80
|
+
|
|
81
|
+
first = DictGroup.from_entries([DictEntry("a", "b")])
|
|
82
|
+
second = DictGroup.from_entries([DictEntry("b", "c")])
|
|
83
|
+
convert = converter_factory(first, second)
|
|
84
|
+
print(convert("a")) # c
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## XML-compatible HTML 轉換
|
|
88
|
+
|
|
89
|
+
此功能使用 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。
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from opencc_py import HtmlConverter, converter
|
|
93
|
+
|
|
94
|
+
convert = converter("hk", "cn")
|
|
95
|
+
html = HtmlConverter.from_xml_string(
|
|
96
|
+
convert,
|
|
97
|
+
"<html lang='zh-HK'><body><p lang='zh-HK'>漢語</p></body></html>",
|
|
98
|
+
"zh-HK",
|
|
99
|
+
"zh-CN",
|
|
100
|
+
)
|
|
101
|
+
html.convert()
|
|
102
|
+
print(html.to_xml_string())
|
|
103
|
+
html.restore()
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## CLI
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
opencc-py input.txt cn tw2
|
|
110
|
+
opencc-py input.txt cn tw2 -o output.txt
|
|
111
|
+
opencc-py input.txt cn tw2 --in-place
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`-o/--output` 與 `-i/--in-place` 不能同時使用。輸入與輸出皆使用 UTF-8。
|
|
115
|
+
|
|
116
|
+
## 開發
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
python -m venv .venv
|
|
120
|
+
source .venv/bin/activate
|
|
121
|
+
python -m unittest discover -s tests
|
|
122
|
+
python -m pip install build
|
|
123
|
+
python -m build
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
如果 C# OpenCC source 與本專案位於同一個父目錄,可重新產生內嵌詞庫:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
python tools/generate_dict_data.py
|
|
130
|
+
python -m unittest discover -s tests
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## 發布
|
|
134
|
+
|
|
135
|
+
PyPI 發布流程請見 [PUBLISHING.md](PUBLISHING.md)。
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
MIT
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "opencc-py-tw2"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Pure Python port of Will 保哥's C# OpenCC implementation."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Will 保哥" }
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"opencc",
|
|
17
|
+
"chinese",
|
|
18
|
+
"conversion",
|
|
19
|
+
"traditional",
|
|
20
|
+
"simplified",
|
|
21
|
+
"internationalization"
|
|
22
|
+
]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Development Status :: 3 - Alpha",
|
|
25
|
+
"Intended Audience :: Developers",
|
|
26
|
+
"License :: OSI Approved :: MIT License",
|
|
27
|
+
"Natural Language :: Chinese (Simplified)",
|
|
28
|
+
"Natural Language :: Chinese (Traditional)",
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Programming Language :: Python :: 3.11",
|
|
31
|
+
"Programming Language :: Python :: 3.12",
|
|
32
|
+
"Programming Language :: Python :: 3.13",
|
|
33
|
+
"Topic :: Text Processing :: Linguistic"
|
|
34
|
+
]
|
|
35
|
+
dependencies = []
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/doggy8088/opencc-py"
|
|
39
|
+
Repository = "https://github.com/doggy8088/opencc-py.git"
|
|
40
|
+
Issues = "https://github.com/doggy8088/opencc-py/issues"
|
|
41
|
+
|
|
42
|
+
[project.scripts]
|
|
43
|
+
opencc-py = "opencc_py.cli:main"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = ["src/opencc_py"]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.sdist]
|
|
49
|
+
include = [
|
|
50
|
+
"/src",
|
|
51
|
+
"/tests",
|
|
52
|
+
"/tools",
|
|
53
|
+
"/README.md",
|
|
54
|
+
"/LICENSE",
|
|
55
|
+
"/CHANGELOG.md",
|
|
56
|
+
"/pyproject.toml"
|
|
57
|
+
]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Pure Python OpenCC converter."""
|
|
2
|
+
|
|
3
|
+
from . import locale, presets
|
|
4
|
+
from .core import (
|
|
5
|
+
Converter,
|
|
6
|
+
ConverterOptions,
|
|
7
|
+
Dict,
|
|
8
|
+
DictEntry,
|
|
9
|
+
DictGroup,
|
|
10
|
+
LocalePreset,
|
|
11
|
+
OpenCCError,
|
|
12
|
+
Trie,
|
|
13
|
+
converter,
|
|
14
|
+
converter_builder,
|
|
15
|
+
converter_factory,
|
|
16
|
+
converter_with_options,
|
|
17
|
+
custom_converter,
|
|
18
|
+
)
|
|
19
|
+
from .html import HTMLConverter, HtmlConverter, html_converter
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"Converter",
|
|
23
|
+
"ConverterOptions",
|
|
24
|
+
"Dict",
|
|
25
|
+
"DictEntry",
|
|
26
|
+
"DictGroup",
|
|
27
|
+
"HTMLConverter",
|
|
28
|
+
"HtmlConverter",
|
|
29
|
+
"LocalePreset",
|
|
30
|
+
"OpenCCError",
|
|
31
|
+
"Trie",
|
|
32
|
+
"converter",
|
|
33
|
+
"converter_builder",
|
|
34
|
+
"converter_factory",
|
|
35
|
+
"converter_with_options",
|
|
36
|
+
"custom_converter",
|
|
37
|
+
"html_converter",
|
|
38
|
+
"locale",
|
|
39
|
+
"presets",
|
|
40
|
+
]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .core import OpenCCError, converter
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main(argv: list[str] | None = None) -> int:
|
|
11
|
+
parser = argparse.ArgumentParser(
|
|
12
|
+
prog="opencc-py",
|
|
13
|
+
description="Convert Chinese text between OpenCC locales.",
|
|
14
|
+
)
|
|
15
|
+
parser.add_argument("file", help="Input UTF-8 text file")
|
|
16
|
+
parser.add_argument("from_locale", help="Source locale: cn, hk, tw, tw2, twp, jp, or t")
|
|
17
|
+
parser.add_argument("to_locale", help="Target locale: cn, hk, tw, tw2, twp, jp, or t")
|
|
18
|
+
output_group = parser.add_mutually_exclusive_group()
|
|
19
|
+
output_group.add_argument("-o", "--output", help="Output file path")
|
|
20
|
+
output_group.add_argument("-i", "--in-place", action="store_true", help="Overwrite the input file")
|
|
21
|
+
|
|
22
|
+
args = parser.parse_args(argv)
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
input_path = Path(args.file)
|
|
26
|
+
content = input_path.read_text(encoding="utf-8")
|
|
27
|
+
converted = converter(args.from_locale, args.to_locale).convert(content)
|
|
28
|
+
|
|
29
|
+
if args.output:
|
|
30
|
+
Path(args.output).write_text(converted, encoding="utf-8")
|
|
31
|
+
elif args.in_place:
|
|
32
|
+
input_path.write_text(converted, encoding="utf-8")
|
|
33
|
+
else:
|
|
34
|
+
sys.stdout.write(converted)
|
|
35
|
+
if converted and not converted.endswith("\n"):
|
|
36
|
+
sys.stdout.write("\n")
|
|
37
|
+
except (OSError, OpenCCError, UnicodeError) as exc:
|
|
38
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
39
|
+
return 1
|
|
40
|
+
|
|
41
|
+
return 0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if __name__ == "__main__":
|
|
45
|
+
raise SystemExit(main())
|