BoostCompass(建立正排索引和倒排索引模块)

2024-04-14 01:04

本文主要是介绍BoostCompass(建立正排索引和倒排索引模块),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

阅读导航

  • 一、模块概述
  • 二、编写正排索引和倒排索引模块
    • ✅安装 jsoncpp
    • ✅Jieba分词库的安装
    • 1. 代码基本框架
    • 2. 正排索引的建立
    • 3. 倒排索引的建立
  • 三、整体代码
    • ⭕index.hpp

一、模块概述

这个模块我们定义了一个名为Index的C++类,用于构建和维护一个文档索引系统。该系统采用单例模式确保只有一个索引实例,并使用正排索引和倒排索引来快速检索文档。正排索引存储了文档的基本信息,如标题、内容和URL,而倒排索引则根据关键词将文档分组。类中提供了构建索引、获取文档信息和获取倒排列表的方法。构建索引的过程涉及读取处理过的数据文件,解析文档数据,并根据文档内容构建索引。此外,我们还实现了简单的进度显示功能。整个索引系统的构建旨在提高文档检索的效率和准确性。

二、编写正排索引和倒排索引模块

✅安装 jsoncpp

🔴安装方法:sudo yum install -y jsoncpp-devel

✅Jieba分词库的安装

PS:我们要先在Linux机器上安装Jieba分词库链接:🔴 "结巴(Jieba)"中文分词的C++版本

在这里插入图片描述

1. 代码基本框架

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <mutex>
#include "util.hpp" 
#include "log.hpp"  namespace ns_index {// 定义文档信息结构体struct DocInfo {std::string title;   // 文档的标题std::string content; // 文档内容(去标签后)std::string url;     // 文档的URLuint64_t doc_id;     // 文档的唯一ID};// 定义倒排列表中的元素结构体struct InvertedElem {uint64_t doc_id;   // 文档IDstd::string word;  // 关键字int weight;        // 关键字权重InvertedElem() : weight(0) {} // 默认构造函数,权重初始化为0};// 获取单例模式的实例static Index* GetInstance() {// 双重检查锁定模式,确保线程安全地获取单例if (nullptr == instance) {mtx.lock();if (nullptr == instance) {instance = new Index();}mtx.unlock();}return instance;}// 定义索引类Indexclass Index {private:// 构造函数、拷贝构造函数和赋值操作符都设置为私有,防止被实例化Index() {}Index(const Index&) = delete;Index& operator=(const Index&) = delete;// 单例模式的实例指针static Index* instance;// 保护单例模式的互斥锁static std::mutex mtx;public:// 析构函数~Index() {}// 根据关键字获取倒排拉链InvertedList* GetInvertedList(const std::string& word) {auto iter = inverted_index.find(word);if (iter == inverted_index.end()) {std::cerr << word << " have no InvertedList" << std::endl;return nullptr;}return &(iter->second);}};// 初始化单例模式的实例指针为nullptrIndex* Index::instance = nullptr;// 初始化互斥锁std::mutex Index::mtx;
}

代码分析

  1. 文档信息结构体 (DocInfo):

    • 定义了存储文档信息的结构体,包括标题、内容、URL和文档ID。
  2. 倒排列表元素结构体 (InvertedElem):

    • 定义了倒排列表中的元素结构体,包括文档ID、关键字和关键字权重。
  3. 单例模式的实现 (Index 类):

    • Index 类使用单例模式来确保整个程序中只有一个索引实例。
    • 构造函数、拷贝构造函数和赋值操作符都是私有的,防止外部直接创建实例。
    • GetInstance 方法用于获取索引实例,采用双重检查锁定模式来确保线程安全。
    • GetInvertedList 方法用于根据关键字获取对应的倒排列表。
  4. 全局变量和互斥锁

    • instance 是一个静态指针,指向Index类的实例。
    • mtx 是一个静态互斥锁,用于保护单例模式的实例创建过程。

总体来说,上面的代码展示了一个索引系统的基础框架,包括文档信息的存储结构和单例模式的索引管理。

2. 正排索引的建立

