C++:jieba库的安装使用保姆级教程
文章目录
- 一、jieba是什么?
- 二、安装步骤
- 1.引入库
- 2.进入库
- 3.初始化config
- 4.下载子模块
- 5.编译可用库
- 三、使用
一、jieba是什么?
在官方的jieba
的解释里面可能有些不足,因为如果是小白可能会在安装的时候报错不知道怎么做,所以写下这篇文章针对官方md
解释对小白理解的不足
下面如果内容有不理解或者错误,请各位帅哥,美女,大佬,大神该骂就骂,(๑•̀ㅂ•́)و✧努力中!
C++
的 jieba
,其实是 中文分词库 Jieba
的 C++
版本,就是其它开发者,给C++
开发的一个分词库,能将一段句子的词组分开,适合做搜索引擎,我们需要将它的库下载到本地,然后include
引入头文件直接使用它给我们提供的接口
二、安装步骤
1.引入库
git clone https://github.com/yanyiwu/cppjieba.git
在github
中将资源拉取下来
root@hcss-ecs-f59a:/gch/test# git clone https://github.com/yanyiwu/cppjieba.git
Cloning into 'cppjieba'...
remote: Enumerating objects: 5906, done.
remote: Counting objects: 100% (139/139), done.
remote: Compressing objects: 100% (82/82), done.
remote: Total 5906 (delta 72), reused 57 (delta 57), pack-reused 5767 (from 4)
Receiving objects: 100% (5906/5906), 11.27 MiB | 11.30 MiB/s, done.
Resolving deltas: 100% (3518/3518), done.
root@hcss-ecs-f59a:/gch/test# ll
total 12
drwxr-xr-x 3 root root 4096 Oct 19 12:38 ./
drwxr-xr-x 7 root root 4096 Oct 19 12:38 ../
drwxr-xr-x 8 root root 4096 Oct 19 12:38 cppjieba/
我们现在看到了我们有一个cppjieba
的目录,这个目录就是jieba
的源代码
2.进入库
cd cppjieba
我们需要进入项目文件中,因为使用这个项目我们还需要安装一个子模块,相当于我们下载的cppjieba
库也需要下载一个其它的库,而我们的cppjieba
里面记载了这个库的信息
root@hcss-ecs-f59a:/gch/test# cd cppjieba
root@hcss-ecs-f59a:/gch/test/cppjieba# ll
total 72
drwxr-xr-x 8 root root 4096 Oct 19 12:38 ./
drwxr-xr-x 3 root root 4096 Oct 19 12:38 ../
-rw-r--r-- 1 root root 11832 Oct 19 12:38 CHANGELOG.md
-rw-r--r-- 1 root root 1412 Oct 19 12:38 CMakeLists.txt
drwxr-xr-x 3 root root 4096 Oct 19 12:38 deps/
drwxr-xr-x 3 root root 4096 Oct 19 12:38 dict/
drwxr-xr-x 8 root root 4096 Oct 19 12:38 .git/
drwxr-xr-x 3 root root 4096 Oct 19 12:38 .github/
-rw-r--r-- 1 root root 165 Oct 19 12:38 .gitignore
-rw-r--r-- 1 root root 91 Oct 19 12:38 .gitmodules
drwxr-xr-x 3 root root 4096 Oct 19 12:38 include/
-rw-r--r-- 1 root root 1066 Oct 19 12:38 LICENSE
-rw-r--r-- 1 root root 8975 Oct 19 12:38 README.md
drwxr-xr-x 4 root root 4096 Oct 19 12:38 test/
root@hcss-ecs-f59a:/gch/test/cppjieba# cat .gitmodules
[submodule "deps/limonp"]path = deps/limonpurl = https://github.com/yanyiwu/limonp.git
这里的这个.gitmoduls
记载了limonp
库的安装位置path
和安装地址url
3.初始化config
git submodule init
git submodule init
会将.git
的配置进行初始化,会把 .gitmodules
文件中子模块的配置信息写入本地 .git/config
后续就可以直接将库拉取下来
root@hcss-ecs-f59a:/gch/test/cppjieba# cat .git/config
[core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = true
[remote "origin"]url = https://github.com/yanyiwu/cppjieba.gitfetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]remote = originmerge = refs/heads/master
[submodule "deps/limonp"]active = trueurl = https://github.com/yanyiwu/limonp.git
这里的配置已经写进.git/config
中了,所以git submodule init
的功能就是将.gitmodules
的内容写进.git/config
中
submodule "deps/limonp"
:说明仓库中有一个子模块,路径为 deps/limonp
。
active = true
:表示子模块是激活状态,会被初始化和更新。
url
:子模块的远程仓库地址,即 limonp
的 GitHub
链接
4.下载子模块
git submodule update
git submodule update
就是将.git/config
的子模块配置下载下来
root@hcss-ecs-f59a:/gch/test/cppjieba/deps/limonp# ll
total 48
drwxr-xr-x 5 root root 4096 Oct 19 13:08 ./
drwxr-xr-x 3 root root 4096 Oct 19 12:38 ../
-rw-r--r-- 1 root root 4819 Oct 19 13:08 CHANGELOG.md
-rw-r--r-- 1 root root 1347 Oct 19 13:08 CMakeLists.txt
-rw-r--r-- 1 root root 39 Oct 19 13:08 .git
drwxr-xr-x 3 root root 4096 Oct 19 13:08 .github/
-rw-r--r-- 1 root root 50 Oct 19 13:08 .gitignore
-rw-r--r-- 1 root root 0 Oct 19 13:08 .gitmodules
drwxr-xr-x 3 root root 4096 Oct 19 13:08 include/
-rw-r--r-- 1 root root 1066 Oct 19 13:08 LICENSE
-rw-r--r-- 1 root root 1122 Oct 19 13:08 README.md
drwxr-xr-x 4 root root 4096 Oct 19 13:08 test/
root@hcss-ecs-f59a:/gch/test/cppjieba/deps/limonp#
这里的limonp
库的配置已经被下载到/gch/test/cppjieba/deps/limonp
目录中
5.编译可用库
在cppjieba
目录中执行
mkdir build
cd build
cmake .. -Dlimonp_DIR=../deps/limonp
make
make test
cppjieba
被编译成可用库,你可以在 C++
项目中直接使用中文分词功能
make test
用来测试库是否可以使用
root@hcss-ecs-f59a:/gch/boost/text/cppjieba/build# make test
Running tests...
Test project /gch/boost/text/cppjieba/buildStart 1: ./test/test.run
1/2 Test #1: ./test/test.run .................. Passed 6.00 secStart 2: ./load_test
2/2 Test #2: ./load_test ...................... Passed 4.06 sec100% tests passed, 0 tests failed out of 2Total Test time (real) = 10.07 sec
出现这些信息说明我们的库可以正常使用了
三、使用
在cppjieba
文件下创建一个文件demo.cpp
#include "cppjieba/Jieba.hpp"
#include <iostream>
#include <vector>using namespace std;int main() {const string dict_path = "dict/jieba.dict.utf8";const string hmm_path = "dict/hmm_model.utf8";const string user_dict_path = "dict/user.dict.utf8";const string idf_path = "dict/idf.utf8";const string stop_words_path = "dict/stop_words.utf8";cppjieba::Jieba jieba(dict_path, hmm_path, user_dict_path, idf_path, stop_words_path);string s = "我来到北京清华大学";vector<string> words;jieba.CutAll(s, words);cout << limonp::Join(words.begin(), words.end(), "/") << endl;return 0;
}
编译代码
g++ -std=c++11 demo.cpp -Iinclude -Ideps/limonp/include -o demo
这里我们直接指定库所在的相对位置分别是cppjieba
的include
和limonp
的include
,缺乏一个都会使程序报错
演示结果
root@hcss-ecs-f59a:/gch/boost/text/cppjieba# ./demo
我/来到/北京/清华/清华大学/华大/大学
我们也可以在其它地方使用
我们采用简单的方式通过软连接使用
比如我的cppjieba
的目录现在在/gch/boost/text
,那我们就在/gch/boost/text
下新建一个using
目录
新建一个demo.cpp
#include "cppjieba/Jieba.hpp"
#include <iostream>
#include <vector>using namespace std;int main() {const string dict_path = "/gch/boost/text/cppjieba/dict/jieba.dict.utf8";const string hmm_path = "/gch/boost/text/cppjieba/dict/hmm_model.utf8";const string user_dict_path = "/gch/boost/text/cppjieba/dict/user.dict.utf8";const string idf_path = "/gch/boost/text/cppjieba/dict/idf.utf8";const string stop_words_path = "/gch/boost/text/cppjieba/dict/stop_words.utf8";cppjieba::Jieba jieba(dict_path, hmm_path, user_dict_path, idf_path, stop_words_path);string s = "我来到北京清华大学";vector<string> words;jieba.CutAll(s, words);cout << limonp::Join(words.begin(), words.end(), "/") << endl;return 0;
}
现在我们需要连接到cppjieba
的include/cppjieba
和limonp
的include/limonp
中,创建两个软件接,使用绝对路径
root@hcss-ecs-f59a:/gch/boost/text/using# ln -s /gch/boost/text/cppjieba/deps/limonp/include/limonp limonp
root@hcss-ecs-f59a:/gch/boost/text/using# ln -s /gch/boost/text/cppjieba/include/cppjieba cppjieba
查看软连接
root@hcss-ecs-f59a:/gch/boost/text/using# ll
total 472
drwxr-xr-x 2 root root 4096 Oct 19 16:37 ./
drwxr-xr-x 4 root root 4096 Oct 19 16:27 ../
lrwxrwxrwx 1 root root 41 Oct 19 16:36 cppjieba -> /gch/boost/text/cppjieba/include/cppjieba/
-rw-r--r-- 1 root root 770 Oct 19 16:37 demo.cpp
lrwxrwxrwx 1 root root 51 Oct 19 16:35 limonp -> /gch/boost/text/cppjieba/deps/limonp/include/limonp/
这里要注意一个点,就是我们demo.cpp
中的词典
路径也要修改
编译代码
g++ -std=c++11 demo.cpp -I. -o demo
-I. 就是在当前路径下寻找头文件
一般来讲我们该连接的都连接了,生成可执行程序应该没什么问题,执行可执行文件
root@hcss-ecs-f59a:/gch/boost/text/using# ./demo
我/来到/北京/清华/清华大学/华大/大学
文件链接示意图如下
上述就是一个工程文件和cppjieba
链接并使用的全部流程,如果有错误和看不懂的地方非常欢迎各位指正
(๑•̀ㅂ•́)و✧努力中!