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;     // 文档的URL
        uint64_t doc_id;     // 文档的唯一ID
    };

    // 定义倒排列表中的元素结构体
    struct InvertedElem {
        uint64_t doc_id;   // 文档ID
        std::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;
    }
    
    // 定义索引类Index
    class 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);
        }
    };
    // 初始化单例模式的实例指针为nullptr
    Index* 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) {
    // 分割字符串为标题、内容和URL
    std::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();
    // 插入到正排索引的vector
    forward_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,内容中的词乘以Y
        item.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 1

namespace ns_index {
    // 定义文档信息结构体
    struct DocInfo {
        std::string title;   // 文档的标题
        std::string content; // 文档内容(去标签后)
        std::string url;     // 文档的URL
        uint64_t doc_id;     // 文档的唯一ID
    };

    // 定义倒排列表中的元素结构体
    struct InvertedElem {
        uint64_t doc_id;   // 文档ID
        std::string word;  // 关键字
        int weight;        // 关键字权重
        InvertedElem() : weight(0) {} // 默认构造函数,权重初始化为0
    };

    // 倒排拉链储存列表
    typedef std::vector<InvertedElem> InvertedList;

    // 定义索引类Index
    class 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) {
            // 分割字符串为标题、内容和URL
            std::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();
            //插入到正排索引的vector
            forward_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;
        }
    };
    // 初始化单例模式的实例指针为nullptr
    Index* Index::instance = nullptr;
    // 初始化互斥锁
    std::mutex Index::mtx;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/539006.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

微信小程序 uniapp+vue城市公交线路查询系统dtjl3

小程序Android端运行软件 微信开发者工具/hbuiderx uni-app框架&#xff1a;使用Vue.js开发跨平台应用的前端框架&#xff0c;编写一套代码&#xff0c;可编译到Android、小程序等平台。 前端&#xff1a;HTML5,CSS3 VUE 后端&#xff1a;java(springbootssm)/python(flaskdja…

Uniapp+基于百度智能云完成AI视觉功能(附前端思路)

本博客使用uniapp百度智能云图像大模型中的AI视觉API&#xff08;本文以物体检测为例&#xff09;完成了一个简单的图像识别页面&#xff0c;调用百度智能云API可以实现快速训练模型并且部署的效果。 uniapp百度智能云AI视觉页面实现 先上效果图实现过程百度智能云Easy DL训练图…

【Python】什么是pip,conda,pycharm,jupyter notebook?conda基本教程

pip--conda--pycharm--jupyter notebook &#x1f343;pip&#x1f343;conda&#x1f343;Pycharm&#x1f343;jupyter notebook&#x1f343;Conda基本教程☘️进入base环境☘️创建一个新的环境☘️激活环境☘️退出环境☘️查看电脑上都安装了哪些环境☘️删除已创建的项目…

时间序列分析 #ARMA模型的识别与参数估计 #R语言

掌握ARMA模型的识别和参数估计。 原始数据在文末&#xff01;&#xff01;&#xff01; 练习1、 根据某1915-2004年澳大利亚每年与枪支有关的凶杀案死亡率&#xff08;每10万人&#xff09;数据&#xff08;题目1数据.txt&#xff09;&#xff0c;求&#xff1a; 第1小题&…

Vim:强大的文本编辑器

文章目录 Vim&#xff1a;强大的文本编辑器Vim的模式命令模式常用操作光标移动文本编辑查找和替换 底行命令模式常用操作Vim的多窗口操作批量注释与去注释Vim插件推荐&#xff1a;vimforcpp结论 Vim&#xff1a;强大的文本编辑器 Vim&#xff0c;代表 Vi IMproved&#xff0c;…

【python】图像边缘提取效果增强方法-高斯模糊

一、介绍 高斯模糊是一种常用的图像处理技术&#xff0c;用于减少图像中的噪声和细节。它通过对图像中的每个像素点进行加权平均来实现模糊效果。具体而言&#xff0c;高斯模糊使用一个高斯核函数作为权重&#xff0c;对每个像素点周围的邻域进行加权平均。这样可以使得每个像…

软件开发安全备受重视,浙江某运营商引入CWASP认证课程,

​浙江省某大型运营商是一家实力雄厚、服务优质的通信运营商&#xff0c;致力于为全省用户提供优质、高效的通信服务。数字时代&#xff0c;该运营商顺应信息能量融合发展趋势&#xff0c;系统打造以5G、算力网络、能力中台为重点的新型信息基础设施&#xff0c;夯实产业转型升…

npm install 报 ERESOLVE unable to resolve dependency tree 异常解决方法

问题 在安装项目依赖时&#xff0c;很大可能会遇到安装不成功的问题&#xff0c;其中有一个很大的原因&#xff0c;可能就是因为你的npm版本导致的。 1.npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree 2.ERESOLVE unable to resolve dependenc…

【力扣】17.04.消失的数字