// 定义宏常量
#define NUM 101// 正排索引存储文档信息
std::vector<DocInfo> forward_index;// 根据文档ID获取文档信息
DocInfo* GetForwardIndex(uint64_t doc_id) {if (doc_id >= forward_index.size()) {std::cerr << "doc_id out of range, error!" << std::endl;return nullptr;}return &forward_index[doc_id];
}// 构建索引,输入为处理完毕的数据文件路径
bool BuildIndex(const std::string& input) {// 打开输入文件std::ifstream in(input, std::ios::in | std::ios::binary);if (!in.is_open()) {std::cerr << "sorry, " << input << " open error" << std::endl;return false;}// 读取文件行并构建索引std::string line;int count = 0;std::string bar(NUM, ' '); // 创建进度条bar[1] = '=';while (std::getline(in, line)) {DocInfo* doc = BuildForwardIndex(line);if (nullptr == doc) {continue;}BuildInvertedIndex(*doc);count++;// 显示进度if (count % 86 == 0) {int cnt = count / 86 + 1;bar[cnt] = '=';std::cout << "成功建立索引进度: " << bar << " [" << cnt << "%]" << "\r";std::cout.flush();}}std::cout << std::endl;return true;
}// 私有辅助函数,用于构建正排索引
DocInfo* BuildForwardIndex(const std::string& line) {// 分割字符串为标题、内容和URLstd::vector<std::string> results;const std::string sep = "\3"; // 行内分隔符ns_util::StringUtil::Split(line, &results, sep);if (results.size() != 3) {return nullptr;}// 创建文档信息并添加到正排索引DocInfo doc;doc.title = results[0];doc.content = results[1];doc.url = results[2];doc.doc_id = forward_index.size();// 插入到正排索引的vectorforward_index.push_back(std::move(doc));return &forward_index.back();
}

代码分析

  1. forward_index 是一个 std::vector,用于存储所有文档的正排索引信息。
  2. GetForwardIndex 函数通过文档ID从正排索引中检索文档信息。如果文档ID超出范围,则返回空指针并打印错误信息。
  3. BuildIndex 函数用于从数据文件中读取文档数据并构建索引。它打开输入文件,逐行读取并处理每一行,构建正排索引和倒排索引,并显示进度条。
  4. BuildForwardIndex 函数是一个私有辅助函数,用于构建单个文档的正排索引条目。它将输入行分割为标题、内容和URL,创建一个 DocInfo 对象,并将其添加到 forward_index 向量中。

3. 倒排索引的建立

// 定义宏常量
#define X 10
#define Y 1// 倒排索引存储关键字到倒排列表的映射
std::unordered_map<std::string, InvertedList> inverted_index;// 定义倒排列表的类型为InvertedElem元素的向量
typedef std::vector<InvertedElem> InvertedList;// 私有辅助函数,用于构建倒排索引
bool BuildInvertedIndex(const DocInfo& doc) {// 分词并统计词频struct word_cnt {int title_cnt;int content_cnt;word_cnt() : title_cnt(0), content_cnt(0) {}};// 用来暂存词频的映射表std::unordered_map<std::string, word_cnt> word_map;// 对标题进行分词std::vector<std::string> title_words;ns_util::JiebaUtil::CutString(doc.title, &title_words);// 对标题进行词频统计for (std::string s : title_words) {boost::to_lower(s);  // 将单词转换为小写word_map[s].title_cnt++;  // 如果存在就增加计数,否则创建新条目}// 对文档内容进行分词std::vector<std::string> content_words;ns_util::JiebaUtil::CutString(doc.content, &content_words);// 对内容进行词频统计for (std::string s : content_words) {boost::to_lower(s);word_map[s].content_cnt++;}// 构建倒排列表for (const auto& word_pair : word_map) {InvertedElem item;item.doc_id = doc.doc_id;item.word = word_pair.first;// 计算权重,标题中的词乘以X,内容中的词乘以Yitem.weight = X * word_pair.second.title_cnt + Y * word_pair.second.content_cnt;// 获取对应关键字的倒排列表,并添加新的倒排元素InvertedList& inverted_list = inverted_index[word_pair.first];inverted_list.push_back(std::move(item));}return true;
}

