main.cpp
3.28 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
#include "PaperGraphWidget.h"
#include <QtWidgets/QApplication>
#include <QDebug>
#include <algorithm>
#include <exception>
#include <fstream>
#include <iterator>
#include <string>
#include <map>
#include <vector>
#include <boost/algorithm/string.hpp> //boost::split
//#include <boost/bind.hpp>
#include <boost/bimap.hpp>
using namespace std;
const string PAPER_FILENAME = "dblp-paper.txt";
Graph read_graph(ifstream& in) {
/**
* Parse Paper dataset
* - paper_key, [author_list], publish_year
* Column Delimiter: ||
* Author list Delimiter: &&
*/
std::string line;
vector<std::string> tokens;
vector<std::string> authors;
vector<pair<string, string>> edges;
typedef boost::bimap<string, int> bm_type;
bm_type node_ids;
vector<simple_edge> edges_indexes;
int node_cnt = 0;
qDebug() << "* graph reading start" << endl;
while (std::getline(in, line) && !line.empty()) {
boost::split(tokens, line, boost::is_any_of("||"), boost::token_compress_on);
boost::split(authors, tokens[1], boost::is_any_of("&&"), boost::token_compress_on);
const string& paper_key = tokens[0];
if (node_ids.left.find(paper_key) == node_ids.left.end()) {
//node_ids[paper_key] = node_cnt++;
node_ids.insert(bm_type::value_type(paper_key, node_cnt++));
//qDebug() << paper_key.c_str() << " ";
}
for (auto author : authors) {
edges.push_back(pair<string, string>(paper_key, author));
if (node_ids.left.find(author) == node_ids.left.end()) {
//node_ids[author] = node_cnt++;
node_ids.insert(bm_type::value_type(author, node_cnt++));
//qDebug() << author.c_str() << " ";
}
}
//debug
if (node_cnt > 100) break;
}
qDebug() << "* graph reading complete" << endl;
//std::sort(node_names.begin(), node_names.end());
//Make graph
//Graph --> defined in "PaperGraphWidget.h"
for (auto edge : edges) {
edges_indexes.push_back({
node_ids.left.find(edge.first)->get_right(),
node_ids.left.find(edge.second)->get_right()
});
}
Graph graph(edges_indexes.begin(), edges_indexes.end(), node_ids.size());
//print map
//for (auto it=node_ids.left.begin(), itend=node_ids.left.end();
// it!=itend; ++it) {
// qDebug() << it->first.c_str() << " " << it->second << endl;
//}
//set index property
typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
vertex_iterator vi, vi_end;
int i = 0;
for (boost::tie(vi, vi_end)=vertices(graph); vi!=vi_end; ++vi) {
boost::put(vertex_index, graph, *vi, i);
boost::put(vertex_name, graph, *vi,
node_ids.right.find(i)->get_left());
//VertexProperties prop = VertexProperties(i,
// boost::property<vertex_name_t, std::string,
// boost::property<vertex_position_t, point>>());
++i;
}
//for (auto edge : edges) {
// //add edge
// //VertexProperties prop = VertexProperties()
// //add_edge(node_ids[edge.first], node_ids[edge.second],);
//}
//make graph layout
return graph;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
PaperGraphWidget w;
try {
ifstream fin(PAPER_FILENAME);
w.print_graph(read_graph(fin));
fin.close();
} catch (const std::exception& e) {
qDebug() << "Error: " << e.what() << endl;
return -1;
}
w.show();
return app.exec();
}