当前位置: 首页 > news >正文

C++:jieba库的安装使用保姆级教程

文章目录

  • 一、jieba是什么?
  • 二、安装步骤
    • 1.引入库
    • 2.进入库
    • 3.初始化config
    • 4.下载子模块
    • 5.编译可用库
  • 三、使用


一、jieba是什么?

在官方的jieba的解释里面可能有些不足,因为如果是小白可能会在安装的时候报错不知道怎么做,所以写下这篇文章针对官方md解释对小白理解的不足

下面如果内容有不理解或者错误,请各位帅哥,美女,大佬,大神该骂就骂,(๑•̀ㅂ•́)و✧努力中!

C++jieba,其实是 中文分词库 JiebaC++ 版本,就是其它开发者,给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:子模块的远程仓库地址,即 limonpGitHub 链接


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

这里我们直接指定库所在的相对位置分别是cppjiebaincludelimonpinclude,缺乏一个都会使程序报错


演示结果

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;
}

现在我们需要连接到cppjiebainclude/cppjiebalimonpinclude/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链接并使用的全部流程,如果有错误和看不懂的地方非常欢迎各位指正

(๑•̀ㅂ•́)و✧努力中!

在这里插入图片描述

http://www.dtcms.com/a/504552.html

相关文章:

  • 网站空间的根目录刷粉网站推广便宜
  • 建设网站需要哪些流程图办公室装饰
  • 校园官方网站建设营销软文范文200字
  • 找网络公司做网站需要注意的青岛建设银行股份有限公司网站
  • 苏州设置网站建设怎么看网站是哪家公司做的
  • 使用ACME自动签发SSL 证书
  • 泉州网站建设技术公司成功的网络营销案例及分析
  • 网站数据库连接错误建设项目所在地公共媒体网站
  • 网站如何提高转化率社区网站设计
  • 建站优化公司黎平网站开发
  • 免费php企业网站源码外贸网站建设厦门
  • 个人做的网站能备案吗帝国cms 网站名称
  • 广州智能模板建站大型公司办公室设计
  • 做网站需要什么基础主体备案与网站备案
  • 惠东网站建设网站制作用的软件有哪些
  • 我想网上做网站搜索引擎实训心得体会
  • 猪八戒网站怎么做任务wordpress位置
  • 网站建设unohacha电子招标投标平台网站建设
  • 夹娃娃网站如何做网站开发struts
  • 国外毕业设计网站网站建设 钱
  • android 利用反射和注解绑定控件id和点击事件
  • windows 网站模板网站流量统计查询
  • 601138(工业富联)-2025年10月19日
  • 桂林企业网站建设做网站的作用
  • 免费网站模块一个人在线观看免费中文
  • 比亚迪召回超11万辆车:质量管控亮起警示灯
  • 企业网站建设推广费用网站里添加百度地图
  • 手机网站模版 优帮云seo整站优化公司持续监控
  • 自己做电视视频网站网站域名和网址
  • sm2025 模拟赛19 (2025.10.14)