view src/wikigraph/ReadWikiLink.java @ 26:cfbab7d87188 draft

Page Rank in ascendiang order in ReadWikiLin.java
author one
date Fri, 31 Aug 2012 20:17:49 +0900
parents fbf0cf550b06
children 4c0b10bb5192
line wrap: on
line source

package wikigraph;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.traversal.Traverser;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.tooling.GlobalGraphOperations;

import wikigraph.LinkToNode.RelTypes;

public class ReadWikiLink {

	public static void main(String[] args) {

		GraphDatabaseService graphDb = new EmbeddedGraphDatabase("wikiLinkDB");
		GlobalGraphOperations graphOpe = GlobalGraphOperations.at(graphDb);
		LinkToNode ltn = new LinkToNode(graphDb, graphOpe);
		Transaction tx = graphDb.beginTx();

		try {

			// initALlNodePageRank();
			final long AllNodeNumber = ltn.searchAllNodes();
			HashMap<String, WikiPage> wikiHash = ltn.getWikiPageHash();

//			ltn.printNodeInfo(2);

			
			WikiPage maxW = new WikiPage();
			ArrayList<WikiPage> list = new ArrayList<WikiPage>();
			for (String title : wikiHash.keySet()) {
				WikiPage w = wikiHash.get(title);
				list.add(w);
				if (maxW.getRank() < w.getRank() ) {
					maxW = w;
				}
			}

			Collections.sort(list, new Comparator<WikiPage>(){
 				public int compare(WikiPage w1, WikiPage w2) {
					return (int)(w2.getRank()*Math.pow(10, 6)) - (int)(w1.getRank()*Math.pow(10,6));
 				}
			});
			
			FileOutputStream fos = new FileOutputStream("./resource/wikipage2.log");
			
			for (WikiPage w : list) {
				w.printInfo(fos);
			}
			fos.close();

			System.out.println("\nMax Page Rank");
			maxW.printInfo();
			

			
/*
			final int nodeIds[] = {4, 5, 6};
			LinkedList<FileOutputStream> fosList = new LinkedList<FileOutputStream>();
			for (int i: nodeIds) {
				String filename = String.format("./resource/NodeId_%d.dat", i);
				FileOutputStream fos = new FileOutputStream(filename);
				fos.write( String.format("# Node ID %d\n",i).getBytes());
				fosList.add(fos);
			}
			
			for (int i=0; i<50; i++) {
				for (Node node : graphOpe.getAllNodes()) {
					ltn.computePageRank(node);
				}

				for (int index=0; index<nodeIds.length; index++){
					FileOutputStream fos = fosList.get(index);
					printPageRankLog(fos, ltn, nodeIds[index], i);
				}
				
			}
			for (FileOutputStream fos: fosList) {
				fos.close();
			}
			*/

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			tx.success();
			tx.finish();
			graphDb.shutdown();
		}

	}
	
	public static void printPageRankLog(FileOutputStream fos, int x, double rank) throws IOException {
		fos.write( (x+" "+ rank+"\n").getBytes() );		
		fos.flush();
	}
	
	public static void printPageRankLog(FileOutputStream fos, LinkToNode ltn, int nodeId, int x) throws IOException {
		double rank = ltn.getPageRank(ltn.getNode(nodeId));
		fos.write( (x+" "+ rank+"\n").getBytes() );		
		fos.flush();
	}

}