yanyiwu/cppjieba
"结巴"中文分词的C++版本
CppJieba – C++ implementation of the Jieba Chinese word‑segmentation library
What it is
- A C++ library that performs Chinese text segmentation (tokenisation), part‑of‑speech tagging and keyword extraction. It is a port of the popular Python
jiebalibrary.
Key features
- High performance – compiled C++ code, benchmarked for fast dictionary loading and tokenisation throughput.
- Multiple segmentation modes – precise, full, search‑engine and mixed (MP + HMM) modes.
- Custom dictionaries – users can load one or more user‑defined word lists (with optional frequency and POS tags) at runtime.
- UTF‑8 native – works directly with UTF‑8 encoded Chinese text.
- Cross‑platform – builds on Linux, macOS and Windows via CMake.
- Header‑only usage – just include the
cppjieba/*.hppfiles; no separate binary library is required.
How to get it
git clone https://github.com/yanyiwu/cppjieba.git
cd cppjieba
mkdir build && cd build
cmake ..
make # builds the library and demo
make test # runs unit tests
A benchmark target (make benchmark) measures dictionary load time, memory usage and tokenisation speed.
Typical usage
#include "cppjieba/Jieba.hpp"
int main(){
cppjieba::Jieba jieba("dict/jieba.dict.utf8",
"dict/hmm_model.utf8",
"dict/idf.utf8",
"dict/stop_words.utf8");
std::vector<std::string> words;
jieba.Cut("我来到北京清华大学", words, true); // true = use HMM
for (auto &w : words) std::cout << w << "/";
}
The demo (./demo) shows examples of:
- Cutting with/without HMM, full cut, search‑engine cut
- Inserting user words on the fly
- Returning word offsets for search‑engine mode
- POS tagging (
Tag) and keyword extraction (ExtractKeyword).
Ecosystem CppJieba is the core C++ engine behind many language bindings:
- Go (
gojieba), Node.js (nodejieba), C (cjieba), R (jiebaR), Erlang, Ruby, PHP, Perl, iOS, etc. - It is also used in projects such as a Chinese similarity‑hash tool, PostgreSQL full‑text search plugin, GitBook search plugin, Nginx module, and the OpenCC conversion suite.
When to use it
- When you need fast Chinese tokenisation in a C++ application or a service that can’t afford the overhead of a Python interpreter.
- When you want to integrate custom vocabularies (e.g., domain‑specific terms) without rebuilding the whole dictionary.
- When you need deterministic, offline processing (no external model server required).
Contribution The project welcomes bug reports, documentation improvements, and code contributions via pull requests.
Related
- Project
- Project
- Project
- Project