2
|
1 package pagerank;
|
|
2
|
|
3 import java.util.HashMap;
|
|
4
|
|
5 import com.tinkerpop.blueprints.Direction;
|
|
6 import com.tinkerpop.blueprints.Edge;
|
|
7 import com.tinkerpop.blueprints.Graph;
|
|
8 import com.tinkerpop.blueprints.Vertex;
|
|
9
|
|
10 import pagerank.WikiPage;
|
|
11
|
|
12 public class LinkToVertex {
|
|
13
|
|
14 Graph graph;
|
|
15 public final static String PAGE_TITLE = "pageTitle";
|
|
16 public final static String PAGE_RANK = "pageRank";
|
7
|
17 // pageIdTable
|
|
18 // key: pageTitle value: Vertex ID
|
5
|
19 private HashMap<String, Object> pageIdTable = new HashMap<String, Object>();
|
2
|
20
|
7
|
21 // wikiPageHash
|
|
22 // key: pageTitle value: wikiPage(class)
|
2
|
23 private HashMap<String, WikiPage> wikiPageHash = new HashMap<String, WikiPage>();
|
4
|
24 private long AllVertexNumber;
|
2
|
25
|
8
|
26 private final double weight = 0.85;
|
2
|
27
|
|
28 public static final String HAS_LINK = "HasLink";
|
|
29
|
4
|
30 public LinkToVertex(Graph graph) {
|
2
|
31 this.graph = graph;
|
4
|
32 AllVertexNumber = 0;
|
2
|
33 }
|
|
34
|
5
|
35 public Object getId(String pageTitle) {
|
2
|
36 return pageIdTable.get(pageTitle);
|
|
37 }
|
|
38
|
4
|
39 public boolean isHasLink(String label) {
|
2
|
40 return label.equals(HAS_LINK);
|
|
41 }
|
|
42
|
4
|
43 public Vertex createVertex() {
|
2
|
44 return graph.addVertex(null);
|
|
45 }
|
|
46
|
4
|
47 public Vertex createVertex(Object id) {
|
2
|
48 return graph.addVertex(id);
|
|
49 }
|
|
50
|
5
|
51
|
|
52 Vertex createVertexWithProperty(String key, Object value, Object id) {
|
|
53 Vertex v = createVertex(id);
|
2
|
54 v.setProperty(key,value);
|
|
55 return v;
|
|
56 }
|
|
57
|
5
|
58 Vertex createVertexWithProperty(String key, Object value) {
|
|
59 return createVertexWithProperty(key, value, null);
|
|
60 }
|
|
61
|
4
|
62 public String getPageTitle(Vertex v) {
|
2
|
63 return (String) v.getProperty(PAGE_TITLE);
|
|
64 }
|
|
65
|
4
|
66 public Double getPageRank(Vertex v) {
|
2
|
67 return (Double) v.getProperty(PAGE_RANK);
|
|
68 }
|
|
69
|
5
|
70
|
|
71 Vertex createVertexWithPageTitle(String pageTitle, Object id) {
|
|
72 Vertex v = createVertexWithProperty(PAGE_TITLE, pageTitle, id);
|
|
73 setPageRank(v, (Double)0.0);
|
|
74 pageIdTable.put(pageTitle, id );
|
|
75 return v;
|
|
76 }
|
|
77
|
2
|
78 Vertex createVertexWithPageTitle(String pageTitle) {
|
5
|
79 return createVertexWithPageTitle(pageTitle, null);
|
2
|
80 }
|
|
81
|
|
82 Vertex setPageRank(Vertex v, Double rank) {
|
|
83 v.setProperty(PAGE_RANK, rank);
|
|
84 return v;
|
|
85 }
|
|
86
|
|
87 Vertex getVertex(String name) {
|
5
|
88 Object id = pageIdTable.get(name);
|
2
|
89 return graph.getVertex(id);
|
|
90 }
|
|
91
|
7
|
92 Vertex getVertexById(String id) {
|
|
93 return graph.getVertex(id);
|
2
|
94 }
|
|
95
|
|
96 Edge setRelationship(Vertex v1, Vertex v2, String label) {
|
|
97 Edge e = graph.addEdge(null, v1, v2, label);
|
|
98 return e;
|
|
99 }
|
|
100
|
|
101 Edge setHasLink(Vertex v1, Vertex v2) {
|
|
102 return setRelationship(v1, v2, HAS_LINK);
|
|
103 }
|
|
104
|
8
|
105 void initPageRankAllVertex() {
|
|
106 for (Vertex v : graph.getVertices()) {
|
|
107 setPageRank(v, 0.0);
|
|
108 }
|
|
109 }
|
|
110
|
4
|
111 long searchAllVertices() {
|
|
112 AllVertexNumber = 0;
|
2
|
113 for (Vertex v : graph.getVertices()) {
|
|
114 if ( (v.getProperty(PAGE_TITLE) != null) &&
|
|
115 (v.getProperty(PAGE_RANK)) != null ) {
|
5
|
116 WikiPage wiki = new WikiPage(v);
|
|
117 pageIdTable.put((String) v.getProperty(PAGE_TITLE), v.getId());
|
2
|
118 wiki.setInHasLink(computeInHasLink(v));
|
|
119 wiki.setOutHasLink(computeOutHasLink(v));
|
|
120 wikiPageHash.put((String) v.getProperty(PAGE_TITLE), wiki);
|
4
|
121 AllVertexNumber++;
|
2
|
122 }
|
|
123 }
|
4
|
124 return AllVertexNumber;
|
2
|
125 }
|
|
126
|
|
127 void searchRegiNodes(Vertex v) {
|
|
128
|
|
129 if ( (v.getProperty(PAGE_TITLE) != null) &&
|
|
130 (v.getProperty(PAGE_RANK) != null)) {
|
|
131 WikiPage wiki = new WikiPage(v);
|
5
|
132 pageIdTable.put((String) v.getProperty(PAGE_TITLE), v.getId());
|
2
|
133 wiki.setInHasLink(computeInHasLink(v));
|
|
134 wiki.setOutHasLink(computeOutHasLink(v));
|
|
135 wikiPageHash.put((String) v.getProperty(PAGE_TITLE), wiki);
|
4
|
136 AllVertexNumber++;
|
2
|
137 }
|
|
138 }
|
|
139
|
|
140 HashMap<String, WikiPage> getWikiPageHash() {
|
|
141 return wikiPageHash;
|
|
142 }
|
|
143
|
5
|
144 HashMap<String, Object> getPageIdTable() {
|
2
|
145 return pageIdTable;
|
|
146 }
|
|
147
|
4
|
148 public Iterable<Vertex> getAllVertices() {
|
2
|
149 return graph.getVertices();
|
|
150 }
|
|
151
|
4
|
152 public void printAllVertices() {
|
2
|
153 for (Vertex v : graph.getVertices() ) {
|
|
154 System.out.println("ID = "+ v.getId());
|
|
155 for (String key: v.getPropertyKeys()) {
|
|
156 System.out.println(key + "=" + v.getProperty(key));
|
|
157 }
|
|
158 /*
|
|
159 for (Edge e : v.getEdges(Direction.IN, HAS_LINK) ) {
|
|
160 System.out.println();
|
|
161 }
|
|
162 */
|
|
163 }
|
|
164 System.out.println("--");
|
|
165
|
|
166 }
|
|
167
|
|
168 public long computeOutHasLink(Vertex v) {
|
|
169 long count = 0;
|
|
170 for (Edge edge : v.getEdges(Direction.OUT, HAS_LINK)) {
|
|
171 count++;
|
|
172 }
|
|
173 return count;
|
|
174 }
|
|
175
|
|
176 public long computeInHasLink(Vertex v) {
|
|
177 long count = 0;
|
|
178 for (Edge edge : v.getEdges(Direction.IN, HAS_LINK)) {
|
|
179 count++;
|
|
180 }
|
|
181 return count;
|
|
182 }
|
|
183
|
|
184 public void printOutHasLink(Vertex v, int depth) {
|
|
185 int numberOfLinkPages = 0;
|
4
|
186 String output = "title:"+v.getProperty(PAGE_TITLE) + " outHasLink pages:";
|
2
|
187 System.out.println(output);
|
|
188 for (Edge edge : v.getEdges(Direction.OUT, HAS_LINK)) {
|
|
189 Vertex outV = edge.getVertex(Direction.IN);
|
|
190 String str = (String) outV.getProperty(PAGE_TITLE);
|
|
191 System.out.println(str);
|
|
192 numberOfLinkPages++;
|
|
193 }
|
|
194 String numOutput = "Number of outHaslink pages: " + numberOfLinkPages;
|
|
195 System.out.println(numOutput);
|
|
196 }
|
|
197
|
|
198 public void printInHasLink(Vertex v, int depth) {
|
|
199 int numberOfLinkPages = 0;
|
4
|
200 String output = "title:" +v.getProperty(PAGE_TITLE) + " inHasLink pages:";
|
2
|
201 System.out.println(output);
|
|
202 for (Edge edge : v.getEdges(Direction.IN, HAS_LINK)) {
|
|
203 Vertex outV = edge.getVertex(Direction.OUT);
|
|
204 String str = (String) outV.getProperty(PAGE_TITLE);
|
|
205 System.out.println(str);
|
|
206 numberOfLinkPages++;
|
|
207 }
|
|
208 String numOutput = "Number of inHaslink pages: " + numberOfLinkPages + "\n";
|
|
209 System.out.println(numOutput);
|
|
210 }
|
|
211
|
|
212 public double computePageRank(Vertex v) {
|
7
|
213 double sum = 0.0;
|
|
214 double pageRank = 0.0;
|
2
|
215 String title = getPageTitle(v);
|
|
216 WikiPage wiki = wikiPageHash.get(title);
|
|
217
|
|
218 for (Edge edge : v.getEdges(Direction.IN, HAS_LINK) ) {
|
|
219 Vertex linkV = edge.getVertex(Direction.OUT);
|
9
|
220 assert computeOutHasLink(linkV)!=0 ;
|
|
221 double pr = (Double)linkV.getProperty(PAGE_RANK);
|
|
222 sum += (double) pr / computeOutHasLink(linkV) ;
|
2
|
223 }
|
9
|
224 double tmp = (double) 1 - weight;
|
|
225 pageRank = (double) tmp / AllVertexNumber
|
8
|
226 + (double) sum * weight;
|
7
|
227
|
2
|
228 wiki.setRank(pageRank);
|
|
229 v.setProperty(PAGE_RANK, pageRank);
|
|
230 return pageRank;
|
|
231 }
|
|
232
|
4
|
233 public void printVertexInfo(int nodeId) {
|
2
|
234 Vertex v = graph.getVertex(nodeId);
|
|
235 printInHasLink(v, 1);
|
|
236 printOutHasLink(v, 1);
|
|
237
|
|
238 String title = getPageTitle(v);
|
|
239 double rank = getPageRank(v);
|
|
240 long inHasLink = computeInHasLink(v);
|
|
241 long outHasLink = computeOutHasLink(v);
|
|
242
|
|
243 System.out.println("id:"+nodeId+" title:"+title+" rank:"+rank);
|
|
244 System.out.println("inHasLink:"+inHasLink+" outHasLink:"+outHasLink);
|
4
|
245 System.out.println();
|
2
|
246
|
|
247 }
|
|
248
|
|
249 }
|