代码分析

  1. 定义数据结构

    • DocInfo 结构体定义了文档信息,包括标题、内容、URL和唯一的文档ID。
    • InvertedElem 结构体定义了倒排列表中的元素,包括文档ID、关键字和权重。
    • InvertedList 类型定义为 std::vector<InvertedElem>,表示一个倒排列表,包含多个 InvertedElem 元素。
  2. 构建正排索引

    • forward_index 是一个 std::vector<DocInfo>,用于存储所有文档的正排索引信息。
    • GetForwardIndex 函数通过文档ID从正排索引中检索文档信息。
  3. 构建倒排索引

    • inverted_index 是一个 std::unordered_map<std::string, InvertedList>,用于存储关键字到倒排列表的映射。
    • BuildInvertedIndex 函数用于根据文档信息构建倒排索引。它首先对文档的标题和内容进行分词,然后统计每个词在标题和内容中出现的次数(词频)。
    • 每个分词后的词都会被转换为小写,以便进行不区分大小写的搜索。
    • 为每个词创建一个 InvertedElem 对象,并根据其在标题和内容中的出现次数计算权重。
    • InvertedElem 对象添加到 inverted_index 中对应关键字的倒排列表中。
  4. 处理文本数据

    • BuildIndex 函数打开并读取输入文件,该文件包含处理完毕的文档数据。
    • 对文件中的每一行数据,使用 BuildForwardIndex 函数构建正排索引条目,并调用 BuildInvertedIndex 函数构建倒排索引。
    • 在构建索引的过程中,显示进度条以指示索引构建的进度。

整体来说,上面这段代码展示了如何从文本数据中提取文档信息,并构建正排索引和倒排索引,以便在搜索引擎中快速检索相关文档。通过倒排索引,可以有效地根据关键字找到所有相关文档,提高搜索效率。

三、整体代码

