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
|
|
28 final String fileDB = "./resources/tinkerpopDB";
|
|
29
|
|
30
|
|
31 try {
|
|
32 Graph graph = new TinkerGraph();
|
|
33 FileInputStream in = new FileInputStream(new File(fileDB));
|
|
34 GraphMLReader.inputGraph(graph, in);
|
|
35 in.close();
|
|
36 LinkToVertex ltv = new LinkToVertex(graph);
|
7
|
37
|
8
|
38 // ltv.initPageRankAllVertex();
|
6
|
39 final long AllVertexNumber = ltv.searchAllVertices();
|
|
40 HashMap<String, WikiPage> wikiHash = ltv.getWikiPageHash();
|
|
41 System.out.println("AllVertexNumber = "+AllVertexNumber);
|
7
|
42
|
9
|
43 /*
|
|
44 String nodeIds[] = {"80", "290", "21", "164"};
|
|
45 writeComputeTransition(ltv, nodeIds, 50);
|
8
|
46
|
9
|
47 */
|
10
|
48
|
8
|
49 FileOutputStream fos = new FileOutputStream(new File("./resources/wikiPageRank.log"));
|
|
50 descendingOrder(wikiHash, fos);
|
|
51
|
|
52
|
6
|
53 FileOutputStream out = new FileOutputStream(new File(fileDB));
|
|
54 GraphMLWriter.outputGraph(graph, out);
|
|
55 out.close();
|
7
|
56
|
6
|
57
|
|
58 // loop(ltv);
|
|
59
|
|
60 } catch (NumberFormatException e){
|
|
61 System.out.println("Program exit");
|
|
62 } catch (Exception e) {
|
|
63 e.printStackTrace();
|
|
64 } finally {
|
|
65
|
|
66 }
|
|
67
|
|
68 }
|
|
69
|
|
70 public static void loop(LinkToVertex ltv) throws IOException {
|
|
71 BufferedReader r = new BufferedReader( new InputStreamReader(System.in), 1);
|
|
72 System.out.print("\nPlease enter Node Id...>");
|
|
73 System.out.flush();
|
|
74 String s;
|
|
75 while ((s = r.readLine()) != null ) {
|
|
76 int nodeId = Integer.parseInt(s);
|
|
77 ltv.printVertexInfo(nodeId);
|
|
78 System.out.print("\nPlease enter Node Id...>");
|
|
79 System.out.flush();
|
|
80 }
|
|
81 }
|
|
82
|
|
83 public static void writeComputeTransition(LinkToVertex ltv,final String nodeIds[], int count) throws IOException {
|
|
84 LinkedList<FileOutputStream> fosList = new LinkedList<FileOutputStream>();
|
7
|
85 for (String id: nodeIds) {
|
|
86 String filename = "./resources/NodeId_"+id+".dat";
|
6
|
87 FileOutputStream fos = null;
|
|
88 fos = new FileOutputStream(filename);
|
|
89
|
7
|
90 Vertex v = ltv.getVertexById(id);
|
|
91 fos.write( ("# Node ID "+id+" "+ ltv.getPageTitle(v)+"\n").getBytes());
|
6
|
92 fosList.add(fos);
|
|
93 }
|
|
94
|
|
95 for (int i=0; i<count; i++) {
|
|
96 for (Vertex v : ltv.getAllVertices() ) {
|
|
97 ltv.computePageRank(v);
|
|
98 }
|
|
99
|
|
100 for (int index=0; index<nodeIds.length; index++){
|
|
101 FileOutputStream fos = fosList.get(index);
|
|
102 printPageRankLog(fos, ltv, nodeIds[index], i);
|
|
103 }
|
|
104 }
|
|
105 for (FileOutputStream fos: fosList) {
|
|
106 fos.close();
|
|
107 }
|
|
108
|
|
109 }
|
|
110
|
|
111 // Write PageRank in descending order to fos.
|
|
112 public static void descendingOrder(HashMap<String, WikiPage> wikiHash , FileOutputStream fos) throws IOException {
|
|
113 ArrayList<WikiPage> list = new ArrayList<WikiPage>();
|
|
114 for (String title : wikiHash.keySet()) {
|
|
115 WikiPage w = wikiHash.get(title);
|
|
116 list.add(w);
|
|
117 }
|
|
118 Collections.sort(list, new Comparator<WikiPage>(){
|
|
119 public int compare(WikiPage w1, WikiPage w2) {
|
|
120 return (int)(w2.getRank()*Math.pow(10, 10)) - (int)(w1.getRank()*Math.pow(10,10));
|
|
121 }
|
|
122 });
|
|
123
|
|
124 for (WikiPage w : list) {
|
|
125 w.printInfo(fos);
|
|
126 }
|
|
127 fos.close();
|
|
128
|
|
129 }
|
|
130
|
|
131 public static void printPageRankLog(FileOutputStream fos, int x, double rank) throws IOException {
|
|
132 fos.write( (x+" "+ rank+"\n").getBytes() );
|
|
133 fos.flush();
|
|
134 }
|
|
135
|
7
|
136 public static void printPageRankLog(FileOutputStream fos, LinkToVertex ltv, String id, int x) throws IOException {
|
|
137 double rank = ltv.getPageRank(ltv.getVertexById(id));
|
6
|
138 fos.write( (x+" "+ rank+"\n").getBytes() );
|
|
139 fos.flush();
|
|
140 }
|
|
141
|
|
142 }
|