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