6
|
1 package pagerank;
|
|
2
|
|
3 import java.io.BufferedReader;
|
|
4 import java.io.File;
|
|
5 import java.io.FileInputStream;
|
|
6 import java.io.FileNotFoundException;
|
|
7 import java.io.FileOutputStream;
|
|
8 import java.io.IOException;
|
|
9 import java.io.InputStreamReader;
|
|
10 import java.util.ArrayList;
|
|
11 import java.util.Collections;
|
|
12 import java.util.Comparator;
|
|
13 import java.util.HashMap;
|
|
14 import java.util.LinkedList;
|
|
15
|
|
16 import com.tinkerpop.blueprints.Graph;
|
|
17 import com.tinkerpop.blueprints.Vertex;
|
|
18 import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
|
|
19 import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader;
|
|
20 import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter;
|
|
21
|
|
22 import pagerank.WikiPage;
|
|
23
|
|
24 public class TPReadWikiLink {
|
|
25
|
|
26 public static void main(String[] args) {
|
|
27
|
13
|
28 // final String fileDB = "./resources/tinkerpopDB";
|
|
29
|
|
30 final long PAGENUM = 100;
|
|
31 final String fileDB = "./resources/tinkerGraph"+Long.toString(PAGENUM);
|
|
32 final String pageRankLog = "./resources/wikiPageRank"+Long.toString(PAGENUM)+".log";
|
6
|
33
|
|
34 try {
|
|
35 Graph graph = new TinkerGraph();
|
|
36 FileInputStream in = new FileInputStream(new File(fileDB));
|
|
37 GraphMLReader.inputGraph(graph, in);
|
|
38 in.close();
|
|
39 LinkToVertex ltv = new LinkToVertex(graph);
|
7
|
40
|
13
|
41 ltv.initPageRankAllVertex();
|
|
42
|
6
|
43 final long AllVertexNumber = ltv.searchAllVertices();
|
|
44 HashMap<String, WikiPage> wikiHash = ltv.getWikiPageHash();
|
|
45 System.out.println("AllVertexNumber = "+AllVertexNumber);
|
13
|
46
|
|
47
|
|
48 // String nodeIds[] = {"80", "290", "21", "164", "41972", "103700", "65956", "103700"};
|
|
49 String nodeIds[] = {"146","148"};
|
|
50
|
9
|
51 /*
|
13
|
52 long start = java.lang.System.currentTimeMillis();
|
|
53 writeComputeTransition(ltv, nodeIds, 50, PAGENUM);
|
|
54 long end = java.lang.System.currentTimeMillis();
|
|
55 long time = end - start;
|
|
56 System.out.println(time);
|
|
57 */
|
|
58
|
8
|
59
|
13
|
60 for (int i=0; i<10; i++) {
|
|
61 long start = java.lang.System.currentTimeMillis();
|
|
62 for (int j=0; j<10; j++){
|
|
63 for (Vertex v : graph.getVertices()) {
|
|
64 ltv.computePageRankUsingPipes(v.getId());
|
|
65 }
|
|
66 }
|
|
67 long end = java.lang.System.currentTimeMillis();
|
|
68 long time = end - start;
|
|
69 // System.out.println(time);
|
|
70 }
|
|
71 /*
|
|
72 long count = 0;
|
|
73 for (Vertex v: graph.getVertices()) {
|
|
74 count++;
|
|
75 System.out.print("No."+count+" ");
|
|
76 System.out.print("title: "+v.getProperty("pageTitle"));
|
|
77 System.out.print(" pageRank: "+v.getProperty("pageRank"));
|
|
78 System.out.println();
|
|
79 System.out.flush();
|
|
80 }
|
11
|
81 */
|
8
|
82
|
13
|
83 FileOutputStream fos = new FileOutputStream(new File(pageRankLog));
|
|
84 descendingOrder(graph, fos);
|
|
85 // descendingOrder(wikiHash, fos);
|
11
|
86
|
6
|
87 FileOutputStream out = new FileOutputStream(new File(fileDB));
|
|
88 GraphMLWriter.outputGraph(graph, out);
|
|
89 out.close();
|
|
90
|
|
91 // loop(ltv);
|
|
92
|
|
93 } catch (NumberFormatException e){
|
|
94 System.out.println("Program exit");
|
|
95 } catch (Exception e) {
|
|
96 e.printStackTrace();
|
|
97 } finally {
|
|
98
|
|
99 }
|
|
100
|
|
101 }
|
|
102
|
|
103 public static void loop(LinkToVertex ltv) throws IOException {
|
|
104 BufferedReader r = new BufferedReader( new InputStreamReader(System.in), 1);
|
|
105 System.out.print("\nPlease enter Node Id...>");
|
|
106 System.out.flush();
|
|
107 String s;
|
|
108 while ((s = r.readLine()) != null ) {
|
|
109 int nodeId = Integer.parseInt(s);
|
|
110 ltv.printVertexInfo(nodeId);
|
|
111 System.out.print("\nPlease enter Node Id...>");
|
|
112 System.out.flush();
|
|
113 }
|
|
114 }
|
|
115
|
13
|
116 public static void writeComputeTransition(LinkToVertex ltv,final String nodeIds[], int count, long pagenum) throws IOException {
|
6
|
117 LinkedList<FileOutputStream> fosList = new LinkedList<FileOutputStream>();
|
7
|
118 for (String id: nodeIds) {
|
13
|
119 String filename = "./resources/VertexId_"+id+"_num"+Long.toString(pagenum)+".dat";
|
6
|
120 FileOutputStream fos = null;
|
|
121 fos = new FileOutputStream(filename);
|
|
122
|
7
|
123 Vertex v = ltv.getVertexById(id);
|
13
|
124 fos.write( ("# Vertex ID "+id+" "+ ltv.getPageTitle(v)+"\n").getBytes());
|
6
|
125 fosList.add(fos);
|
|
126 }
|
|
127
|
|
128 for (int i=0; i<count; i++) {
|
|
129 for (Vertex v : ltv.getAllVertices() ) {
|
13
|
130 // ltv.computePageRank(v);
|
|
131 ltv.computePageRankUsingPipes(v.getId());
|
6
|
132 }
|
|
133
|
|
134 for (int index=0; index<nodeIds.length; index++){
|
|
135 FileOutputStream fos = fosList.get(index);
|
|
136 printPageRankLog(fos, ltv, nodeIds[index], i);
|
|
137 }
|
|
138 }
|
|
139 for (FileOutputStream fos: fosList) {
|
|
140 fos.close();
|
|
141 }
|
|
142
|
|
143 }
|
|
144
|
|
145 // Write PageRank in descending order to fos.
|
|
146 public static void descendingOrder(HashMap<String, WikiPage> wikiHash , FileOutputStream fos) throws IOException {
|
|
147 ArrayList<WikiPage> list = new ArrayList<WikiPage>();
|
|
148 for (String title : wikiHash.keySet()) {
|
|
149 WikiPage w = wikiHash.get(title);
|
|
150 list.add(w);
|
|
151 }
|
|
152 Collections.sort(list, new Comparator<WikiPage>(){
|
|
153 public int compare(WikiPage w1, WikiPage w2) {
|
13
|
154 return (int)(w2.getRank()*Math.pow(10, 5)) - (int)(w1.getRank()*Math.pow(10, 5));
|
6
|
155 }
|
|
156 });
|
|
157
|
13
|
158 long count = 1;
|
6
|
159 for (WikiPage w : list) {
|
13
|
160 fos.write(("No."+count+"\n").getBytes());
|
6
|
161 w.printInfo(fos);
|
13
|
162 count++;
|
|
163 }
|
|
164 fos.close();
|
|
165
|
|
166 }
|
|
167
|
|
168 public static void descendingOrder(Graph graph, FileOutputStream fos) throws IOException {
|
|
169 ArrayList<WikiPage> list = new ArrayList<WikiPage>();
|
|
170 for (Vertex v : graph.getVertices()) {
|
|
171 WikiPage w = new WikiPage(v);
|
|
172 list.add(w);
|
|
173 }
|
|
174 Collections.sort(list, new Comparator<WikiPage>(){
|
|
175 public int compare(WikiPage w1, WikiPage w2) {
|
|
176 return (int)(w2.getRank()*Math.pow(10, 5)) - (int)(w1.getRank()*Math.pow(10, 5));
|
|
177 }
|
|
178 });
|
|
179
|
|
180 long count = 1;
|
|
181 for (WikiPage w : list) {
|
|
182 fos.write(("No."+count+"\n").getBytes());
|
|
183 w.printInfo(fos);
|
|
184 count++;
|
6
|
185 }
|
|
186 fos.close();
|
|
187
|
|
188 }
|
|
189
|
|
190 public static void printPageRankLog(FileOutputStream fos, int x, double rank) throws IOException {
|
|
191 fos.write( (x+" "+ rank+"\n").getBytes() );
|
|
192 fos.flush();
|
|
193 }
|
|
194
|
7
|
195 public static void printPageRankLog(FileOutputStream fos, LinkToVertex ltv, String id, int x) throws IOException {
|
|
196 double rank = ltv.getPageRank(ltv.getVertexById(id));
|
6
|
197 fos.write( (x+" "+ rank+"\n").getBytes() );
|
|
198 fos.flush();
|
|
199 }
|
|
200
|
|
201 }
|