jieba3 1.0.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.
jieba3-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Shihong Yan
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.
jieba3-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,177 @@
1
+ Metadata-Version: 2.1
2
+ Name: jieba3
3
+ Version: 1.0.0
4
+ Summary: “结巴 3”中文分词:做最好的 Modern Python 3 中文分词组件
5
+ Keywords: Chinese,NLP,Analysis,Tokenizer
6
+ Author-email: Shihong Yan <yansh97@foxmail.com>
7
+ Requires-Python: >=3.10,<4
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: pydantic
10
+ Requires-Dist: isort ; extra == "dev"
11
+ Requires-Dist: black ; extra == "dev"
12
+ Requires-Dist: flit ; extra == "dev"
13
+ Project-URL: Home, https://github.com/yansh97/jieba3
14
+ Provides-Extra: dev
15
+
16
+ # jieba3
17
+
18
+ “结巴 3”中文分词:做最好的 Modern Python 3 中文分词组件
19
+
20
+ # 与 jieba 的区别
21
+
22
+ jieba3 是 [jieba](https://github.com/fxsjy/jieba) 分词模块的 Modern Python 3 重构版本
23
+
24
+ - 删除 Python 2 兼容代码,支持 type hints 等 Modern Python 3 特性
25
+ - 重构分词模块,在纯 Python 实现前提下,提高约 **20%** 的性能,且与 jieba 分词结果对齐
26
+ - 暂不支持除分词外的其他 jieba 功能,如关键词提取、词性标注等
27
+
28
+ # 安装说明
29
+
30
+ jieba3 仅支持 Python 3.10+ 版本
31
+
32
+ ```bash
33
+ pip install jieba3
34
+ ```
35
+
36
+ # 算法
37
+
38
+ - 基于前缀词典实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图 (DAG)
39
+ - 采用了动态规划查找最大概率路径, 找出基于词频的最大切分组合
40
+ - 对于未登录词,采用了基于汉字成词能力的 HMM 模型,使用了 Viterbi 算法
41
+
42
+ # 主要功能
43
+
44
+ 构建 `jieba3.jieba3` 分词器实例,支持以下参数:
45
+
46
+ - `model: Literal["base", "small", "large"] = "base"`
47
+ - 分词模型选项,可选值为 `small`、`base`、`large`,默认为 `base`
48
+ - `base` 模型是 jieba 提供的默认模型
49
+ - `small` 模型是 jieba 提供的占用内存较小的模型
50
+ - `large` 模型是 jieba 支持繁体分词更好的模型
51
+ - `use_hmm: bool = True`
52
+ - 是否开启 HMM 新词发现,可选值为 `True`、`False`,默认为 `True`
53
+
54
+ 示例如下:
55
+
56
+ ```python
57
+ import jieba3
58
+
59
+ tokenizer = jieba3.jieba3() # 默认为 base 模型,开启 HMM 新词发现
60
+ tokenizer = jieba3.jieba3(model="small") # 使用 small 模型
61
+ tokenizer = jieba3.jieba3(model="base") # 使用 base 模型
62
+ tokenizer = jieba3.jieba3(model="large") # 使用 large 模型
63
+ tokenizer = jieba3.jieba3(use_hmm=False) # 关闭 HMM 新词发现
64
+ tokenizer = jieba3.jieba3(use_hmm=True) # 开启 HMM 新词发现
65
+ ```
66
+
67
+ ## 文档模式
68
+
69
+ 试图将句子最精确地切开,适合文档分析
70
+
71
+ > 当使用默认的 `base` 模型时,jieba3 文档模式与 jieba 精确模式的分词结果完全一致
72
+
73
+ ```python
74
+ import jieba3
75
+ import jieba
76
+
77
+ # 开启 HMM 新词发现
78
+
79
+ tokenizer = jieba3.jieba3()
80
+ tokenizer.cut_text("小明硕士毕业于中国科学院计算所")
81
+ # ["小明", "硕士", "毕业", "于", "中国科学院", "计算所"]
82
+
83
+ jieba.lcut("小明硕士毕业于中国科学院计算所")
84
+ # ["小明", "硕士", "毕业", "于", "中国科学院", "计算所"]
85
+
86
+ # 关闭 HMM 新词发现
87
+
88
+ tokenizer = jieba3.jieba3(use_hmm=False)
89
+ tokenizer.cut_text("小明硕士毕业于中国科学院计算所")
90
+ # ["小", "明", "硕士", "毕业", "于", "中国科学院", "计算所"]
91
+
92
+ jieba.lcut("小明硕士毕业于中国科学院计算所", HMM=False)
93
+ # ["小", "明", "硕士", "毕业", "于", "中国科学院", "计算所"]
94
+ ```
95
+
96
+ ## 查询模式
97
+
98
+ 在文档模式的基础上,对长词再次切分,提高召回率,适合查询分析
99
+
100
+ > 当使用默认的 `base` 模型时,jieba3 查询模式与 jieba 搜索引擎模式的分词结果完全一致
101
+
102
+ ```python
103
+ import jieba3
104
+ import jieba
105
+
106
+ # 开启 HMM 新词发现
107
+
108
+ tokenizer = jieba3.jieba3()
109
+ tokenizer.cut_query("小明硕士毕业于中国科学院计算所")
110
+ # ["小明", "硕士", "毕业", "于", "中国", "科学", "学院", "科学院", "中国科学院", "计算", "计算所"]
111
+
112
+ jieba.lcut_for_search("小明硕士毕业于中国科学院计算所")
113
+ # ["小明", "硕士", "毕业", "于", "中国", "科学", "学院", "科学院", "中国科学院", "计算", "计算所"]
114
+
115
+ # 关闭 HMM 新词发现
116
+
117
+ tokenizer = jieba3.jieba3(use_hmm=False)
118
+ tokenizer.cut_query("小明硕士毕业于中国科学院计算所")
119
+ # ["小", "明", "硕士", "毕业", "于", "中国", "科学", "学院", "科学院", "中国科学院", "计算", "计算所"]
120
+
121
+ jieba.lcut_for_search("小明硕士毕业于中国科学院计算所", HMM=False)
122
+ # ["小", "明", "硕士", "毕业", "于", "中国", "科学", "学院", "科学院", "中国科学院", "计算", "计算所"]
123
+ ```
124
+
125
+ # 性能测试
126
+
127
+ jieba3 均使用默认的 `base` 模型,与 jieba 的默认模型对比
128
+
129
+ 测试环境:MacBookPro18,3,macOS 14.5,Apple M1 Pro @ 3.20 GHz,16 GB
130
+
131
+ ## SIGHAN Bakeoff 2005 测试集(逐行分词)
132
+
133
+ ### `as_test.utf8`(繁体)
134
+
135
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
136
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
137
+ | 文档模式(关闭 HMM) | 0.26 秒 | 2.28 MB/s | 0.20 秒 | 2.94 MB/s | 22% |
138
+ | 文档模式(开启 HMM) | 0.60 秒 | 0.98 MB/s | 0.48 秒 | 1.23 MB/s | 20% |
139
+ | 查询模式(关闭 HMM) | 0.27 秒 | 2.17 MB/s | 0.21 秒 | 2.79 MB/s | 22% |
140
+ | 查询模式(开启 HMM) | 0.63 秒 | 0.93 MB/s | 0.51 秒 | 1.15 MB/s | 20% |
141
+
142
+ ### `cityu_test.utf8`(繁体)
143
+
144
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
145
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
146
+ | 文档模式(关闭 HMM) | 0.09 秒 | 2.22 MB/s | 0.07 秒 | 2.87 MB/s | 23% |
147
+ | 文档模式(开启 HMM) | 0.21 秒 | 0.93 MB/s | 0.17 秒 | 1.16 MB/s | 20% |
148
+ | 查询模式(关闭 HMM) | 0.09 秒 | 2.11 MB/s | 0.07 秒 | 2.71 MB/s | 22% |
149
+ | 查询模式(开启 HMM) | 0.21 秒 | 0.90 MB/s | 0.17 秒 | 1.12 MB/s | 20% |
150
+
151
+ ### `msr_test.utf8`(简体)
152
+
153
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
154
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
155
+ | 文档模式(关闭 HMM) | 0.26 秒 | 2.06 MB/s | 0.20 秒 | 2.69 MB/s | 24% |
156
+ | 文档模式(开启 HMM) | 0.30 秒 | 1.79 MB/s | 0.24 秒 | 2.25 MB/s | 20% |
157
+ | 查询模式(关闭 HMM) | 0.28 秒 | 1.91 MB/s | 0.22 秒 | 2.47 MB/s | 23% |
158
+ | 查询模式(开启 HMM) | 0.32 秒 | 1.67 MB/s | 0.26 秒 | 2.08 MB/s | 20% |
159
+
160
+ ### `pku_test.utf8`(简体)
161
+
162
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
163
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
164
+ | 文档模式(关闭 HMM) | 0.25 秒 | 1.91 MB/s | 0.20 秒 | 2.48 MB/s | 23% |
165
+ | 文档模式(开启 HMM) | 0.30 秒 | 1.64 MB/s | 0.24 秒 | 2.04 MB/s | 20% |
166
+ | 查询模式(关闭 HMM) | 0.26 秒 | 1.85 MB/s | 0.20 秒 | 2.41 MB/s | 23% |
167
+ | 查询模式(开启 HMM) | 0.33 秒 | 1.48 MB/s | 0.27 秒 | 1.82 MB/s | 19% |
168
+
169
+ ## 《围城》(全文分词)
170
+
171
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
172
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
173
+ | 文档模式(关闭 HMM) | 0.35 秒 | 1.85 MB/s | 0.28 秒 | 2.32 MB/s | 20% |
174
+ | 文档模式(开启 HMM) | 0.51 秒 | 1.25 MB/s | 0.42 秒 | 1.52 MB/s | 18% |
175
+ | 查询模式(关闭 HMM) | 0.33 秒 | 1.93 MB/s | 0.26 秒 | 2.45 MB/s | 21% |
176
+ | 查询模式(开启 HMM) | 0.55 秒 | 1.17 MB/s | 0.45 秒 | 1.42 MB/s | 18% |
177
+
jieba3-1.0.0/README.md ADDED
@@ -0,0 +1,161 @@
1
+ # jieba3
2
+
3
+ “结巴 3”中文分词:做最好的 Modern Python 3 中文分词组件
4
+
5
+ # 与 jieba 的区别
6
+
7
+ jieba3 是 [jieba](https://github.com/fxsjy/jieba) 分词模块的 Modern Python 3 重构版本
8
+
9
+ - 删除 Python 2 兼容代码,支持 type hints 等 Modern Python 3 特性
10
+ - 重构分词模块,在纯 Python 实现前提下,提高约 **20%** 的性能,且与 jieba 分词结果对齐
11
+ - 暂不支持除分词外的其他 jieba 功能,如关键词提取、词性标注等
12
+
13
+ # 安装说明
14
+
15
+ jieba3 仅支持 Python 3.10+ 版本
16
+
17
+ ```bash
18
+ pip install jieba3
19
+ ```
20
+
21
+ # 算法
22
+
23
+ - 基于前缀词典实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图 (DAG)
24
+ - 采用了动态规划查找最大概率路径, 找出基于词频的最大切分组合
25
+ - 对于未登录词,采用了基于汉字成词能力的 HMM 模型,使用了 Viterbi 算法
26
+
27
+ # 主要功能
28
+
29
+ 构建 `jieba3.jieba3` 分词器实例,支持以下参数:
30
+
31
+ - `model: Literal["base", "small", "large"] = "base"`
32
+ - 分词模型选项,可选值为 `small`、`base`、`large`,默认为 `base`
33
+ - `base` 模型是 jieba 提供的默认模型
34
+ - `small` 模型是 jieba 提供的占用内存较小的模型
35
+ - `large` 模型是 jieba 支持繁体分词更好的模型
36
+ - `use_hmm: bool = True`
37
+ - 是否开启 HMM 新词发现,可选值为 `True`、`False`,默认为 `True`
38
+
39
+ 示例如下:
40
+
41
+ ```python
42
+ import jieba3
43
+
44
+ tokenizer = jieba3.jieba3() # 默认为 base 模型,开启 HMM 新词发现
45
+ tokenizer = jieba3.jieba3(model="small") # 使用 small 模型
46
+ tokenizer = jieba3.jieba3(model="base") # 使用 base 模型
47
+ tokenizer = jieba3.jieba3(model="large") # 使用 large 模型
48
+ tokenizer = jieba3.jieba3(use_hmm=False) # 关闭 HMM 新词发现
49
+ tokenizer = jieba3.jieba3(use_hmm=True) # 开启 HMM 新词发现
50
+ ```
51
+
52
+ ## 文档模式
53
+
54
+ 试图将句子最精确地切开,适合文档分析
55
+
56
+ > 当使用默认的 `base` 模型时,jieba3 文档模式与 jieba 精确模式的分词结果完全一致
57
+
58
+ ```python
59
+ import jieba3
60
+ import jieba
61
+
62
+ # 开启 HMM 新词发现
63
+
64
+ tokenizer = jieba3.jieba3()
65
+ tokenizer.cut_text("小明硕士毕业于中国科学院计算所")
66
+ # ["小明", "硕士", "毕业", "于", "中国科学院", "计算所"]
67
+
68
+ jieba.lcut("小明硕士毕业于中国科学院计算所")
69
+ # ["小明", "硕士", "毕业", "于", "中国科学院", "计算所"]
70
+
71
+ # 关闭 HMM 新词发现
72
+
73
+ tokenizer = jieba3.jieba3(use_hmm=False)
74
+ tokenizer.cut_text("小明硕士毕业于中国科学院计算所")
75
+ # ["小", "明", "硕士", "毕业", "于", "中国科学院", "计算所"]
76
+
77
+ jieba.lcut("小明硕士毕业于中国科学院计算所", HMM=False)
78
+ # ["小", "明", "硕士", "毕业", "于", "中国科学院", "计算所"]
79
+ ```
80
+
81
+ ## 查询模式
82
+
83
+ 在文档模式的基础上,对长词再次切分,提高召回率,适合查询分析
84
+
85
+ > 当使用默认的 `base` 模型时,jieba3 查询模式与 jieba 搜索引擎模式的分词结果完全一致
86
+
87
+ ```python
88
+ import jieba3
89
+ import jieba
90
+
91
+ # 开启 HMM 新词发现
92
+
93
+ tokenizer = jieba3.jieba3()
94
+ tokenizer.cut_query("小明硕士毕业于中国科学院计算所")
95
+ # ["小明", "硕士", "毕业", "于", "中国", "科学", "学院", "科学院", "中国科学院", "计算", "计算所"]
96
+
97
+ jieba.lcut_for_search("小明硕士毕业于中国科学院计算所")
98
+ # ["小明", "硕士", "毕业", "于", "中国", "科学", "学院", "科学院", "中国科学院", "计算", "计算所"]
99
+
100
+ # 关闭 HMM 新词发现
101
+
102
+ tokenizer = jieba3.jieba3(use_hmm=False)
103
+ tokenizer.cut_query("小明硕士毕业于中国科学院计算所")
104
+ # ["小", "明", "硕士", "毕业", "于", "中国", "科学", "学院", "科学院", "中国科学院", "计算", "计算所"]
105
+
106
+ jieba.lcut_for_search("小明硕士毕业于中国科学院计算所", HMM=False)
107
+ # ["小", "明", "硕士", "毕业", "于", "中国", "科学", "学院", "科学院", "中国科学院", "计算", "计算所"]
108
+ ```
109
+
110
+ # 性能测试
111
+
112
+ jieba3 均使用默认的 `base` 模型,与 jieba 的默认模型对比
113
+
114
+ 测试环境:MacBookPro18,3,macOS 14.5,Apple M1 Pro @ 3.20 GHz,16 GB
115
+
116
+ ## SIGHAN Bakeoff 2005 测试集(逐行分词)
117
+
118
+ ### `as_test.utf8`(繁体)
119
+
120
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
121
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
122
+ | 文档模式(关闭 HMM) | 0.26 秒 | 2.28 MB/s | 0.20 秒 | 2.94 MB/s | 22% |
123
+ | 文档模式(开启 HMM) | 0.60 秒 | 0.98 MB/s | 0.48 秒 | 1.23 MB/s | 20% |
124
+ | 查询模式(关闭 HMM) | 0.27 秒 | 2.17 MB/s | 0.21 秒 | 2.79 MB/s | 22% |
125
+ | 查询模式(开启 HMM) | 0.63 秒 | 0.93 MB/s | 0.51 秒 | 1.15 MB/s | 20% |
126
+
127
+ ### `cityu_test.utf8`(繁体)
128
+
129
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
130
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
131
+ | 文档模式(关闭 HMM) | 0.09 秒 | 2.22 MB/s | 0.07 秒 | 2.87 MB/s | 23% |
132
+ | 文档模式(开启 HMM) | 0.21 秒 | 0.93 MB/s | 0.17 秒 | 1.16 MB/s | 20% |
133
+ | 查询模式(关闭 HMM) | 0.09 秒 | 2.11 MB/s | 0.07 秒 | 2.71 MB/s | 22% |
134
+ | 查询模式(开启 HMM) | 0.21 秒 | 0.90 MB/s | 0.17 秒 | 1.12 MB/s | 20% |
135
+
136
+ ### `msr_test.utf8`(简体)
137
+
138
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
139
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
140
+ | 文档模式(关闭 HMM) | 0.26 秒 | 2.06 MB/s | 0.20 秒 | 2.69 MB/s | 24% |
141
+ | 文档模式(开启 HMM) | 0.30 秒 | 1.79 MB/s | 0.24 秒 | 2.25 MB/s | 20% |
142
+ | 查询模式(关闭 HMM) | 0.28 秒 | 1.91 MB/s | 0.22 秒 | 2.47 MB/s | 23% |
143
+ | 查询模式(开启 HMM) | 0.32 秒 | 1.67 MB/s | 0.26 秒 | 2.08 MB/s | 20% |
144
+
145
+ ### `pku_test.utf8`(简体)
146
+
147
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
148
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
149
+ | 文档模式(关闭 HMM) | 0.25 秒 | 1.91 MB/s | 0.20 秒 | 2.48 MB/s | 23% |
150
+ | 文档模式(开启 HMM) | 0.30 秒 | 1.64 MB/s | 0.24 秒 | 2.04 MB/s | 20% |
151
+ | 查询模式(关闭 HMM) | 0.26 秒 | 1.85 MB/s | 0.20 秒 | 2.41 MB/s | 23% |
152
+ | 查询模式(开启 HMM) | 0.33 秒 | 1.48 MB/s | 0.27 秒 | 1.82 MB/s | 19% |
153
+
154
+ ## 《围城》(全文分词)
155
+
156
+ | 模式 | jieba 耗时 | jieba 速度 | jieba3 耗时 | jieba3 速度 | 性能提升 |
157
+ | -------------------- | ---------- | ---------- | ----------- | ----------- | -------- |
158
+ | 文档模式(关闭 HMM) | 0.35 秒 | 1.85 MB/s | 0.28 秒 | 2.32 MB/s | 20% |
159
+ | 文档模式(开启 HMM) | 0.51 秒 | 1.25 MB/s | 0.42 秒 | 1.52 MB/s | 18% |
160
+ | 查询模式(关闭 HMM) | 0.33 秒 | 1.93 MB/s | 0.26 秒 | 2.45 MB/s | 21% |
161
+ | 查询模式(开启 HMM) | 0.55 秒 | 1.17 MB/s | 0.45 秒 | 1.42 MB/s | 18% |
@@ -0,0 +1,20 @@
1
+ """“结巴 3”中文分词:做最好的 Modern Python 3 中文分词组件"""
2
+
3
+ from typing import Literal
4
+
5
+ from pydantic import BaseModel
6
+
7
+ from jieba3.tok import _cut_query, _cut_text
8
+
9
+ __version__ = "1.0.0"
10
+
11
+
12
+ class jieba3(BaseModel):
13
+ model: Literal["base", "small", "large"] = "base"
14
+ use_hmm: bool = True
15
+
16
+ def cut_text(self, sentence: str, /) -> list[str]:
17
+ return list(_cut_text(sentence, model=self.model, use_hmm=self.use_hmm))
18
+
19
+ def cut_query(self, sentence: str, /) -> list[str]:
20
+ return list(_cut_query(sentence, model=self.model, use_hmm=self.use_hmm))