《C/C+++ Boost 轻量级搜索引擎实战:架构流程、技术栈与工程落地指南——构造正/倒排索引(中篇)》

《C/C+++ Boost 轻量级搜索引擎实战:架构流程、技术栈与工程落地指南——构造正/倒排索引(中篇)》
前引:这是一个聚焦基础搜索引擎核心工作流的实操项目,基于 C/C++ 技术生态落地:从全网爬虫抓取网页资源,到服务器端完成 “去标签 - 数据清洗 - 索引构建” 的预处理,再通过 HTTP 服务接收客户端请求、检索索引并拼接结果页返回 —— 完整覆盖了轻量级搜索引擎的端到端逻辑。项目采用 C++11、STL、Boost 等核心技术栈,搭配 CentOS 7 云服务器 + GCC 编译环境(或 VS 系列开发工具)部署,既适配后端工程的性能需求,也能通过可选的前端技术(HTML5/JS 等)优化用户交互,是理解搜索引擎底层原理与 C++ 工程实践的典型案例

目录

【一】Jieba分词工具

【二】正/倒排索引结构设计

【三】关键函数设计

(1)由文档ID返回文档内容

(2)由关键字返回倒排拉链

(3)说明

(4)建立索引

(5)建立正排索引

(6)建立倒排索引

【四】单例模式


【一】Jieba分词工具

我们在对使用倒排索引的时候需要用到“关键词”,这个关键词由每个.html文档的标题和内容而来,因此就涉及到分词,所以我们使用 cppjieba 分词工具来完成,下面是上传:

如果需要使用到cppjieba分词工具,可以在gitcode上面直接搜索使用Git命令上传,我这里是本地上传到服务器:

然后对 cppjieba/include/cppjieba和cppjieba/dict分别建立软链接:头文件和词库

我们把cppjieba移动到上级目录,然后更新一下这两个软链接:

【二】正/倒排索引结构设计

//正排结构 typedef struct Forward_index { //文档内容 std::string title; std::string source; std::string chain; //对应正排ID uint64_t doc_id; }Forwardindex; //倒排结构 typedef struct Inverted_index { //对应正排ID int doc_id; //涉及到的关键字 std::string word; //权重 int weight; }Invertedindex; 

正排:根据ID映射对应的文档(标题、内容、URL),这个ID刚好利用vector的下标!

倒排:根据“关键字”映射对应的ID,所以利用unordered_map快速的搜索特性

//正排存储 std::vector<Forwardindex> Forward; typedef std::vector<Invertedindex> Stock_Inverted; //倒排存储 std::unordered_map<std::string,Stock_Inverted> Inverted;

【三】关键函数设计

(1)由文档ID返回文档内容

含义:即正排外部的实现,根据ID返回vector中对应的具体内容

//根据ID返回文档内容 Forwardindex* GetForward_index(const long long& id) { if(id>=Forward.size()) { std::cerr<<"GetForward_index is errno"<<std::endl; return nullptr; } return &Forward[id]; }
(2)由关键字返回倒排拉链

含义:倒排外部实现,根据“关键字”返回对应的ID,即返回对应的vector<Invertedindex>成员

解释:每个关键词涉及到的ID肯定不止一个,所以需要vector<Invertedindex>代表一个关键字涉               及到的所有文档

//根据关键字返回倒排拉链 Stock_Inverted* GetInverted_index(const std::string word) { auto it =Inverted.find(word); if(it == Inverted.end()) { std::cerr<<"GetInverted_index is errno"<<std::endl; return {}; } return &it->second; }
(3)说明

现在我们已经有了给外部的接口:由ID返回文档内容、由关键词返回文档ID

那么接下来就是填变量、建立索引关系:

//正排存储 std::vector<Forwardindex> Forward; typedef std::vector<Invertedindex> Stock_Inverted; //倒排存储 std::unordered_map<std::string,Stock_Inverted> Inverted;
(4)建立索引

原理:利用上一篇文章完成的数据清洗,最终每个.html文档内容都被输出到了data_clean.txt中

我们先利用getline按行读取(也就是按单个文档读取,因为data_clean.txt中也是按行写的)到 line变量里面,这样就拿到了单个文档的内容,因此while就是拿取data_clean.txt的全部文档内容!

