forked from SMUCSE2341/22s-final-project-karinashin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserInterface.cpp
188 lines (165 loc) · 6.2 KB
/
UserInterface.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
//
// Created by Karina Shin on 4/23/2022.
//
#include "UserInterface.h"
UserInterface::UserInterface() {}
void UserInterface::run(const string& file)
{
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
bool go = true;
string choice;
while (choice != "1" && choice != "2"){
cout << "Enter 1 to parse files or 2 to use persistence file: " << endl;
cin.clear();
cin >> choice;
if (choice == "1"){
cout << "parsing..." << endl;
start = std::chrono::high_resolution_clock::now();
docReader.getFiles(file, stops);
end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time_in_seconds = end - start;
cout << std::fixed << "Parsing Time: " << time_in_seconds.count() << endl;
cout << "done!" << endl;
}
else if (choice == "2"){
cout << "parsing..." << endl;
// docReader.persistenceIndex();
docReader.getFiles(file, stops);
cout << "done!" << endl;
}
else{
cout << "Incorrect input." << endl;
}
}
while (go)
{
cin.get();
cout << "Search: " << endl;
string query;
getline(cin, query);
start = std::chrono::high_resolution_clock::now();
process.parseQuery(query, docReader.getWordTree(), docReader.getOrgTree(), docReader.getPersonTree(), stops);
end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time_in_seconds = end - start;
cout << std::fixed << "Query Execution Time: " << time_in_seconds.count() << endl;
displayResults();
choice = -1;
while (choice != "0"){
cout << "Enter the corresponding number to the article you wish to read (Enter 0 to skip): ";
cin >> choice;
if (choice == "0")//exit
break;
else if (choice == "1" || choice == "2" || choice == "3" || choice == "4" || choice == "5" || choice == "6" || choice == "7" || choice == "8" || choice == "9" || choice == "10" || choice == "11" || choice == "12" || choice == "13" || choice == "14" || choice == "15"){
if (stoi(choice) > 0 && stoi(choice) <= process.getBest().size()){
showText(process.getBest().at(stoi(choice) - 1));
cout << endl;
}
else
cout << "Incorrect input." << endl;
}
else
cout << "Incorrect input." << endl;
}
choice = -1;//reset choice;
while (choice != "0"){
cout << "Enter 1 to search again, 2 to display search engine stats, or 0 to exit the search engine: " << endl;
cin.get();
cin >> choice;
if (choice == "1"){
process.clearFinal();//reset results
break;
}
else if (choice == "2"){
stats();
cout << endl;
}
else if (choice == "0"){
go = false;
break;
}
else
cout << "Incorrect input." << endl;
}
}
}
void UserInterface::clearIndex()
{
docReader.getWordTree().deleteTree(docReader.getWordTree().getRoot());
docReader.getOrgTree().deleteTree(docReader.getOrgTree().getRoot());
docReader.getPersonTree().deleteTree(docReader.getPersonTree().getRoot());
}
void UserInterface::displayResults()//with ranking
{
if (process.getBest().size() == 0)
cout << "No results found" << endl;
for (int i = 0; i < process.getBest().size(); i++)
{
if (i == 15)
break;
cout << i + 1 << ") ";
cout << "Title: " << process.getBest().at(i).getTitle() << ", " << process.getBest().at(i).getPub() << ", Date: " << process.getBest().at(i).getDate() << endl;
// cout << "Path: " << process.getBest().at(i).getPath() << endl;
}
}
void UserInterface::showText(Document& d)
{
rapidjson::Document doc;
ifstream stream;
stream.open(d.getPath());
string wholeFile;
string temp;
while (getline(stream, temp))
wholeFile += temp;
stream.close();
doc.Parse(wholeFile.c_str());
if (!doc.IsObject()) cout << "somethings wrong" << endl;
string text = doc["text"].GetString();
cout << text << endl;
}
void UserInterface::stats()
{
//do not parse more than once (doubles numDocs)
//only call stats once you have parsed the documents
cout << "Search Engine stats:" << endl;
cout << "Total number of articles indexed: " << docReader.getNumDocs() << endl;
cout << "Total number of unique words indexed: " << docReader.getWordTree().getCount() << endl;
cout << "Total number of unique organizations indexed: " << docReader.getOrgTree().getCount() << endl;
cout << "Total number of unique persons indexed: " << docReader.getPersonTree().getCount() << endl;
getTopWords();
}
void UserInterface::topWordsHelper(Node<Word>* n)
{
if (n != nullptr){
topWordsHelper(n->getLeft());
bestWords.insert(std::pair<int, Word>(n->getData().getTotal(), n->getData()));
topWordsHelper(n->getRight());
}
}
void UserInterface::getTopWords()//go through tree and get the frequency of each word (in order)
{
topWordsHelper(docReader.getWordTree().getRoot());
vector<Word> top;//top 25 words
//result: total frequency for each doc
//get the top 25 most frequent words
cout << "Top 25 Most Frequent Words: " << endl;
map<int, Word>::iterator it = bestWords.begin();
map<int, Word>::iterator curr = bestWords.begin();
for (int n = 0; n < 25; n++){
it = bestWords.begin();
int highest = it->first;
if (n > bestWords.size())
break;
while (it != bestWords.end())
{
if(it->first > highest){
highest = it->first;
curr = it;
}
it++;
}
cout << curr->second << ": " << curr->first << endl;
bestWords.erase(curr);
}
}
DocParser& UserInterface::getDocParser() { return docReader; }
QueryProcessor& UserInterface::getQueryProcessor() { return process; }