-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecords.cpp
211 lines (191 loc) · 4.61 KB
/
Records.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
#include "Records.h"
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
CreateRecords::CreateRecords(std::vector<std::string>& fileList,std::string& stopFile)
:files(fileList),
stopWordFile(stopFile),
toker(new Toker(stopFile))
{
}
CreateRecords::~CreateRecords()
{
delete(toker);
for(DocList::iterator it = docs.begin();it!=docs.end();++it)
{
delete(*it);
}
}
bool
CreateRecords::initTokensAndDocuments()
{
if(!toker->getIsValid())
{
std::cout<<"Toker error:stop words not initialied\n";
return false;
}
DocList::const_iterator dIter=docs.begin();
for(std::vector<std::string>::iterator it=files.begin();it!=files.end();++it)
{
Document *d = new Document(*it,toker);
if(d->processDocument())
{
docs.insert_after(dIter,d);
}
else
{
std::cout<<"Error processing document: "<<d->getDocName();
delete(d);
return false;
}
}
return true;
}
bool
CreateRecords::indexAndCreateII()
{
for(DocList::iterator d=docs.begin();d!=docs.end();++d)
{
BOOST_FOREACH(std::string t,(*d)->getTokenList())
{
if (iIndex.find(t)!=iIndex.end())
{
DocList& dtemp=iIndex[t];
DocList::iterator it=dtemp.begin();
dtemp.insert_after(it,*d);
}
else
{
DocList temp;
DocList::iterator it=temp.begin();
temp.insert_after(it,*d);
iIndex.insert(std::make_pair<std::string,DocList>(t,temp));
}
}
}
}
bool
CreateRecords::helper_sort(Document* first,Document* second)
{
if (first->getDocID()<second->getDocID())
return true;
else
return false;
}
void
CreateRecords::parseSingleInputQString(std::string& q,std::string& prefix,std::string& suffix,bool& ANDQuery)
{
//support two delims -and & or
Tokenizer tok(q);
bool delimreached=false;
for(Tokenizer::iterator it=tok.begin();it!=tok.end();++it)
{
if(!delimreached)
{
if((ANDQuery=((*it)=="and"))||((*it)=="or"))
{
delimreached=true;
continue;
}
prefix+=(*it);
}
else
{
suffix+=(*it);
}
}
}
void
CreateRecords::processSingleQueryString(std::string& q,DocList& result)
{
std::size_t pos;
std::string suffix;
std::string prefix;
bool isAndQuery=false;
parseSingleInputQString(q,prefix,suffix,isAndQuery);
DocList& list1 =iIndex[prefix];
DocList& list2 =iIndex[suffix];
list1.sort(boost::bind(&CreateRecords::helper_sort,this,_1,_2));
list2.sort(boost::bind(&CreateRecords::helper_sort,this,_1,_2));
std::cout<<prefix;
printDocList(list1,prefix);
std::cout<<suffix;
printDocList(list2,suffix);
if(isAndQuery)
processANDQueryString(list1,list2,result);
else
processORQueryString(list1,list2,result);
std::cout<<"result ";
std::string res=prefix+";"+suffix;
printDocList(result,res);
}
void
CreateRecords::processANDQueryString(DocList& list1,DocList& list2,DocList& result)
{
if (list1.empty()||list2.empty())
{
return;
}
DocList::iterator resIter=result.begin();
DocList::iterator itList1,itList2;
for(itList1=list1.begin(),itList2=list2.begin();itList1!=list1.end() && itList2!=list2.end();)
{
if ((*itList1)->getDocID()==(*itList2)->getDocID())
{
resIter=result.insert_after(resIter,(*itList1));
++itList1;++itList2;
continue;
}
(*itList1)->getDocID()<(*itList2)->getDocID()?++itList1:++itList2;
}
}
void
CreateRecords::processORQueryString(DocList& list1,DocList& list2,DocList& result)
{
if (list1.empty()||list2.empty())
{
list1.empty()?result=list2:result=list1;
return;
}
result=list1;
result.merge(list2);
result.unique();
}
void
CreateRecords::printDocList(DocList& d,std::string& term)
{
if(!d.empty())
{
for(DocList::iterator dit=d.begin();dit!=d.end();++dit)
{
std::cout<<" -> "<<(*dit)->getDocID();
std::vector<std::string> splitVec;
boost::split(splitVec,term,boost::is_any_of(";"));
for(std::vector<std::string>::iterator it=splitVec.begin();it!=splitVec.end();it++)
std::cout<<" (cnt for "<<*it<<": "<<(*dit)->getTermCountAttribute(*it)<<")";
}
}
std::cout<<"\n";
}
int main(int argc,char *argv[])
{
//stub
std::vector<std::string> files;
std::string sw("./stopwords.txt");
std::string s;
while (*++argv)
{
s.assign(*argv);
files.push_back(s);
}
CreateRecords cr(files,sw);
cr.initTokensAndDocuments();
cr.indexAndCreateII();
//query - AND/OR single query proc
std::string query;
std::cout<<"Enter query string: ";
std::getline(std::cin,query);
DocList result;
cr.processSingleQueryString(query,result);
}