# HG changeset patch # User one # Date 1346827166 -32400 # Node ID 8ea2212eaee038579c261d19035d91aa1db67af1 # Parent 140272228818a2738f1064f046e946ac9dfc2fc9 add TPReadWikiLink.java diff -r 140272228818 -r 8ea2212eaee0 src/pagerank/TPReadWikiLink.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pagerank/TPReadWikiLink.java Wed Sep 05 15:39:26 2012 +0900 @@ -0,0 +1,140 @@ +package pagerank; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedList; + +import com.tinkerpop.blueprints.Graph; +import com.tinkerpop.blueprints.Vertex; +import com.tinkerpop.blueprints.impls.tg.TinkerGraph; +import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader; +import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter; + +import pagerank.WikiPage; + +public class TPReadWikiLink { + + public static void main(String[] args) { + + final String fileDB = "./resources/tinkerpopDB"; + + + try { + Graph graph = new TinkerGraph(); + FileInputStream in = new FileInputStream(new File(fileDB)); + GraphMLReader.inputGraph(graph, in); + in.close(); + LinkToVertex ltv = new LinkToVertex(graph); + + final long AllVertexNumber = ltv.searchAllVertices(); + HashMap wikiHash = ltv.getWikiPageHash(); + System.out.println("AllVertexNumber = "+AllVertexNumber); + + + 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); + + } catch (NumberFormatException e){ + System.out.println("Program exit"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + + } + + } + + public static void loop(LinkToVertex ltv) throws IOException { + BufferedReader r = new BufferedReader( new InputStreamReader(System.in), 1); + System.out.print("\nPlease enter Node Id...>"); + System.out.flush(); + String s; + while ((s = r.readLine()) != null ) { + int nodeId = Integer.parseInt(s); + ltv.printVertexInfo(nodeId); + System.out.print("\nPlease enter Node Id...>"); + System.out.flush(); + } + } + + public static void writeComputeTransition(LinkToVertex ltv,final String nodeIds[], int count) throws IOException { + LinkedList fosList = new LinkedList(); + for (String i: nodeIds) { + String filename = "./resources/NodeId_"+i+".dat"; + FileOutputStream fos = null; + fos = new FileOutputStream(filename); + + Vertex v = ltv.getVertex(i); + fos.write( ("# Node ID "+i+" "+ (String)ltv.getPageTitle(v)).getBytes()); + fosList.add(fos); + } + + for (int i=0; i wikiHash , FileOutputStream fos) throws IOException { + ArrayList list = new ArrayList(); + for (String title : wikiHash.keySet()) { + WikiPage w = wikiHash.get(title); + list.add(w); + } + Collections.sort(list, new Comparator(){ + public int compare(WikiPage w1, WikiPage w2) { + return (int)(w2.getRank()*Math.pow(10, 10)) - (int)(w1.getRank()*Math.pow(10,10)); + } + }); + + for (WikiPage w : list) { + w.printInfo(fos); + } + fos.close(); + + } + + 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, LinkToVertex ltv, String nodeId, int x) throws IOException { + double rank = ltv.getPageRank(ltv.getNode(nodeId)); + fos.write( (x+" "+ rank+"\n").getBytes() ); + fos.flush(); + } + +}