-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
398 lines (288 loc) · 9.36 KB
/
Copy pathmain.cpp
File metadata and controls
398 lines (288 loc) · 9.36 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include "graph.h"
#include "article.h"
#include "Author.h"
using namespace std;
typedef map <string, string> DuplicatesMap;
DuplicatesMap duplicates;
typedef map <string, string> NamesMap;
NamesMap author_id_name;
Graph * g;
vector<Article * > articles;
vector<Author * > authors;
typedef map <string, Article*> ArticlesMap;
ArticlesMap article_id_art;
typedef map <string, Author*> AuthorsMap;
AuthorsMap author_by_id;
typedef map <string, GraphNode*> GraphNodeMap;
GraphNodeMap gnode_by_id;
vector<GraphEdge *> edges;
vector<GraphNode *> nodes;
typedef map <string, GraphEdge*> EdgeMap;
EdgeMap edges_by_ids;
inline std::string trim_right(const std::string &source , const std::string& t = " ")
{
std::string str = source;
return str.erase( str.find_last_not_of(t) + 1);
}
inline std::string trim_left( const std::string& source, const std::string& t = " ")
{
std::string str = source;
return str.erase(0 , source.find_first_not_of(t) );
}
inline std::string trim(const std::string& source, const std::string& t = " ")
{
std::string str = source;
return trim_left( trim_right( str , t) , t );
}
void loadDuplicatesFile() {
ifstream myfile;
myfile.open ("../../iv04/additional/duplicate_author_map.txt");
string duplicate, original, name;
if (!myfile){
printf("Error in openening names file \n");
}
else {
string firstline;
getline(myfile, firstline); //discard first line
while (!myfile.eof( )){
myfile >> duplicate >> original;
getline(myfile, name);
duplicates[duplicate] = original;
author_id_name [original] = name;
//printf("name: %s, dup: %s, orig: %s \n", name.c_str(), duplicate.c_str(), original.c_str());
}
}
myfile.close();
}
void loadArticlesByAuthor() {
ofstream outfile;
outfile.open ("../../iv04/additional/tableform_data/articles_by_author_dupcorrect.txt");
ifstream myfile;
myfile.open ("../../iv04/additional/tableform_data/articles_by_author.txt");
string article_id, author_id, num_author, author;
if (!myfile || !outfile){
printf("Error in openening names file \n");
}
else {
string firstline;
getline(myfile, firstline); //discard first line
while (!myfile.eof( )){
myfile >> article_id >> num_author >> author_id;
getline(myfile, author);
if ( duplicates.count(author_id)>0 ) {
//this author has a duplicate
author_id = duplicates[author_id];
//printf("duplicate replaced %s by %s \n", author.c_str(), author_id_name[author_id].c_str() );
author = author_id_name[author_id];
}
else{
//get rid of white space in front of author and end of line at the end if it has one
author = trim(author);
author_id_name[author_id] = author;
}
string corrected = article_id+" "+num_author+" "+author_id+" "+author;
//printf("%s \n", corrected.c_str());
outfile << article_id <<" "<< num_author <<" "<< author_id <<" "<< author << endl;
}
}
myfile.close();
outfile.close();
}
void testdupcorrectfile(){
ifstream myfile;
myfile.open ("../../iv04/additional/tableform_data/articles_by_author_dupcorrect.txt");
string article_id, num_author, author_id, author;
if (!myfile){
printf("Error in openening names file \n");
}
else {
while (!myfile.eof( )){
myfile >> article_id >> num_author >> author_id;
getline(myfile, author);
string corrected = article_id+" "+num_author+" "+author_id+" "+author;
printf("%s \n", corrected.c_str());
}
}
myfile.close();
}
void createDataStructures(){
ifstream myfile;
myfile.open ("../../iv04/additional/tableform_data/articles_by_author_dupcorrect.txt");
string article_id, author_id, authorname;
int num_author;
if (!myfile){
printf("createDataStructures:: Error in openening file \n");
}
else {
while (!myfile.eof( )){
myfile >> article_id >> num_author >> author_id;
getline(myfile, authorname);
if ( author_id_name.count(author_id)>0 ) {
}
else{
author_id_name[author_id] = authorname;
author_by_id[author_id] = new Author(author_id);
authors.push_back(author_by_id[author_id]);
}
/*
if( article_id_art.count(article_id) > 0) {
}
else {
article_id_art[article_id] = new Article(article_id, num_author);
articles.push_back(article_id_art[article_id]);
}
*/
//Article * a = article_id_art[article_id];
//Author * auth = author_by_id[author_id];
//a-> addAuthor(author_id);
//auth -> addPub(article_id);
}
}
myfile.close();
}
void testDataStructures(){
printf("number of unique authors: %d \n", (int)author_id_name.size());
printf("number of unique articles: %d \n", (int)article_id_art.size());
printf("created authors: %d \n", (int) authors.size());
Article * lastarticle = articles.back();
vector<string> lastauthors = lastarticle->authorsList();
for (int s=0; s < lastauthors.size(); s++) {
printf("author %d is: %s \n", s+1, lastauthors.at(s).c_str());
}
Author * lastauthor = authors.back();
vector<string> lastauthpubs = lastauthor->pubsList();
for (int p=0; p < lastauthpubs.size(); p++) {
printf("pubs %d is: %s \n", p+1, lastauthpubs.at(p).c_str());
}
}
void createGraphDataStructures(){
//create graph nodes
map<string, string>::iterator auth;
for(auth = author_id_name.begin(); auth != author_id_name.end(); auth++) {
string idd = auth->first;
string authorname = auth->second;
int idpos = nodes.size();
GraphNode * anewnode = new GraphNode (idpos, idd, authorname);
gnode_by_id[idd] = anewnode;
nodes.push_back(anewnode);
}
//now create edges
map<string, Article*>::iterator art;
for(art = article_id_art.begin(); art != article_id_art.end(); art++) {
string articleid = art->first;
Article* thearticle = art->second;
vector<string> auths = thearticle->authorsList();
//for each author in an article, create an edge connecting that to the rest
for (int s= 0; s<auths.size(); s++) {
string auth1 = auths.at(s);
for (int rest=s+1; rest<auths.size(); rest++) {
string auth2 = auths.at(rest);
if (auth1 == auth2) {
printf("ERROR: author duplicate %s \n", auth1.c_str());
}
//create an edge between aut1 and aut2
//but...
//need to prevent duplicates
string edgekey1 = auth1+auth2;
string edgekey2 = auth2+auth1; //whichever encountered before, we don't know
if (edges_by_ids.count(edgekey1) > 0 || edges_by_ids.count(edgekey2) > 0 ) {
//this edge exists
}
else{
int posid = edges.size();
GraphEdge * anewedge = new GraphEdge(posid, auth1, auth2);
edges.push_back(anewedge);
edges_by_ids[edgekey1] = anewedge;
edges_by_ids[edgekey2] = anewedge;
}
}
}
}
//now print all edges
//printf("all number of edges created % d \n ", (int)edges.size());
//edges sanity check
/*
for (int e=0; e<edges.size(); e++) {
GraphEdge * edg = edges.at(e);
string efrom = edg->from;
string eto = edg->to;
for (int c=0; c<edges.size(); c++) {
GraphEdge * compare = edges.at(c);
if (compare->from == efrom && compare->to == eto) {
if (e != c) {
printf("duplicate at: %d, %d \n", e, c);
}
}
}
//printf("edge id: %d, from: %s, to: %s \n", edg->getEdgeid(), edg->from.c_str(), edg->to.c_str());
}
*/
}
ofstream xmloutfile;
ofstream xmlpuboutfile;
void printNode(GraphNode * g){
string thenodeid = trim(g->getNodeid(), "\t");
xmloutfile << "\t\t\t<DNVNODE IntId=\""<< g->getIntid() << "\" Id=\"" << g->getNodeid() << "\" Label=\"" << g->getLabel() << "\"";
xmloutfile <<" />" << endl;
}
void printEdge(GraphEdge * edg){
xmloutfile << "\t\t\t<DNVEDGE Id=\""<< edg->getEdgeid() << "\" To=\"" << edg->to << "\" From=\"" << edg->from << "\"";
xmloutfile <<" />" << endl;
}
void outputGraphXML(){
//ofstream xmloutfile;
xmloutfile.open ("../../iv04/additional/xmlform_data/co_author_graph.xml");
if (!xmloutfile) {
printf("ERROR OPENING OUTPUT XML FILE \n");
}
else{
int numnodes = nodes.size();
int numedges = edges.size();
xmloutfile << "<?xml version=\"1.0\" standalone=\"no\" ?>" << endl;
xmloutfile << "\t<DNVGRAPH>" << endl;
xmloutfile << "\t\t <Level value=\"0\" numberOfNodes=\" " << numnodes <<"\" numberOfEdges=\"" << numedges <<"\">" << endl;
//output nodes
for_each(nodes.begin(), nodes.end(), printNode);
//output edges
for_each(edges.begin(), edges.end(), printEdge);
}
xmloutfile.close();
}
void printPubs(Author * theat){
vector<string> pubs = theat->pubsList();
string auid = theat -> getId();
//xmlpuboutfile << auid << "\t" << pubs.size() << "\t";
for(int ps=0; ps<pubs.size(); ps++){
xmlpuboutfile << auid << "\t" << pubs.size() << "\t" << pubs.at(ps) << endl;
}
//xmlpuboutfile << endl;
}
void outputArticlesXML(){
//ofstream xmloutfile;
xmlpuboutfile.open ("../../iv04/additional/xmlform_data/articles.xml");
if (!xmloutfile) {
printf("ERROR OPENING OUTPUT XML FILE \n");
}
else{
int numauth = authors.size();
xmlpuboutfile << "author_id \t num_pubs \t pubs " << endl;
for_each(authors.begin(), authors.end(), printPubs);
}
xmlpuboutfile.close();
}
int main (int argc, char * const argv[]) {
//loadDuplicatesFile();
//loadArticlesByAuthor();
//testdupcorrectfile();
createDataStructures();
testDataStructures();
createGraphDataStructures();
outputArticlesXML();
//outputGraphXML();
return 0;
}