建索引也就是同时建立正排和倒排的内容,也就是填vector和Unordered_map的操作!

//建立索引 bool Buildindex(const std::string &input) { std::ifstream in(input,std::ios::in | std::ios::binary); if(!in.is_open()) { std::cerr<<"Buildindex is errno"<<std::endl; return false; } //存单个读取文档内容 std::string line; while(std::getline(in,line)) { //正排 Forwardindex * doc = BUild_Forward_Index(line); printf("正在建立索引:%lld\ntitle:%s\nchain:%s\n",doc->doc_id,doc->title.c_str(),doc->chain.c_str()); if(doc==nullptr)continue; //倒排 Build_Inverted_Index(*doc); } return true; }
(5)建立正排索引

思路:首先我们拿到了单个文档的内容,也就是 line(string)变量,然后对这个内容进行解析,提取出标题、内容、URL(每个line变量都是:标题 | 内容 | URL 的格式,分割即可!)然后填入到vector里面,该文档的ID刚好就是vector的下标,下面有正排的结构体,大家可以对照着看:

//建立正排索引 Forwardindex *BUild_Forward_Index(const std::string&line) { Forwardindex* index = new Forwardindex(); //截取title size_t set_pos1 = line.find('\3'); if(set_pos1 == std::string::npos) { delete index; return nullptr; } index->title = line.substr(0, set_pos1); if (index->title.empty()) index->title = "空"; //截取source size_t set_pos2 = line.find('\3', set_pos1+1); if(set_pos2 == std::string::npos) { delete index; return nullptr; } index->source = line.substr(set_pos1+1, set_pos2 - (set_pos1+1)); if (index->source.empty()) index->source = "空"; //截取URL(chain) index->chain = line.substr(set_pos2+1); if (index->chain.empty()) index->chain = "空"; index->doc_id = Forward.size(); Forward.push_back(*index); return index; }
//正排结构 typedef struct Forward_index { //文档内容 std::string title; std::string source; std::string chain; //对应正排ID uint64_t doc_id; }Forwardindex;
(6)建立倒排索引
//建立倒排索引 bool Build_Inverted_Index(const Forwardindex& doc) { //建立分词对象 JiebaUtil jieba; std::vector<std::string> S; //对标题统计 S=jieba.Tokenize(doc.title); //此时V存放的是每个标题的分词,然后统计结果和出现次数 struct culculate { int title_size=0; int source_size=0; }; std::unordered_map<std::string,culculate> V; //遍历S统计标题的出现次数 for(auto e:S) { (V[e].title_size)++; } //统计内容出现次数 S.clear(); S=jieba.Tokenize(doc.source); for(auto e:S) { (V[e].source_size)++; } //遍历V的结果写入到Inverted for(auto it:V) { Invertedindex index_t; index_t.word=it.first; index_t.doc_id=doc.doc_id; index_t.weight=((it.second.title_size)*2+(it.second.source_size)*1); Inverted[it.first].push_back(std::move(index_t)); } return true; }

【四】单例模式

