-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
98 lines (88 loc) · 2.53 KB
/
main.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
#include <iostream>
#include <string>
#include "indexer.h"
#include "time.h"
using namespace std;
void showHelp();
int main(int argc, char *argv[])
{
Indexer I;
// Store command line arguments
vector<string> args;
for (int i = 1; i < argc; i++)
args.insert(args.end(), string(argv[i]));
vector<string>::iterator it;
// If user needs help, display help then exit
for (it = args.begin(); it != args.end() && *it != "--help"; it++);
if (it != args.end())
{
// Display help
showHelp();
// Exit
return 0;
}
// If user specifies stop words list:
for (it = args.begin(); it != args.end() && *it != "--stop-words-file"; it++);
if (it != args.end())
{
// Consume option
args.erase(it);
// Index stop words list
I.indexStopWords(*it);
// Consume argument
args.erase(it);
}
// else, load default stopwords file
else
{
I.indexStopWords("stopwords");
}
// If there's no file to index, display help then exit
if (args.size() == 0)
{
showHelp();
return 0;
}
// Index all documents from command line args
time_t start = clock();
for (it = args.begin(); it != args.end(); it++)
{
if (I.addDocument(*it))
cout << "Indexed " << *it << "\n";
else
cout << "Ignored " << *it << "\n";
}
cout << "Complete in " << (float) (clock() - start)/CLOCKS_PER_SEC << " second(s).\n";
while (1)
{
cout << "Query: ";
string q;
getline(cin, q);
if (q == ".")
break;
I.setQuery(q);
I.execute();
vector<Document> result = I.result();
for (vector<Document>::iterator it = result.begin(); it != result.end(); it++)
{
cout << it->name() << " " << it->occurrence() << "\n";
}
}
return 0;
}
void showHelp()
{
cout << "CLI Manual (can apply for GUI version):\n"
<< "Running:\n"
<< " search-engine [--option [<argument>]] <files_to_index>\n"
<< "Provide all file names as commandline arguments for the program\n\n"
<< "Stop the program:\n"
<< " Use \".\" (dot) as the query\n\n"
<< "Program's option:\n"
<< "--stop-words-file <path/to/file>\n"
<< " specify stop words list file. Default file: \"./stopwords\"\n"
<< "--no-gui (for GUI version)\n"
<< " run without GUI\n"
<< "--help"
<< " show this manual\n";
}