-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvertedIndex.cpp
229 lines (189 loc) · 6.01 KB
/
InvertedIndex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <fstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <sstream>
#include <QResource>
#include <QString>
#include <QDir>
#include <QCoreApplication>
#include "InvertedIndex.h"
#include "./include/cppjieba/Jieba.hpp"
std::vector<std::string> InvertedIndex::WordSegmentation(std::string sentence)
{
QString targetDirectory = QCoreApplication::applicationDirPath()+"/Jiebadict";
// 将相对路径传递给jieba库的构造函数
cppjieba::Jieba jieba((targetDirectory+"/jieba.dict.utf8").toStdString(), (targetDirectory+"/hmm_model.utf8").toStdString(),
(targetDirectory+"/user.dict.utf8").toStdString(), (targetDirectory+"/idf.utf8").toStdString(),
(targetDirectory+"/stop_words.utf8").toStdString());
// Tokenize the sentence
std::vector<std::string> words;
jieba.Cut(sentence, words, true);
return words;
}
// Function to build an inverted index from a file
std::unordered_map<std::string, std::vector<int>> InvertedIndex::buildInvertedIndex(const std::string &filenameafter)
{
std::unordered_map<std::string, std::vector<int>> invertedIndex;
std::ifstream inputFile(filenameafter);
if(!inputFile.is_open())
{
}
std::string line;
int lineNumber = 1;
while (std::getline(inputFile, line))
{
std::istringstream iss(line);
std::string word;
while (iss >> word)
{
invertedIndex[word].push_back(lineNumber);
}
lineNumber++;
}
inputFile.close();
return invertedIndex;
}
// Function to search for a word in the inverted index
std::vector<int> InvertedIndex::searchInvertedIndex(const std::unordered_map<std::string, std::vector<int>> &invertedIndex, const std::string &word)
{
std::vector<int> results;
if (invertedIndex.count(word) > 0)
{
results = invertedIndex.at(word);
}
return results;
}
std::vector<int> InvertedIndex::FindIntersection(const std::vector<std::vector<int>> &arrays)
{
if (arrays.empty())
return {};
std::unordered_set<int> intersectionSet(arrays[0].begin(), arrays[0].end());
for (size_t i = 1; i < arrays.size(); ++i)
{
std::unordered_set<int> currentSet(arrays[i].begin(), arrays[i].end());
std::unordered_set<int> intersection;
for (const auto &num : currentSet)
{
if (intersectionSet.count(num) > 0)
intersection.insert(num);
}
intersectionSet = std::move(intersection);
}
std::vector<int> intersectionVec(intersectionSet.begin(), intersectionSet.end());
return intersectionVec;
}
std::vector<std::string> InvertedIndex::CtoE(std::string key)
{
std::string searchWord = key;
std::vector<std::string> resultline;
std::vector<std::string> searchWordsplit = WordSegmentation(searchWord);
std::vector<std::vector<int>> searchresultsRaw;
for (const auto &searchWordsingle : searchWordsplit)
{
std::vector<int> searchResults = searchInvertedIndex(invertedIndex, searchWordsingle);
searchresultsRaw.push_back(searchResults);
}
std::vector<int> searchResultsCommon = FindIntersection(searchresultsRaw);
std::ifstream wordFile(filename);
if (wordFile.is_open())
{
std::string line;
int count=1;
for (const auto &result : searchResultsCommon)
{
// 定位到目标行
wordFile.seekg(std::ios::beg);
for (int i = 1; i < result; ++i)
{
std::getline(wordFile, line);
}
// 读取目标行并添加到结果
std::getline(wordFile, line);
resultline.push_back(line);
count++;
if (count>=100)
{
break;
}
}
}
wordFile.close();
return resultline;
}
void InvertedIndex::build()
{
invertedIndex = buildInvertedIndex(filenameafter);
}
void InvertedIndex::removeUniqueResult(int lineNumber)
{
if (lineNumber != 0)
{
std::ifstream inputFile(filenameafter);
if (inputFile.is_open())
{
std::vector<std::string> lines;
std::string line;
int currentLine = 1;
// 读取文件的每一行,并将其存储在向量中
while (std::getline(inputFile, line))
{
if (currentLine == lineNumber)
{
// 忽略需要删除的行
currentLine++;
continue;
}
lines.push_back(line);
currentLine++;
}
inputFile.close();
// 将修改后的内容写回文件
std::ofstream outputFile(filenameafter, std::ofstream::trunc);
if (outputFile.is_open())
{
for (const auto &line : lines)
{
outputFile << line << std::endl;
}
outputFile.close();
}
}
}
}
void InvertedIndex::rebuild()
{
// 清空现有的倒排索引
invertedIndex.clear();
// 重新构建倒排索引
invertedIndex = buildInvertedIndex(filenameafter);
}
void InvertedIndex::insertLine(int lineNumber, std::string newLine) {
std::ifstream inFile(filenameafter);
std::vector<std::string> lines;
std::string line;
std::vector<std::string> temp = WordSegmentation(newLine);
newLine="";
for(const auto& line : temp)
{
newLine+=line;
newLine+=" ";
}
// 读取文件内容
while (std::getline(inFile, line)) {
lines.push_back(line);
}
inFile.close();
// 在指定行之前插入新行
if (lineNumber > 0 && lineNumber <= lines.size() + 1) {
lines.insert(lines.begin() + lineNumber - 1, newLine);
} else {
return;
}
// 将修改后的内容写回文件
std::ofstream outFile(filenameafter);
for (const auto& l : lines) {
outFile << l << std::endl;
}
outFile.close();
}