Upload jieba3k/Test_Jieba3k.py with huggingface_hub
Browse files- jieba3k/Test_Jieba3k.py +132 -0
jieba3k/Test_Jieba3k.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
On-device verification for the jieba3k 0.35.1 wheel.
|
| 3 |
+
|
| 4 |
+
jieba3k is the Python-3 port of the jieba Chinese text segmentation library.
|
| 5 |
+
We verify imports, tokenizer configuration, basic cut() output for Chinese
|
| 6 |
+
and English text, and search-mode segmentation, all without any I/O.
|
| 7 |
+
|
| 8 |
+
Usage:
|
| 9 |
+
python Test_Jieba3k.py
|
| 10 |
+
|
| 11 |
+
Exit code 0 = everything required PASSed.
|
| 12 |
+
|
| 13 |
+
Generated by RIMI
|
| 14 |
+
"""
|
| 15 |
+
import sys
|
| 16 |
+
|
| 17 |
+
RESULTS = []
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class SkipTest(Exception):
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test(name, fn):
|
| 25 |
+
try:
|
| 26 |
+
fn()
|
| 27 |
+
RESULTS.append((name, "PASS", None))
|
| 28 |
+
print(" [PASS] %s" % name)
|
| 29 |
+
except SkipTest as exc:
|
| 30 |
+
RESULTS.append((name, "SKIP", str(exc)))
|
| 31 |
+
print(" [SKIP] %s: %s (optional)" % (name, exc))
|
| 32 |
+
except Exception as exc:
|
| 33 |
+
RESULTS.append((name, "FAIL", "%s: %s" % (type(exc).__name__, exc)))
|
| 34 |
+
print(" [FAIL] %s: %s" % (name, exc))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def section(title):
|
| 38 |
+
print("=" * 60)
|
| 39 |
+
print(" %s" % title)
|
| 40 |
+
print("=" * 60)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def eq(label, a, b):
|
| 44 |
+
if a != b:
|
| 45 |
+
raise AssertionError("%s: %r != %r" % (label, a, b))
|
| 46 |
+
return True
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def t_import():
|
| 50 |
+
import jieba
|
| 51 |
+
section("Core Import")
|
| 52 |
+
print(" jieba imported from:", jieba.__file__)
|
| 53 |
+
for attr in ("cut", "cut_for_search", "add_word", "load_userdict", "setLogLevel", "initialize"):
|
| 54 |
+
eq("has %s" % attr, True, hasattr(jieba, attr))
|
| 55 |
+
jieba.setLogLevel(60)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def t_cut_default():
|
| 59 |
+
import jieba
|
| 60 |
+
section("cut() default mode")
|
| 61 |
+
jieba.initialize()
|
| 62 |
+
words = list(jieba.cut("我来到北京清华大学"))
|
| 63 |
+
eq("returns tokens", len(words) > 0, True)
|
| 64 |
+
eq("joined text", "".join(words), "我来到北京清华大学")
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def t_cut_all():
|
| 68 |
+
import jieba
|
| 69 |
+
section("cut_all=True full mode")
|
| 70 |
+
jieba.setLogLevel(60)
|
| 71 |
+
words = list(jieba.cut("小明硕士毕业于中国科学院计算所", cut_all=True))
|
| 72 |
+
joined = "".join(words)
|
| 73 |
+
eq("returns tokens", len(words) > 0, True)
|
| 74 |
+
eq("contains 中国科学院", "中国科学院" in words, True)
|
| 75 |
+
eq("all tokens concatenate to source", joined, "小明硕士毕业于中国中国科学院科学科学院学院计算计算所")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def t_cut_english():
|
| 79 |
+
import jieba
|
| 80 |
+
section("cut() English")
|
| 81 |
+
words = list(jieba.cut("hello world python programming"))
|
| 82 |
+
eq("contains hello", "hello" in [w.strip() for w in words], True)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def t_cut_for_search():
|
| 86 |
+
import jieba
|
| 87 |
+
section("cut_for_search")
|
| 88 |
+
words = list(jieba.cut_for_search("小明硕士毕业于中国科学院计算所"))
|
| 89 |
+
joined = "".join(words)
|
| 90 |
+
eq("returns tokens", len(words) > 0, True)
|
| 91 |
+
eq("contains 中国科学院", "中国科学院" in words, True)
|
| 92 |
+
eq("search tokens concatenate to source", joined, "小明硕士毕业于中国科学学院科学院中国科学院计算计算所")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def main():
|
| 96 |
+
tests = [
|
| 97 |
+
("import + API", t_import),
|
| 98 |
+
("cut() default mode", t_cut_default),
|
| 99 |
+
("cut_all=True full mode", t_cut_all),
|
| 100 |
+
("cut() English", t_cut_english),
|
| 101 |
+
("cut_for_search", t_cut_for_search),
|
| 102 |
+
]
|
| 103 |
+
for name, fn in tests:
|
| 104 |
+
test(name, fn)
|
| 105 |
+
print()
|
| 106 |
+
print("=" * 60)
|
| 107 |
+
print(" SUMMARY")
|
| 108 |
+
print("=" * 60)
|
| 109 |
+
total = len(RESULTS)
|
| 110 |
+
fails = sum(1 for _, s, _ in RESULTS if s == "FAIL")
|
| 111 |
+
skips = sum(1 for _, s, _ in RESULTS if s == "SKIP")
|
| 112 |
+
passed = total - fails - skips
|
| 113 |
+
print(" Total: %d | Passed: %d | Skipped: %d | Failed: %d" % (total, passed, skips, fails))
|
| 114 |
+
skipped_items = [(n, m) for n, s, m in RESULTS if s == "SKIP"]
|
| 115 |
+
if skipped_items:
|
| 116 |
+
print()
|
| 117 |
+
print(" Skipped tests (optional):")
|
| 118 |
+
for n, m in skipped_items:
|
| 119 |
+
print(" - %s: %s" % (n, m))
|
| 120 |
+
print()
|
| 121 |
+
if fails == 0:
|
| 122 |
+
print(" ALL TESTS PASSED")
|
| 123 |
+
else:
|
| 124 |
+
print(" SOME TESTS FAILED")
|
| 125 |
+
for n, s, m in RESULTS:
|
| 126 |
+
if s == "FAIL":
|
| 127 |
+
print(" FAILED: %s (%s)" % (n, m))
|
| 128 |
+
sys.exit(1 if fails else 0)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
main()
|