-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.cpp
More file actions
332 lines (286 loc) · 10.1 KB
/
Copy pathIndex.cpp
File metadata and controls
332 lines (286 loc) · 10.1 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
// Created by Sammy Timmins on 11/19/20.
//
#include "Index.h"
#include <ctype.h>
void removeTrainingPunct();
using namespace std;
using namespace rapidjson;
/**
* Creates the filler words set (K.L.)
*/
void makeFillerSet(set<string> &fillerSet)
{
string fillerWord;
ifstream fin;
fin.open("../stopwords.txt");
while(getline(fin, fillerWord)){
fillerSet.insert(fillerWord);
}
}
/**
* Removes the trailing Punctuation (K.L)
*/
void removeTrailingPunct(string& word) {
for(int i = word.length() - 1; i > 0; i--)
{
if(!isalpha(word[i]) && !isdigit(word[i])){
word.erase(i, 1);
} else {
break;
}
}
}
/**
* Removes the leading Punctuation (K.L)
*/
void removeLeadingPunct(string& word) {
while(!isalpha(word[0])){
if(word.length() == 0){
break;
}
word.erase(0,1);
}
}
/**
* Removes the in-between Punctuation (K.L)
*/
void punctDestroyer(string& word) {
for(int i = 0; i < word.length(); ++i){
if(!isascii(word[i])){
word.erase(i,1);
i--;
}
if(word[i] == '"'){
word.erase(i, 1);
i--;
}
if(word[i] == '\?'){
word.erase(i, 1);
i--;
}
if(word[i] == '\\'){
word.erase(i, 1);
i--;
}
}
}
/**
* Goes through each word and transform the word to lowercase (S.T.)
* @param word
*/
void toLower(string& word)
{
transform(word.begin(), word.end(), word.begin(), ::tolower);
}
/**
* loops through the document directory
* parses each JSON file as it loops
* populates both author and word indexes
*/
int buildIndexes(DSHashTable<string, Title> &authorIndex, DSTree<Word> &wordIndex, string &path, int &average)
{
string filePath, paperID;
DIR *directoryPath;
struct dirent *dirp;
int totalWords = 0;
directoryPath = opendir(path.c_str());
if(directoryPath == nullptr)
{
cout << "Error opening file" << endl;
exit(0);
}
int numArticles = 0;
while((dirp = readdir(directoryPath))) //loops through the directory of files
{
numArticles++;
filePath = path + "/" + dirp->d_name;
Document doc;
doc.Parse(getFile(filePath).c_str()); //parses json file into tree
if(doc.IsObject()) //checks that the document begins with an object
{
if(doc.HasMember("paper_id")) //checks that the file has an ID
{
paperID = doc["paper_id"].GetString();
}
if(doc["metadata"].HasMember("authors")) //checks that the file has metadata member
{
for(int i = 0; i < doc["metadata"]["authors"].Size(); i++) //loops through the authors json array
{
string author = doc["metadata"]["authors"][i]["last"].GetString(); //sets author name to last name from json
toLower(author);
if(!authorIndex.find(author)) //if the author is not already in the table, adds author to table and adds id to title vector
{
Title title;
title.addTitle(paperID);
pair<string , Title> pair(author, title);
authorIndex.insert(pair);
}
else //if the author is already in the table then add the paper ID to the ID vector
{
authorIndex[author].addTitle(paperID);
}
}
}
/**
* Creates a set of filler words (K.L.)
*/
set<string> fillerSet;
makeFillerSet(fillerSet);
/**
* adding words from title (S.T.)
*/
if(doc["metadata"].HasMember("title"))
{
stringstream ss;
string title = doc["metadata"]["title"].GetString();
ss << title;
string singleWord;
while(getline(ss, singleWord, ' '))
{
if((fillerSet.count(singleWord) == 0))
{
removeLeadingPunct(singleWord);
removeTrailingPunct(singleWord);
if(singleWord.length() == 0){
break;
}
punctDestroyer(singleWord);
Porter2Stemmer::stem(singleWord); //stemmer from: https://bitbucket.org/smassung/porter2_stemmer/src/master/
toLower(singleWord);
if(wordIndex.find(singleWord))
{
wordIndex.get(singleWord).getTitleList()[paperID]++;
}
else
{
wordIndex.insert(singleWord);
wordIndex.get(singleWord).addPaperID(paperID);
wordIndex.get(singleWord).getTitleList()[paperID]++;
}
/** adds one to totalFreq of Word */
wordIndex.get(singleWord).iterTotalFreq();
}
}
}
/**
* Adding words from abstract (K.L.)
*/
if(doc.HasMember("abstract"))
{
for(int i = 0; i < doc["abstract"].Size(); i++)
{
stringstream ss;
string abstract = doc["abstract"][i]["text"].GetString();
ss << abstract;
string singleWord;
while(getline(ss, singleWord, ' ')){
totalWords++;
if((fillerSet.count(singleWord) == 0))
{
removeLeadingPunct(singleWord);
removeTrailingPunct(singleWord);
if(singleWord.length() == 0){
break;
}
punctDestroyer(singleWord);
Porter2Stemmer::stem(singleWord); //stemmer from: https://bitbucket.org/smassung/porter2_stemmer/src/master/
toLower(singleWord);
if(wordIndex.find(singleWord))
{
wordIndex.get(singleWord).getTitleList()[paperID]++;
}
else
{
wordIndex.insert(singleWord);
wordIndex.get(singleWord).addPaperID(paperID);
wordIndex.get(singleWord).getTitleList()[paperID]++;
}
/** adds one to totalFreq of Word */
wordIndex.get(singleWord).iterTotalFreq();
}
}
}
}
/**
* Adding words from body text (K.L.)
*/
if(doc.HasMember("body_text"))
{
for(int i = 0; i < doc["body_text"].Size(); i++)
{
stringstream ss;
string body = doc["body_text"][i]["text"].GetString();
ss << body;
string singleWord;
while(getline(ss, singleWord, ' ')){
totalWords++;
/**
* for body text excerpt
*/
int btWordCount = 0;
string excerptBody;
if(btWordCount < 300){
excerptBody += singleWord;
}
if((fillerSet.count(singleWord) == 0)){
removeLeadingPunct(singleWord);
removeTrailingPunct(singleWord);
if(singleWord.length() == 0){
break;
}
punctDestroyer(singleWord);
Porter2Stemmer::stem(singleWord); //stemmer from: https://bitbucket.org/smassung/porter2_stemmer/src/master/
toLower(singleWord);
if(wordIndex.find(singleWord)){
wordIndex.get(singleWord).getTitleList()[paperID]++;
} else {
wordIndex.insert(singleWord);
wordIndex.get(singleWord).addPaperID(paperID);
wordIndex.get(singleWord).getTitleList()[paperID]++;
}
/** adds one to totalFreq of Word */
wordIndex.get(singleWord).iterTotalFreq();
}
}
}
}
}
}
cout << "\nIndexes built!\n" << endl;
average = totalWords / numArticles;
return numArticles;
}
/**
* Creates metadata for docs (S.T.)
* @param metadata
*/
void buildMetadata(set<Metadata> &metadata)
{
rapidcsv::Document doc("../metadata-cs2341.csv");
vector<string> ids = doc.GetColumn<string>("sha");
vector<string> titles = doc.GetColumn<string>("title");
vector<string> publishDates = doc.GetColumn<string>("publish_time");
vector<string> authors = doc.GetColumn<string>("authors");
vector<string> journals = doc.GetColumn<string>("journal");
for(int i = 0; i < ids.size(); i++)
{
Metadata data(ids[i], titles[i], publishDates[i], authors[i], journals[i]);
metadata.insert(data);
}
}
/**
* gets and returns the file path for each file in the directory (S.T.)
*/
string getFile(string &filePath)
{
ifstream file;
file.open(filePath.c_str());
string json, line;
while(getline(file, line))
{
json += line;
}
file.close();
return json;
}