这道题的题目意思就是从0-n中的数字中找出缺失的那一个&#xff0c;n是数组的长度&#xff0c;因此我的想法就是先将数组进行排序&#xff0c;往sort&#xff08;&#xff09;里面一扔&#xff0c;完了以后看前一个与后一个之差中哪个不是等于1的&#xff0c;就求出来即可。 法…

STM32学习和实践笔记(10): Systick定时器介绍

1.SysTick定时器介绍 sysTick定时器也叫SysTick滴答定时器&#xff0c;它是Cortex-M3内核的一个外设&#xff0c;被嵌入在 NVIC中。(NVIC:嵌套向量中断控制器&#xff0c;属于内核外设&#xff0c;管理着包括内核和片上所有外设的中断相关的功能) 它是一个24位&#xff08;注…

javaweb day29

事务 写法 事务的四大特性

【C++题解】1027 - 求任意三位数各个数位上数字的和

问题&#xff1a;1027 - 求任意三位数各个数位上数字的和 类型&#xff1a;基础问题 题目描述&#xff1a; 对于一个任意的三位自然数 x &#xff0c;编程计算其各个数位上的数字之和 S 。 输入&#xff1a; 输入一行&#xff0c;只有一个整数 x(100≤x≤999) 。 输出&…

三种网络安全行业整合模式深度解读

注&#xff1a;本文写于PANW更新及其引发的所有关于平台化的讨论之前几周。之后&#xff0c;这篇文章没有经过编辑&#xff0c;我相信它在今天仍然和以前一样具有现实意义。 在过去几年里&#xff0c;我一直在讨论网络安全行业的复杂性、细微差别和错综复杂性。在讨论过程中&am…

力扣2923、2924.找到冠军I、II---(简单题、中等题、Java、拓扑排序)

目录 一、找到冠军I 思路描述&#xff1a; 代码&#xff1a; 二、找到冠军II 思路描述&#xff1a; 代码&#xff1a; 一、找到冠军I 一场比赛中共有 n 支队伍&#xff0c;按从 0 到 n - 1 编号。 给你一个下标从 0 开始、大小为 n * n 的二维布尔矩阵 grid 。对于满足…

uniapp 开发小程序如何检测到更新点击重启小程序完成更新?

官方文档&#xff1a;uni.getUpdateManager() | uni-app官网 示例代码&#xff1a; const updateManager uni.getUpdateManager();updateManager.onCheckForUpdate(function (res) {// 请求完新版本信息的回调console.log(res.hasUpdate); });updateManager.onUpdateReady(fu…

【Android广播机制】之静态注册与动态注册全网详解

&#x1f604;作者简介&#xff1a; 小曾同学.com,一个致力于测试开发的博主⛽️&#xff0c;主要职责&#xff1a;测试开发、CI/CD 如果文章知识点有错误的地方&#xff0c;还请大家指正&#xff0c;让我们一起学习&#xff0c;一起进步。 &#x1f60a; 座右铭&#xff1a;不…

界面控件DevExpress WinForms/WPF v23.2 - 富文本编辑器支持内容控件

众所周知内容控件是交互式UI元素(文本字段、下拉列表、日期选择器)&#xff0c;用于在屏幕上输入和管理信息。内容控件通常在模板/表单中使用&#xff0c;以标准化文档格式和简化数据输入。DevExpress文字处理产品库&#xff08;Word Processing Document API、WinForm和WPF富文…

LiveNVR监控流媒体Onvif/RTSP功能-概览负载统计展示取流中、播放中、录像中点击柱状图快速定位相关会话

LiveNVR概览负载统计展示取流中、播放中、录像中点击柱状图快速定位相关会话 1、负载信息说明2、快速定位会话3、RTSP/HLS/FLV/RTMP拉流Onvif流媒体服务 1、负载信息说明 实时展示取流中、播放中、录像中等使用数目 取流中&#xff1a;当前拉流到平台的实时通道数目播放中&am…

【opencv】示例-imagelist_reader.cpp 读取YAML格式文件中的图片列表,并逐个显示这些图片的灰度图...

这段代码的功能是使用OpenCV库读取一个YAML或XML格式文件中的图片列表&#xff0c;并且逐个地在窗口中以灰度图像的形式显示这些图片。用户可以按任意键来查看下一张图片。程序提供了帮助信息输出&#xff0c;指导用户如何使用该程序。此外&#xff0c;它使用命令行参数解析器来…

MariaDB介绍和安装

MariaDB介绍和安装 文章目录 MariaDB介绍和安装1.MariaDB介绍2.MariaDB安装2.1 主机初始化2.1.1 设置网卡名和ip地址2.1.2 配置镜像源2.1.3 关闭防火墙2.1.4 禁用SELinux2.1.5 设置时区 2.2 包安装2.2.1 Rocky和CentOS 安装 MariaDB2.2.2 Ubuntu 安装 MariaDB 2.3 源码安装2.3.…