-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileIndex.cpp
269 lines (254 loc) · 7.42 KB
/
FileIndex.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "FileIndex.h"
#pragma warning (disable: 4267)
wchar_t * char2wchar(const char* cchar) {
wchar_t *m_wchar;
int len = MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), NULL, 0);
m_wchar = new wchar_t[len + 1];
MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), m_wchar, len);
m_wchar[len] = '\0';
return m_wchar;
}
char * wchar2char(const wchar_t* wchar) {
char * m_char;
int len = WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), NULL, 0, NULL, NULL);
m_char = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), m_char, len, NULL, NULL);
m_char[len] = '\0';
return m_char;
}
FileInfo::FileInfo(FILEREF num, const std::wstring& path) {
NLPIR_Init();
FileNum = num;
FileName = identifyName(path);
FilePath = path;
FileContent = Reader::read(path);
std::string temp = wchar2char(FileContent.c_str());
std::string LexResult = NLPIR_ParagraphProcess(temp.c_str(), 0);
std::wstring result = char2wchar(LexResult.c_str());
NLPIR_Exit();
max_freq = 0;
int start_pos;
bool valid = false;
bool k = false;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
for (int i = 0; i < result.length(); i++) {
if (result[i] < 33 || result[i] > 61000) {
if (valid == true) {
if (max_freq == 0)
max_freq = 1;
fword tpword;
tpword.word = result.substr(start_pos, i - start_pos);
tpword.freq = 1;
bool k = false;
for (int j = 0; j < words.size(); j++) {
if (words[j].word == tpword.word) {
words[j].freq++;
if (words[j].freq > max_freq)
max_freq = words[j].freq;
k = true;
break;
}
}
if(k == false)
words.push_back(tpword);
}
valid = false;
}
else {
if (valid == false) {
start_pos = i;
}
valid = true;
}
}
}
std::wstring FileInfo::identifyName(const std::wstring& path) {
for (int i = path.length() - 1; i > 0; i--) {
if (path[i] == L'\\')
return path.substr(i + 1);
}
return L"";
}
FileIndex::FileIndex(USNParser* driver) : driver(driver) {}
void FileIndex::InsertFiles(const std::wstring& dir) {
auto ref_num = driver->getFileRef(dir);
std::set<FileEntry*> files;
driver->recursiveAdd(ref_num, files);
for (auto file : files) {
if (Reader::isValid(file->file_name)) {
file->genPath(driver->all_entries);
InsertFileIndex(file->file_ref, file->full_path);
}
}
}
void FileIndex::InsertFileIndex(FILEREF num, const std::wstring& path) {
if (exist(num)) DeleteFileIndex(path);
FileInfo NewFile = FileInfo(num, path);
bool check = false;
std::list<post>::iterator iter;
for (int i = 0; i < NewFile.words.size(); i++) {
std::wstring word = NewFile.words[i].word;
post tempPost;
tempPost.FileNum = NewFile.FileNum;
tempPost.FreqNum = NewFile.words[i].freq;
DB[word].push_back(tempPost);
}
Files.push_back(NewFile);
}
void FileIndex::DeleteFileIndex(const std::wstring& path) {
std::list<FileInfo>::iterator iter;
std::list<post>::iterator iter2;
for (iter = Files.begin(); iter != Files.end(); ++iter) {
if (iter->FilePath == path) {
break;
}
}
for (int i = 0; i < iter->words.size(); i++) {
for (iter2 = DB[iter->words[i].word].begin(); iter2 != DB[iter->words[i].word].end(); ++iter2) {
if (iter2->FileNum == iter->FileNum) {
DB[iter->words[i].word].erase(iter2);
break;
}
}
}
Files.erase(iter);
}
bool postcompare(post a, post b) {
return a.FreqNum > b.FreqNum;
}
bool FileIndex::exist(FILEREF num) {
for (auto iter = Files.begin(); iter != Files.end(); ++iter)
if (iter->FileNum == num) return true;
return false;
}
std::set<FileEntry*> FileIndex::SearchFile(const std::wstring &sentence) {
NLPIR_Init();
std::string temp = wchar2char(sentence.c_str());
std::string LexResult = NLPIR_ParagraphProcess(temp.c_str(), 0);
std::wstring result = char2wchar(LexResult.c_str());
NLPIR_Exit();
int max_freq = 0;
bool k = false;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
std::vector<fword> words;
int start_pos;
bool valid = false;
std::vector<post> posts;
std::list<post>::iterator iter;
std::list<FileInfo>::iterator iter2;
for (int i = 0; i < result.length(); i++) {
if (result[i] < 33 || result[i] > 61000) {
if (valid == true) {
if (max_freq == 0)
max_freq = 1;
fword tpword;
tpword.word = result.substr(start_pos, i - start_pos);
tpword.freq = 1;
bool k = false;
for (int j = 0; j < words.size(); j++) {
if (words[j].word == tpword.word) {
words[j].freq++;
if (words[j].freq > max_freq)
max_freq = words[j].freq;
k = true;
break;
}
}
if (k == false)
words.push_back(tpword);
}
valid = false;
}
else {
if (valid == false) {
start_pos = i;
}
valid = true;
}
}
bool check = false;
for (int i = 0; i < words.size(); i++) {
for (iter = DB[words[i].word].begin(); iter != DB[words[i].word].end(); ++iter) {
for (int k = 0; k < posts.size(); k++) {
if (iter->FileNum == posts[k].FileNum) {
posts[k].FreqNum += iter->FreqNum;
check = true;
break;
}
}
if (check == false) {
posts.push_back(*iter);
}
check = false;
}
}
for (int i = 0; i < words.size(); i++) {
if (DB[words[i].word].size() == 0) {
words.erase(words.begin() + i);
i--;
continue;
}
words[i].tfidf = (0.5 + 0.5 * (double(words[i].freq) / double(max_freq)));
words[i].tfidf *= double(log10(Files.size() / DB[words[i].word].size()) + 1);
}
double d1 = 0;
for (auto diter = DB.begin(); diter != DB.end(); diter++) {
if (diter->second.size() == 0)
continue;
int k;
double tp_tfidf;
for (k = 0; k < words.size(); k++) {
if (diter->first == words[k].word) {
tp_tfidf = words[k].tfidf;
break;
}
}
if (k == words.size())
tp_tfidf = 0.5 * double(log10(Files.size() / diter->second.size()) + 1);
d1 += tp_tfidf * tp_tfidf;
}
for (int i = 0; i < posts.size(); i++) {
for (auto fiter = Files.begin(); fiter != Files.end(); fiter++) {
if (posts[i].FileNum == fiter->FileNum) {
posts[i].FreqNum = 0;
double d2 = 0;
for (int j = 0; j < fiter->words.size(); j++) {
int k;
double tp_tfidf;
d2 += fiter->words[j].tfidf * fiter->words[j].tfidf;
for (k = 0; k < words.size(); k++) {
if (fiter->words[j].word == words[k].word) {
tp_tfidf = words[k].tfidf;
break;
}
}
if (k == words.size())
tp_tfidf = 0.5 * double(log10(Files.size() / DB[fiter->words[j].word].size()) + 1);
posts[i].FreqNum += fiter->words[j].tfidf * tp_tfidf;
}
posts[i].FreqNum /= sqrt(d1);
posts[i].FreqNum /= sqrt(d2);
break;
}
}
}
sort(posts.begin(), posts.end(), postcompare);
std::set<FileEntry*> Result;
for (int i = 0; i < posts.size(); i++) {
for (iter2 = Files.begin(); iter2 != Files.end(); ++iter2) {
if (iter2->FileNum == posts[i].FileNum) {
Result.insert(driver->all_entries[iter2->FileNum]);
break;
}
}
}
return Result;
}
void FileIndex::Calctfidf() {
for (auto iter = Files.begin(); iter != Files.end(); ++iter) {
for (int i = 0; i < iter->words.size(); i++) {
iter->words[i].tfidf = (double(iter->words[i].freq) / double(iter->max_freq));
iter->words[i].tfidf *= double(log10(Files.size() / DB[iter->words[i].word].size()) + 1);
}
}
}