-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.h
More file actions
201 lines (174 loc) · 5.74 KB
/
Copy pathengine.h
File metadata and controls
201 lines (174 loc) · 5.74 KB
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
/*
SHV181
Copyright (C) 2019 ghaith sassi & ahmed yassine hammami
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ENGINE_H
#define ENGINE_H
#include<bits/stdc++.h>
#ifdef _OPENMP
# include <omp.h>
#endif
#include <sys/types.h>
#include <dirent.h>
#include "index.h"
#include "file.h"
#include "ranking.h"
#include "analyzer.h"
using namespace std;
class engine{
Index *myindex;
rankingAlgorithm *myRanker ;
analyzer *myAnalyzer = new analyzerOccurence;
public:
/* constructors */
engine(){
myindex = new map_map_index;
myRanker = new aLitleBitSmarterAlgorithm;
}
engine(Index *db){
myindex = db;
myRanker = new notVerySmartRankingAlgorithm;
}
engine(rankingAlgorithm *rA){
myindex = new map_vec_index;
myRanker = rA;
}
engine(Index *db,rankingAlgorithm *rA){
myindex = db;
myRanker = rA;
}
engine(rankingAlgorithm *rA,Index *db){
myindex = db;
myRanker =rA;
}
/* end of constructors */
inline void indexFile(file &fileToBeIndexed){
vector<pair<string,wordAttributes>> stat = this->myAnalyzer->analyze(fileToBeIndexed);
string f = fileToBeIndexed.getFileName();
int fileId = myindex->getfileId(f);
if(fileId != -1 ){
// update instead of push
return;
}else{
#pragma omp critical
fileId = myindex->pushfile(f);
}
int n =(int)stat.size();
#pragma omp critical
for(int i = 0;i<n;i++){
myindex->push(stat[i].first,fileId,stat[i].second);
}
}
/*
inline void indexFile(file &fileToBeIndexed){
fileToBeIndexed.open();
string w;
string f = fileToBeIndexed.getFileName();
cerr<<f<<endl;
int fileId = myindex->getfileId(f);
if(fileId != -1 ){
// update instead of push
return;
}else{
fileId = myindex->pushfile(f);
}
while(fileToBeIndexed.getWord(w)){
myindex->push(w,fileId);
}
fileToBeIndexed.close();
}
*/
vector<pair<string,wordAttributes>> findWordInIndex(string &s){
string w = word::pipeline(s);
return myindex->searchWord(w);
}
void search(string s){
istringstream stringstream(s);
input<istringstream> searchTextStream(stringstream);
searchTextStream.addDelimiter();
map< string, vector<pair<string,wordAttributes>> > *searchResault = new map< string, vector<pair<string,wordAttributes>> >;
string words;
while(searchTextStream>>words){
/* make words go through pipe line */
words = word::pipeline(words);
/* ignore some words */
if(!(word::isOK(words))) continue;
/* add result to map */
if(auto it = searchResault->find(words) != searchResault->end() )continue;
vector<pair<string,wordAttributes>> vec = findWordInIndex(words);
(*searchResault)[words]=vec;
}
/* result */
cout<<"-----result------"<<endl;
cout<<"search for "<<s<<endl<<endl;
vector<string> * result = myRanker->sortfiles(s,searchResault);
for(auto i = result->begin() ; i!=result->end();i++){
cout<<(*i)<<endl;
}
delete result;
/*
for(auto it = searchResault->begin();it!=searchResault->end();it++){
int n = (int)(it->second)->size();
for(auto i = it->second->begin();i!=it->second->end();i++){
cout<<it->first<<" file:"<<myindex->getFileNameFromID(i->first)<<'\t'<<*(i->second)<<endl;
}
}
*/
delete searchResault;
}
void indexPath(string path){
vector<string> files;
DIR* dirp = opendir(path.c_str());
struct dirent * dp;
int i(0);
while ((dp = readdir(dirp)) != NULL) {
i++;
string extension;
string name=dp->d_name;
size_t j = name.rfind('.', name.length());
if (j != string::npos) extension=name.substr(j+1, name.length() - j);
else extension="";
string file_path;
file_path=path+"/"+dp->d_name;
if(extension=="txt") {
//cout<<"File : "<<dp->d_name<<" is indexed"<<endl;
//text txt(file_path);
//this->indexFile(txt);
files.push_back(file_path);
}
if (i!=1 && i!=2 && extension==""){
string dir;
dir=path+"/"+dp->d_name;
indexPath(dir);
}
}
closedir(dirp);
int n =(int)files.size();
#pragma omp parallel for //num_threads(1)
for(int i =0;i<n;i++){
text txt(files[i]);
this->indexFile(txt);
}
}
void saveIndex(){
myindex->save();
}
void loadIndex(){
myindex->load();
}
void printfileList(){
myindex->printFileList();
}
void printIndex(){}
};
#endif