单例模式:只允许创建一个Index对象,后面只能由 index 指针调用该对象

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; class Index { public: typedef std::vector<Invertedindex> Stock_Inverted; //禁止拷贝构造和赋值重载 Index(const Index&)=delete; Index& operator=(const Index&)=delete; static Index*handle() { if(instance==nullptr) { pthread_mutex_lock(&mutex); if (instance == nullptr) { instance = new Index; } pthread_mutex_unlock(&mutex); } return instance; } ...... }
 //静态成员初始化 Index* Index::instance=nullptr;

Read more

【AI大模型】DeepSeek + 通义万相高效制作AI视频实战详解

【AI大模型】DeepSeek + 通义万相高效制作AI视频实战详解

目录 一、前言 二、AI视频概述 2.1 什么是AI视频 2.2 AI视频核心特点 2.3 AI视频应用场景 三、通义万相介绍 3.1 通义万相概述 3.1.1 什么是通义万相 3.2 通义万相核心特点 3.3 通义万相技术特点 3.4 通义万相应用场景 四、DeepSeek + 通义万相制作AI视频流程 4.1 DeepSeek + 通义万相制作视频优势 4.1.1 DeepSeek 优势 4.1.2 通义万相视频生成优势 4.2

By Ne0inhk
【DeepSeek微调实践】DeepSeek-R1大模型基于MS-Swift框架部署/推理/微调实践大全

【DeepSeek微调实践】DeepSeek-R1大模型基于MS-Swift框架部署/推理/微调实践大全

系列篇章💥 No.文章01【DeepSeek应用实践】DeepSeek接入Word、WPS方法详解:无需代码,轻松实现智能办公助手功能02【DeepSeek应用实践】通义灵码 + DeepSeek:AI 编程助手的实战指南03【DeepSeek应用实践】Cline集成DeepSeek:开源AI编程助手,终端与Web开发的超强助力04【DeepSeek开发入门】DeepSeek API 开发初体验05【DeepSeek开发入门】DeepSeek API高级开发指南(推理与多轮对话机器人实践)06【DeepSeek开发入门】Function Calling 函数功能应用实战指南07【DeepSeek部署实战】DeepSeek-R1-Distill-Qwen-7B:本地部署与API服务快速上手08【DeepSeek部署实战】DeepSeek-R1-Distill-Qwen-7B:Web聊天机器人部署指南09【DeepSeek部署实战】DeepSeek-R1-Distill-Qwen-7B:基于vLLM 搭建高性能推理服务器10【DeepSeek部署实战】基于Ollama快速部署Dee

By Ne0inhk

DeepSeek各版本说明与优缺点分析_deepseek各版本区别

DeepSeek各版本说明与优缺点分析 DeepSeek是最近人工智能领域备受瞩目的一个语言模型系列,其在不同版本的发布过程中,逐步加强了对多种任务的处理能力。本文将详细介绍DeepSeek的各版本,从版本的发布时间、特点、优势以及不足之处,为广大AI技术爱好者和开发者提供一份参考指南。 1. DeepSeek-V1:起步与编码强劲 DeepSeek-V1是DeepSeek的起步版本,这里不过多赘述,主要分析它的优缺点。 发布时间: 2024年1月 特点: DeepSeek-V1是DeepSeek系列的首个版本,预训练于2TB的标记数据,主打自然语言处理和编码任务。它支持多种编程语言,具有强大的编码能力,适合程序开发人员和技术研究人员使用。 优势: * 强大编码能力:支持多种编程语言,能够理解和生成代码,适合开发者进行自动化代码生成与调试。 * 高上下文窗口:支持高达128K标记的上下文窗口,能够处理较为复杂的文本理解和生成任务。 缺点: * 多模态能力有限:该版本主要集中在文本处理上,缺少对图像、语音等多模态任务的支持。 * 推理能力较弱:尽管在自然语言

By Ne0inhk

用DeepSeek和Cursor从零打造智能代码审查工具:我的AI编程实践

💂 个人网站:【 摸鱼游戏】【神级代码资源网站】【星海网址导航】摸鱼、技术交流群👉 点此查看详情 引言:AI编程革命下的机遇与挑战 GitHub统计显示,使用AI编程工具的开发者平均效率提升55%,但仅有23%的开发者能充分发挥这些工具的潜力。作为一名全栈工程师,我曾对AI编程持怀疑态度,直到一次紧急项目让我彻底改变了看法。客户要求在72小时内交付一个能自动检测代码漏洞、优化性能的智能审查系统,传统开发方式根本不可能完成。正是这次挑战,让我探索出DeepSeek和Cursor这对"黄金组合"的惊人潜力。 一、工具选型:深入比较主流AI编程工具 1.1 为什么最终选择DeepSeek+Cursor? 经过两周的对比测试,我们发现不同工具在代码审查场景的表现差异显著: 工具代码理解深度响应速度定制灵活性多语言支持GitHub Copilot★★★☆★★★★★★☆★★★★Amazon CodeWhisperer★★☆★★★☆★★★★★★☆DeepSeek★★★★☆★★★★★★★☆★★★★☆Cursor★★★☆★★★★☆★★★★★★★★ 关键发现: * Dee

By Ne0inhk