编写测试server
server.cc
#include "searcher.hpp"
#include <iostream>
#include <string>
const std::string input = "data/raw_html/raw.txt";
int main()
{
//for test
ns_searcher::Searcher *search = new ns_searcher::Searcher();
search->InitSearcher(input);
std::string query;
std::string json_string;
while(true){
std::cout << "Please Enter You Search Query# ";
std::cin >> query;
search->Search(query, &json_string);
std::cout << json_string << std::endl;
}
return 0;
}
makefile
PARSER=parser
SSVR=search_server
cc=g++
.PHONY:all
all:$(PARSER) $(SSVR)
$(Parser):parser.cc
$(cc) -o $@ $^ -lboost_system -lboost_filesystem -std=c++11
$(SSVR):server.cc
$(cc) -o $@ $^ -ljsoncpp -std=c++11
.PHONY:clean
clean:
rm -f parser
编译时可能cppjieba里会缺少limonp文件,需要从github上克隆到cppjieba里的include的cppjieba里
测试
make
在searcher文件里添加内容
void InitSearcher(const std::string &input)
{
//1. 获取或者创建index对象
index = ns_index::Index::GetInstance();
std::cout << "获取index单例成功..." << std::endl;
//2. 根据index对象建立索引
index->BuildIndex(input);
std::cout << "建立正排和倒排索引成功..." << std::endl;
}
在index文件里添加计数器
bool BuildIndex(const std::string &input) //parse处理完毕的数据交给我
{
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;
while(std::getline(in, line)){
DocInfo * doc = BuildForwardIndex(line);
if(nullptr == doc){
std::cerr << "build " << line << " error" << std::endl; //for deubg
continue;
}
BuildInvertedIndex(*doc);
count++;
if(count % 50 == 0){
std::cout << "当前已经建立的索引文档:" << count << std::endl;
}
}
return true;
}
输入filesystem
打开其中一个
Dependencies - 1.78.0
发现确实找到一个filesystem
编写获取摘要代码
searcher.hpp
#pragma once
#include "index.hpp"
#include "util.hpp"
#include <algorithm>
#include <unordered_map>
#include <json/json.h>
namespace ns_searcher{
class Searcher{
private:
ns_index::Index *index; //供系统进行查找的索引
public:
Searcher(){}
~Searcher(){}
public:
void InitSearcher(const std::string &input)
{
//1. 获取或者创建index对象
index = ns_index::Index::GetInstance();
std::cout << "获取index单例成功..." << std::endl;
//2. 根据index对象建立索引
index->BuildIndex(input);
std::cout << "建立正排和倒排索引成功..." << std::endl;
}
//query: 搜索关键字
//json_string: 返回给用户浏览器的搜索结果
void Search(const std::string &query, std::string *json_string)
{
//1.[分词]:对我们的query进行按照searcher的要求进行分词
std::vector<std::string> words;
ns_util::JiebaUtil::CutString(query, &words);
//2.[触发]:就是根据分词的各个"词",进行index查找,建立index是忽略大小写,所以搜索,关键字也需要
ns_index::InvertedList inverted_list_all; //内部InvertedElem
for(std::string word : words){
boost::to_lower(word);
ns_index::InvertedList *inverted_list = index->GetInvertedList(word);
if(nullptr == inverted_list){
continue;
}
inverted_list_all.insert(inverted_list_all.end(), inverted_list->begin(), inverted_list->end());
}
//3.[合并排序]:汇总查找结果,按照相关性(weight)降序排序
std::sort(inverted_list_all.begin(), inverted_list_all.end(),\
[](const ns_index::InvertedElem &e1, const ns_index::InvertedElem &e2){
return e1.weight > e2.weight;
});
//4.[构建]:根据查找出来的结果,构建json串 -- jsoncpp --通过jsoncpp完成序列化&&反序列化
Json::Value root;
for(auto &item : inverted_list_all){
ns_index::DocInfo * doc = index->GetForwardIndex(item.doc_id);
if(nullptr == doc){
continue;
}
Json::Value elem;
elem["title"] = doc->title;
elem["desc"] = GetDesc(doc->content, item.word); //content是文档的去标签的结果,但是不是我们想要的,我们要的是一部分
elem["url"] = doc->url;
root.append(elem);
}
Json::StyledWriter writer;
*json_string = writer.write(root);
}
std::string GetDesc(const std::string &html_content, const std::string &word)
{
//找到word在html_content中的首次出现,然后往前找50个字节(如果没有,就从begin开始),往后找100个字节(如果没有,就截取到end)
//截取出这部分内容
const std::size_t prev_step = 50;
const std::size_t next_step = 100;
//1.找到首次出现
std::size_t pos = html_content.find(word);
if(pos == std::string::npos){
return "None"; //不可能发生
}
//2.获取start,end
std::size_t start = 0;
std::size_t end = html_content.size() - 1;
//如果之前有50+字符,就更新开始位置
if(pos - prev_step > start) start = pos - prev_step;
if(pos + next_step < end) end = pos + next_step;
//3.截取子串,return
if(start >= end) return "None";
return html_content.substr(start, end - start);
}
};
}
makefile
PARSER=parser
SSVR=search_server
cc=g++
.PHONY:all
all:$(PARSER) $(SSVR)
$(PARSER):parser.cc
$(cc) -o $@ $^ -lboost_system -lboost_filesystem -std=c++11
$(SSVR):server.cc
$(cc) -o $@ $^ -ljsoncpp -std=c++11
.PHONY:clean
clean:
rm -f $(PARSER) $(SSVR)
调试发现获取摘要成功
选择一个url进入