⭕index.hpp

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <mutex>
#include "util.hpp" 
#include "log.hpp"  #define NUM 101
#define X 10
#define Y 1namespace ns_index {// 定义文档信息结构体struct DocInfo {std::string title;   // 文档的标题std::string content; // 文档内容(去标签后)std::string url;     // 文档的URLuint64_t doc_id;     // 文档的唯一ID};// 定义倒排列表中的元素结构体struct InvertedElem {uint64_t doc_id;   // 文档IDstd::string word;  // 关键字int weight;        // 关键字权重InvertedElem() : weight(0) {} // 默认构造函数,权重初始化为0};// 倒排拉链储存列表typedef std::vector<InvertedElem> InvertedList;// 定义索引类Indexclass Index {private:// 正排索引存储文档信息std::vector<DocInfo> forward_index;// 倒排索引存储关键字到倒排列表的映射std::unordered_map<std::string, InvertedList> inverted_index;// 构造函数、拷贝构造函数和赋值操作符都设置为私有,防止被实例化Index() {}Index(const Index&) = delete;Index& operator=(const Index&) = delete;// 单例模式的实例指针static Index* instance;// 保护单例模式的互斥锁static std::mutex mtx;public:// 析构函数~Index() {}// 获取单例模式的实例static Index* GetInstance() {// 双重检查锁定模式,确保线程安全地获取单例if (nullptr == instance) {mtx.lock();if (nullptr == instance) {instance = new Index();}mtx.unlock();}return instance;}// 根据文档ID获取文档信息DocInfo* GetForwardIndex(uint64_t doc_id) {if (doc_id >= forward_index.size()) {std::cerr << "doc_id out of range, error!" << std::endl;return nullptr;}return &forward_index[doc_id];}// 根据关键字获取倒排拉链InvertedList* GetInvertedList(const std::string& word) {auto iter = inverted_index.find(word);if (iter == inverted_index.end()) {std::cerr << word << " have no InvertedList" << std::endl;return nullptr;}return &(iter->second);}// 构建索引,输入为处理完毕的数据文件路径bool BuildIndex(const std::string& input) {// 打开输入文件std::ifstream in(input, std::ios::in | std::ios::binary);if (!in.is_open()) {std::cerr << "sorry, " << input << " open error" << std::endl;return false;}// 读取文件行并构建索引std::string line;int count = 0;std::string bar(NUM, ' '); // 创建进度条bar[1] = '=';while (std::getline(in, line)) {DocInfo* doc = BuildForwardIndex(line);if (nullptr == doc) {continue;}BuildInvertedIndex(*doc);count++;// 显示进度if (count % 86 == 0) {int cnt = count / 86 + 1;bar[cnt] = '=';std::cout << "成功建立索引进度: " << bar << " [" << cnt << "%]" << "\r";std::cout.flush();}}std::cout << std::endl;return true;}private:// 私有辅助函数,用于构建正排索引DocInfo* BuildForwardIndex(const std::string& line) {// 分割字符串为标题、内容和URLstd::vector<std::string> results;const std::string sep = "\3"; // 行内分隔符ns_util::StringUtil::Split(line, &results, sep);if (results.size() != 3) {return nullptr;}// 创建文档信息并添加到正排索引DocInfo doc;doc.title = results[0];doc.content = results[1];doc.url = results[2];doc.doc_id = forward_index.size();//插入到正排索引的vectorforward_index.push_back(std::move(doc));return &forward_index.back();}// 私有辅助函数,用于构建倒排索引bool BuildInvertedIndex(const DocInfo& doc) {// 分词并统计词频struct word_cnt{int title_cnt;int content_cnt;word_cnt():title_cnt(0), content_cnt(0){}};std::unordered_map<std::string, word_cnt> word_map; //用来暂存词频的映射表//对标题进行分词std::vector<std::string> title_words;ns_util::JiebaUtil::CutString(doc.title, &title_words);//对标题进行词频统计for(std::string s : title_words){boost::to_lower(s);      //需要统一转化成为小写word_map[s].title_cnt++; //如果存在就获取,如果不存在就新建}//对文档内容进行分词std::vector<std::string> content_words;ns_util::JiebaUtil::CutString(doc.content, &content_words);//对内容进行词频统计for(std::string s : content_words){boost::to_lower(s);word_map[s].content_cnt++;}// 构建倒排列表for (const auto& word_pair : word_map) {InvertedElem item;item.doc_id = doc.doc_id;item.word = word_pair.first;item.weight = X * title_cnt.title_cnt + Y * content_cnt.content_cnt;InvertedList& inverted_list = inverted_index[word_pair.first];inverted_list.push_back(std::move(item));}return true;}};// 初始化单例模式的实例指针为nullptrIndex* Index::instance = nullptr;// 初始化互斥锁std::mutex Index::mtx;
}

这篇关于BoostCompass(建立正排索引和倒排索引模块)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/901710

相关文章

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Pandas中多重索引技巧的实现

《Pandas中多重索引技巧的实现》Pandas中的多重索引功能强大,适用于处理多维数据,本文就来介绍一下多重索引技巧,具有一定的参考价值,感兴趣的可以了解一下... 目录1.多重索引概述2.多重索引的基本操作2.1 选择和切片多重索引2.2 交换层级与重设索引3.多重索引的高级操作3.1 多重索引的分组聚

多模块的springboot项目发布指定模块的脚本方式

《多模块的springboot项目发布指定模块的脚本方式》该文章主要介绍了如何在多模块的SpringBoot项目中发布指定模块的脚本,作者原先的脚本会清理并编译所有模块,导致发布时间过长,通过简化脚本... 目录多模块的springboot项目发布指定模块的脚本1、不计成本地全部发布2、指定模块发布总结多模

oracle数据库索引失效的问题及解决

《oracle数据库索引失效的问题及解决》本文总结了在Oracle数据库中索引失效的一些常见场景,包括使用isnull、isnotnull、!=、、、函数处理、like前置%查询以及范围索引和等值索引... 目录oracle数据库索引失效问题场景环境索引失效情况及验证结论一结论二结论三结论四结论五总结ora

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

Python中列表的高级索引技巧分享

《Python中列表的高级索引技巧分享》列表是Python中最常用的数据结构之一,它允许你存储多个元素,并且可以通过索引来访问这些元素,本文将带你深入了解Python列表的高级索引技巧,希望对... 目录1.基本索引2.切片3.负数索引切片4.步长5.多维列表6.列表解析7.切片赋值8.删除元素9.反转列表

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit