changeset 7:c7b139ff27e2 draft

compute PageRank. initial value is 1/ AllVertexNumber
author one
date Wed, 05 Sep 2012 16:34:47 +0900
parents 8ea2212eaee0
children 4d1885a2fa36
files src/pagerank/LinkConvertGraph.java src/pagerank/LinkToVertex.java src/pagerank/TPReadWikiLink.java
diffstat 3 files changed, 34 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/pagerank/LinkConvertGraph.java	Wed Sep 05 15:39:26 2012 +0900
+++ b/src/pagerank/LinkConvertGraph.java	Wed Sep 05 16:34:47 2012 +0900
@@ -90,8 +90,8 @@
 	
 	
 	public static void main(String[] args) {
-		final String filename = "./resources/article.xml";
-//		final String filename = "/Users/aotokage/testProgram/wiki/ja-pages_current.xml";
+//		final String filename = "./resources/article.xml";
+		final String filename = "/Users/aotokage/testProgram/wiki/ja-pages_current.xml";
 		
 		LinkConvertGraph lcg;
 
--- a/src/pagerank/LinkToVertex.java	Wed Sep 05 15:39:26 2012 +0900
+++ b/src/pagerank/LinkToVertex.java	Wed Sep 05 16:34:47 2012 +0900
@@ -14,8 +14,12 @@
 	Graph graph;
 	public final static String PAGE_TITLE = "pageTitle";
 	public final static String PAGE_RANK = "pageRank";
+	// pageIdTable
+	// key: pageTitle value: Vertex ID
 	private HashMap<String, Object> pageIdTable = new HashMap<String, Object>();
 
+	// wikiPageHash 
+	// key: pageTitle  value: wikiPage(class)
 	private HashMap<String, WikiPage> wikiPageHash = new HashMap<String, WikiPage>();
 	private long AllVertexNumber;
 
@@ -86,8 +90,8 @@
 		return graph.getVertex(id);
 	}
 	
-	Vertex getNode(String nodeId) {
-		return graph.getVertex(nodeId);
+	Vertex getVertexById(String id) {
+		return graph.getVertex(id);
 	}
 
 	Edge setRelationship(Vertex v1, Vertex v2, String label) {
@@ -201,23 +205,23 @@
 	}
 
 	public double computePageRank(Vertex v) {
-		double sum = 0;
-		double pageRank = 0;
+		double sum = 0.0;
+		double pageRank = 0.0;
 		String title = getPageTitle(v);
 		WikiPage wiki = wikiPageHash.get(title);
 
 		for (Edge edge : v.getEdges(Direction.IN, HAS_LINK) ) {
 			Vertex linkV = edge.getVertex(Direction.OUT); 
-			sum += (double) ((Double) linkV.getProperty(PAGE_RANK)) / computeInHasLink(linkV) ;
+			if (computeInHasLink(linkV) == 0) {
+				sum += (Double) linkV.getProperty(PAGE_RANK);
+			} else {
+				sum += ((Double) linkV.getProperty(PAGE_RANK)) / computeInHasLink(linkV) ;
+			}
 		}
-		
-		if (computeOutHasLink(v) == 0) {
-			pageRank = (double) sum * weight1
-					+ (double) ((double) 1 / AllVertexNumber * weight2);
-		} else {
-			pageRank = (double) ((double)sum / computeOutHasLink(v) * weight1)
-					+ (double) ((double) 1 / AllVertexNumber * weight2);
-		}
+	
+		pageRank = (double) sum * weight1
+				+ (double) ((double) 1 / AllVertexNumber * weight2);
+
 		wiki.setRank(pageRank);
 		v.setProperty(PAGE_RANK, pageRank);
 		return pageRank;
--- a/src/pagerank/TPReadWikiLink.java	Wed Sep 05 15:39:26 2012 +0900
+++ b/src/pagerank/TPReadWikiLink.java	Wed Sep 05 16:34:47 2012 +0900
@@ -34,24 +34,23 @@
 			GraphMLReader.inputGraph(graph, in);
 			in.close();
 			LinkToVertex ltv = new LinkToVertex(graph);
-			
+
 			final long AllVertexNumber = ltv.searchAllVertices();
 			HashMap<String, WikiPage> wikiHash = ltv.getWikiPageHash();
 			System.out.println("AllVertexNumber = "+AllVertexNumber);
+
+			String nodeIds[] = {"1574", "2829", "2850", "3618"};
+			writeComputeTransition(ltv, nodeIds, 50);
+			
+//			FileOutputStream fos = new FileOutputStream(new File("./resources/wikiPageRank.log"));
+//			descendingOrder(wikiHash, fos);			
 			
 			
-			for (Vertex v : graph.getVertices() ) {
-				String id = (String) v.getId();
-				System.out.println("id:"+id+" title:"+v.getProperty("pageTitle")+" rank:"+v.getProperty("pageRank"));
-			}			
-/*			
-			String nodeIds[] = {"1574", "2829", "2850", "3618"};
-			writeComputeTransition(ltv, nodeIds, 30);
-
+			
 			FileOutputStream out = new FileOutputStream(new File(fileDB));
 			GraphMLWriter.outputGraph(graph, out);
 			out.close();
-*/				
+
 			
 //			loop(ltv);
 			
@@ -80,13 +79,13 @@
 
 	public static void writeComputeTransition(LinkToVertex ltv,final String nodeIds[], int count) throws IOException {
 		LinkedList<FileOutputStream> fosList = new LinkedList<FileOutputStream>();
-		for (String i: nodeIds) {
-			String filename = "./resources/NodeId_"+i+".dat";
+		for (String id: nodeIds) {
+			String filename = "./resources/NodeId_"+id+".dat";
 			FileOutputStream fos = null;
 			fos = new FileOutputStream(filename);
 
-			Vertex v = ltv.getVertex(i);
-			fos.write( ("# Node ID "+i+" "+ (String)ltv.getPageTitle(v)).getBytes());
+			Vertex v = ltv.getVertexById(id);
+			fos.write( ("# Node ID "+id+" "+ ltv.getPageTitle(v)+"\n").getBytes());
 			fosList.add(fos);
 		}
 		
@@ -131,8 +130,8 @@
 		fos.flush();
 	}
 	
-	public static void printPageRankLog(FileOutputStream fos, LinkToVertex ltv, String nodeId, int x) throws IOException {
-		double rank = ltv.getPageRank(ltv.getNode(nodeId));
+	public static void printPageRankLog(FileOutputStream fos, LinkToVertex ltv, String id, int x) throws IOException {
+		double rank = ltv.getPageRank(ltv.getVertexById(id));
 		fos.write( (x+" "+ rank+"\n").getBytes() );		
 		fos.flush();
 	}