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