Debian Codegraph @

plugins/jieba/deps/cppjieba/README.md

raw symbols SCIP
1 # CppJieba
2
3 [![CMake](https://github.com/yanyiwu/cppjieba/actions/workflows/cmake.yml/badge.svg)](https://github.com/yanyiwu/cppjieba/actions/workflows/cmake.yml)
4 [![Author](https://img.shields.io/badge/[email protected]?style=flat)](http://yanyiwu.com/)
5 [![Platform](https://img.shields.io/badge/platform-Linux,macOS,Windows-green.svg?style=flat)](https://github.com/yanyiwu/cppjieba)
6 [![Performance](https://img.shields.io/badge/performance-excellent-brightgreen.svg?style=flat)](http://yanyiwu.com/work/2015/06/14/jieba-series-performance-test.html)
7 [![Tag](https://img.shields.io/github/v/tag/yanyiwu/cppjieba.svg)](https://github.com/yanyiwu/cppjieba/releases)
8
9 ## 简介
10
11 CppJieba是"结巴(Jieba)"中文分词的C++版本
12
13 ### 主要特点
14
15 - 🚀 高性能:经过线上环境验证的稳定性和性能表现
16 - 📦 易集成:源代码以头文件形式提供 (`include/cppjieba/*.hpp`),包含即可使用
17 - 🔍 多种分词模式:支持精确模式、全模式、搜索引擎模式等
18 - 📚 自定义词典:支持用户自定义词典,支持多词典路径(使用'|'或';'分隔)
19 - 💻 跨平台:支持 Linux、macOS、Windows 操作系统
20 - 🌈 UTF-8编码:原生支持 UTF-8 编码的中文处理
21
22 ## 快速开始
23
24 ### 环境要求
25
26 - C++ 编译器:
27 - g++ (推荐 4.1 以上版本)
28 - 或 clang++
29 - cmake (推荐 2.6 以上版本)
30
31 ### 安装步骤
32
33 ```sh
34 git clone https://github.com/yanyiwu/cppjieba.git
35 cd cppjieba
36 mkdir build
37 cd build
38 cmake ..
39 make
40
41 make test
42 ```
43
44 ### Benchmark
45
46 项目提供了一个用于本地性能对比的 benchmark 目标:
47
48 ```sh
49 make benchmark
50 ```
51
52 它会构建并运行 `test/benchmark.cpp`,输出以下指标:
53
54 - `DictTrieLoad`: 词典加载耗时,以及可用时的进程 RSS 内存占用
55 - `HMMModelLoad`: HMM 模型加载耗时,以及可用时的进程 RSS 内存占用
56 - `MPCut`: `MPSegment` 对基准文本的分词吞吐
57 - `MixCut`: `MixSegment` 对基准文本的分词吞吐
58 - `DictFind`: 词典查找吞吐
59
60 这个 benchmark 主要用于本地修改前后的性能回归和对比,不作为默认测试的一部分。
61
62 ## 使用示例
63
64 ```
65 ./demo
66 ```
67
68 结果示例:
69
70 ```
71 [demo] Cut With HMM
72 他/来到/了/网易/杭研/大厦
73 [demo] Cut Without HMM
74 他/来到/了/网易/杭/研/大厦
75 我来到北京清华大学
76 [demo] CutAll
77 我/来到/北京/清华/清华大学/华大/大学
78 小明硕士毕业于中国科学院计算所,后在日本京都大学深造
79 [demo] CutForSearch
80 小明/硕士/毕业/于/中国/科学/学院/科学院/中国科学院/计算/计算所/,/后/在/日本/京都/大学/日本京都大学/深造
81 [demo] Insert User Word
82 男默/女泪
83 男默女泪
84 [demo] CutForSearch Word With Offset
85 [{"word": "小明", "offset": 0}, {"word": "硕士", "offset": 6}, {"word": "毕业", "offset": 12}, {"word": "于", "offset": 18}, {"word": "中国", "offset": 21}, {"word": "科学", "offset": 27}, {"word": "学院", "offset": 30}, {"word": "科学院", "offset": 27}, {"word": "中国科学院", "offset": 21}, {"word": "计算", "offset": 36}, {"word": "计算所", "offset": 36}, {"word": ",", "offset": 45}, {"word": "后", "offset": 48}, {"word": "在", "offset": 51}, {"word": "日本", "offset": 54}, {"word": "京都", "offset": 60}, {"word": "大学", "offset": 66}, {"word": "日本京都大学", "offset": 54}, {"word": "深造", "offset": 72}]
86 [demo] Tagging
87 我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。
88 [我:r, 是:v, 拖拉机:n, 学院:n, 手扶拖拉机:n, 专业:n, 的:uj, 。:x, 不用:v, 多久:m, ,:x, 我:r, 就:d, 会:v, 升职:v, 加薪:nr, ,:x, 当上:t, CEO:eng, ,:x, 走上:v, 人生:n, 巅峰:n, 。:x]
89 [demo] Keyword Extraction
90 我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。
91 [{"word": "CEO", "offset": [93], "weight": 11.7392}, {"word": "升职", "offset": [72], "weight": 10.8562}, {"word": "加薪", "offset": [78], "weight": 10.6426}, {"word": "手扶拖拉机", "offset": [21], "weight": 10.0089}, {"word": "巅峰", "offset": [111], "weight": 9.49396}]
92 ```
93
94 For more details, please see [demo](https://github.com/yanyiwu/cppjieba-demo).
95
96 ### 分词结果示例
97
98 **MPSegment**
99
100 Output:
101 ```
102 我来到北京清华大学
103 我/来到/北京/清华大学
104
105 他来到了网易杭研大厦
106 他/来到/了/网易/杭/研/大厦
107
108 小明硕士毕业于中国科学院计算所,后在日本京都大学深造
109 小/明/硕士/毕业/于/中国科学院/计算所/,/后/在/日本京都大学/深造
110
111 ```
112
113 **HMMSegment**
114
115 ```
116 我来到北京清华大学
117 我来/到/北京/清华大学
118
119 他来到了网易杭研大厦
120 他来/到/了/网易/杭/研大厦
121
122 小明硕士毕业于中国科学院计算所,后在日本京都大学深造
123 小明/硕士/毕业于/中国/科学院/计算所/,/后/在/日/本/京/都/大/学/深/造
124
125 ```
126
127 **MixSegment**
128
129 ```
130 我来到北京清华大学
131 我/来到/北京/清华大学
132
133 他来到了网易杭研大厦
134 他/来到/了/网易/杭研/大厦
135
136 小明硕士毕业于中国科学院计算所,后在日本京都大学深造
137 小明/硕士/毕业/于/中国科学院/计算所/,/后/在/日本京都大学/深造
138
139 ```
140
141 **FullSegment**
142
143 ```
144 我来到北京清华大学
145 我/来到/北京/清华/清华大学/华大/大学
146
147 他来到了网易杭研大厦
148 他/来到/了/网易/杭/研/大厦
149
150 小明硕士毕业于中国科学院计算所,后在日本京都大学深造
151 小/明/硕士/毕业/于/中国/中国科学院/科学/科学院/学院/计算/计算所/,/后/在/日本/日本京都大学/京都/京都大学/大学/深造
152
153 ```
154
155 **QuerySegment**
156
157 ```
158 我来到北京清华大学
159 我/来到/北京/清华/清华大学/华大/大学
160
161 他来到了网易杭研大厦
162 他/来到/了/网易/杭研/大厦
163
164 小明硕士毕业于中国科学院计算所,后在日本京都大学深造
165 小明/硕士/毕业/于/中国/中国科学院/科学/科学院/学院/计算所/,/后/在/中国/中国科学院/科学/科学院/学院/日本/日本京都大学/京都/京都大学/大学/深造
166
167 ```
168
169 以上依次是MP,HMM,Mix三种方法的效果。
170
171 可以看出效果最好的是Mix,也就是融合MP和HMM的切词算法。即可以准确切出词典已有的词,又可以切出像"杭研"这样的未登录词。
172
173 Full方法切出所有字典里的词语。
174
175 Query方法先使用Mix方法切词,对于切出来的较长的词再使用Full方法。
176
177 ### 自定义用户词典
178
179 自定义词典示例请看`dict/user.dict.utf8`。
180
181 用户词典支持以下三种格式,每行一条,列之间用空格分隔:
182
183 ```text
184 词语
185 词语 词性
186 词语 词频 词性
187 ```
188
189 - `1` 列: 只提供词语,词频使用默认值,词性为空。
190 - `2` 列: 当前解释为 `词语 词性`,词频仍使用默认值。
191 - `3` 列: 解释为 `词语 词频 词性`。
192 - 多个用户词典文件可以用 `|` 或 `;` 连接后一起传入。
193 - 不支持行内注释或额外的列。
194
195 主词典 `dict/jieba.dict.utf8` 的格式固定为三列:
196
197 ```text
198 词语 词频 词性
199 ```
200
201 其中:
202
203 - `词频` 在加载时会被转换成对数权重,供概率分词使用。
204 - 用户词典如果未显式提供词频,会使用主词典统计出来的默认权重;默认策略是中位数权重。
205 - `词性` 在代码中只是原样保存的字符串,没有内置枚举校验;主词典当前实际使用了 `55` 种 tag,例如 `n`、`v`、`nr`、`ns`、`nz`、`x`、`eng` 等。
206
207 没有使用自定义用户词典时的结果:
208
209 ```
210 令狐冲/是/云/计算/行业/的/专家
211 ```
212
213 使用自定义用户词典时的结果:
214
215 ```
216 令狐冲/是/云计算/行业/的/专家
217 ```
218
219 ### 关键词抽取
220
221 ```
222 我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。
223 ["CEO:11.7392", "升职:10.8562", "加薪:10.6426", "手扶拖拉机:10.0089", "巅峰:9.49396"]
224 ```
225
226 For more details, please see [demo](https://github.com/yanyiwu/cppjieba-demo).
227
228 ### 词性标注
229
230 ```
231 我是蓝翔技工拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰。
232 ["我:r", "是:v", "拖拉机:n", "学院:n", "手扶拖拉机:n", "专业:n", "的:uj", "。:x", "不用:v", "多久:m", ",:x", "我:r", "就:d", "会:v", "升职:v", "加薪:nr", ",:x", "当上:t", "CEO:eng", ",:x", "走上:v", "人生:n", "巅峰:n", "。:x"]
233 ```
234
235 For more details, please see [demo](https://github.com/yanyiwu/cppjieba-demo).
236
237 支持自定义词性。
238 比如在(`dict/user.dict.utf8`)增加一行
239
240 ```
241 蓝翔 nz
242 ```
243
244 结果如下:
245
246 ```
247 ["我:r", "是:v", "蓝翔:nz", "技工:n", "拖拉机:n", "学院:n", "手扶拖拉机:n", "专业:n", "的:uj", "。:x", "不用:v", "多久:m", ",:x", "我:r", "就:d", "会:v", "升职:v", "加薪:nr", ",:x", "当:t", "上:f", "总经理:n", ",:x", "出任:v", "CEO:eng", ",:x", "迎娶:v", "白富美:x", ",:x", "走上:v", "人生:n", "巅峰:n", "。:x"]
248 ```
249
250 ## 其它词典资料分享
251
252 + [dict.367W.utf8] iLife(562193561 at qq.com)
253
254 ## 生态系统
255
256 CppJieba 已经被广泛应用于各种编程语言的分词实现中:
257
258 - [GoJieba](https://github.com/yanyiwu/gojieba) - Go 语言版本
259 - [NodeJieba](https://github.com/yanyiwu/nodejieba) - Node.js 版本
260 - [CJieba](https://github.com/yanyiwu/cjieba) - C 语言版本
261 - [jiebaR](https://github.com/qinwf/jiebaR) - R 语言版本
262 - [exjieba](https://github.com/falood/exjieba) - Erlang 版本
263 - [jieba_rb](https://github.com/altkatz/jieba_rb) - Ruby 版本
264 - [iosjieba](https://github.com/yanyiwu/iosjieba) - iOS 版本
265 - [phpjieba](https://github.com/jonnywang/phpjieba) - PHP 版本
266 - [perl5-jieba](https://metacpan.org/pod/distribution/Lingua-ZH-Jieba/lib/Lingua/ZH/Jieba.pod) - Perl 版本
267
268 ### 应用项目
269
270 - [simhash](https://github.com/yanyiwu/simhash) - 中文文档相似度计算
271 - [pg_jieba](https://github.com/jaiminpan/pg_jieba) - PostgreSQL 分词插件
272 - [gitbook-plugin-search-pro](https://plugins.gitbook.com/plugin/search-pro) - Gitbook 中文搜索插件
273 - [ngx_http_cppjieba_module](https://github.com/yanyiwu/ngx_http_cppjieba_module) - Nginx 分词插件
274 - [OpenCC](https://github.com/byvoid/OpenCC) - OpenCC 中的 jieba 分词插件
275
276 ## 贡献指南
277
278 我们欢迎各种形式的贡献,包括但不限于:
279
280 - 提交问题和建议
281 - 改进文档
282 - 提交代码修复
283 - 添加新功能
284
285
286 如果您觉得 CppJieba 对您有帮助,欢迎 star ⭐️ 支持项目!