| 1 |
import json |
| 2 |
import os |
| 3 |
import zipfile |
| 4 |
|
| 5 |
_this_dir = os.path.dirname(os.path.abspath(__file__)) |
| 6 |
_opencc_rootdir = os.path.abspath(os.path.join(_this_dir, '..', '..')) |
| 7 |
_testcases_path = os.path.join(_opencc_rootdir, 'test', 'testcases', 'testcases.json') |
| 8 |
|
| 9 |
_TEXT_ZIP_CONFIG = """{ |
| 10 |
"name": "Python Resource Zip Test", |
| 11 |
"segmentation": { |
| 12 |
"type": "mmseg", |
| 13 |
"dict": { |
| 14 |
"type": "text", |
| 15 |
"file": "STPhrases.txt" |
| 16 |
} |
| 17 |
}, |
| 18 |
"conversion_chain": [ |
| 19 |
{ |
| 20 |
"dict": { |
| 21 |
"type": "group", |
| 22 |
"dicts": [ |
| 23 |
{ |
| 24 |
"type": "text", |
| 25 |
"file": "STPhrases.txt" |
| 26 |
}, |
| 27 |
{ |
| 28 |
"type": "text", |
| 29 |
"file": "STCharacters.txt" |
| 30 |
} |
| 31 |
] |
| 32 |
} |
| 33 |
}, |
| 34 |
{ |
| 35 |
"dict": { |
| 36 |
"type": "text", |
| 37 |
"file": "TWPhrases.txt" |
| 38 |
} |
| 39 |
} |
| 40 |
] |
| 41 |
} |
| 42 |
""" |
| 43 |
|
| 44 |
|
| 45 |
def _write_resource_zip(path: str) -> None: |
| 46 |
with zipfile.ZipFile(path, 'w', compression=zipfile.ZIP_STORED) as archive: |
| 47 |
archive.writestr('s2twp.json', _TEXT_ZIP_CONFIG) |
| 48 |
archive.writestr('STPhrases.txt', "打印机\t印表機\n") |
| 49 |
archive.writestr('STCharacters.txt', "鼠\t鼠\n标\t標\n") |
| 50 |
archive.writestr('TWPhrases.txt', "鼠標\t滑鼠\n") |
| 51 |
|
| 52 |
|
| 53 |
def test_import(): |
| 54 |
import opencc |
| 55 |
|
| 56 |
|
| 57 |
def test_init_delete_converter(): |
| 58 |
import opencc |
| 59 |
|
| 60 |
for config in opencc.CONFIGS: |
| 61 |
converter = opencc.OpenCC(config) |
| 62 |
del converter |
| 63 |
|
| 64 |
|
| 65 |
def test_conversion(): |
| 66 |
import opencc |
| 67 |
import re |
| 68 |
from collections import defaultdict |
| 69 |
|
| 70 |
with open(_testcases_path, 'r', encoding='utf-8') as f: |
| 71 |
content = f.read() |
| 72 |
|
| 73 |
|
| 74 |
pattern = re.compile(r'"(?:[^"\\]|\\.)*"|(/\*[\s\S]*?\*/|//.*)|(,\s*(?=[\]}]))') |
| 75 |
clean_content = pattern.sub(lambda m: "" if (m.group(1) or m.group(2)) else m.group(0), content) |
| 76 |
parsed = json.loads(clean_content) |
| 77 |
|
| 78 |
|
| 79 |
config_cases = defaultdict(list) |
| 80 |
for case in parsed.get('cases', []): |
| 81 |
input_text = case.get('input') |
| 82 |
expected = case.get('expected', {}) |
| 83 |
if not input_text or not isinstance(expected, dict): |
| 84 |
continue |
| 85 |
for cfg, ans in expected.items(): |
| 86 |
config_cases[cfg].append((input_text, ans)) |
| 87 |
|
| 88 |
|
| 89 |
for cfg, cases in config_cases.items(): |
| 90 |
converter = opencc.OpenCC(f'{cfg}.json') |
| 91 |
for input_text, ans in cases: |
| 92 |
assert converter.convert(input_text) == ans, \ |
| 93 |
'Failed to convert {} for {} -> {}'.format(cfg, input_text, ans) |
| 94 |
|
| 95 |
|
| 96 |
def test_include_tofu_risk_dictionaries_option(): |
| 97 |
import opencc |
| 98 |
|
| 99 |
assert opencc.OpenCC('t2s.json').convert('㑮') == '𫝈' |
| 100 |
assert opencc.OpenCC( |
| 101 |
't2s.json', |
| 102 |
include_tofu_risk_dictionaries=False, |
| 103 |
).convert('㑮') == '㑮' |
| 104 |
|
| 105 |
|
| 106 |
def test_resource_zip_text_dictionary_support(tmp_path): |
| 107 |
import opencc |
| 108 |
|
| 109 |
resource_zip = tmp_path / 'opencc-resources.zip' |
| 110 |
_write_resource_zip(str(resource_zip)) |
| 111 |
converter = opencc.OpenCC( |
| 112 |
's2twp.json', |
| 113 |
resource_zip=str(resource_zip), |
| 114 |
) |
| 115 |
assert converter.convert('打印机和鼠标') == '印表機和滑鼠' |
| 116 |
|
| 117 |
|
| 118 |
def test_builtin_config_stem_is_normalized(): |
| 119 |
import opencc |
| 120 |
|
| 121 |
assert opencc.OpenCC('s2t').convert('汉字') == '漢字' |
| 122 |
|
| 123 |
|
| 124 |
def test_custom_config_path_without_json_extension_is_preserved(): |
| 125 |
import opencc |
| 126 |
|
| 127 |
assert opencc._normalize_config_name('custom-config') == 'custom-config' |
| 128 |
assert opencc._normalize_config_name('custom-config.jsonc') == 'custom-config.jsonc' |
| 129 |
assert opencc._normalize_config_name('./s2t') == './s2t' |
| 130 |
assert opencc._normalize_config_name('configs/s2t') == 'configs/s2t' |
| 131 |
assert opencc._normalize_config_name(r'configs\s2t') == r'configs\s2t' |