Mercurial > hg > Members > nobuyasu > TPPageRank
changeset 4:dcd59917a2dd draft
fix LinkToVertex
author | one |
---|---|
date | Wed, 05 Sep 2012 12:44:08 +0900 |
parents | b44abb9aa09f |
children | 140272228818 |
files | src/pagerank/LinkConvertGraph.java src/pagerank/LinkToVertex.java src/sample/CreateTinkerGraph.java |
diffstat | 3 files changed, 53 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- a/src/pagerank/LinkConvertGraph.java Wed Sep 05 11:59:02 2012 +0900 +++ b/src/pagerank/LinkConvertGraph.java Wed Sep 05 12:44:08 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; @@ -101,17 +101,17 @@ lcg.parseXml(); // lcg.printHash(); - + FileOutputStream fos = new FileOutputStream("./resources/wikiLink.log"); lcg.printHash(fos); HashMap<String,HashSet<String>> hash = lcg.getHash(); - final String filenameD = "./resources/tinkerpopDB"; + final String fileDB = "./resources/tinkerpopDB"; Graph graph = new TinkerGraph(); - FileOutputStream out = new FileOutputStream(new File(filename)); + FileOutputStream out = new FileOutputStream(new File(fileDB)); LinkToVertex ltn = new LinkToVertex(graph); for (Map.Entry<String, HashSet<String>> map : hash.entrySet()) {
--- a/src/pagerank/LinkToVertex.java Wed Sep 05 11:59:02 2012 +0900 +++ b/src/pagerank/LinkToVertex.java Wed Sep 05 12:44:08 2012 +0900 @@ -17,31 +17,31 @@ private HashMap<String, Long> pageIdTable = new HashMap<String, Long>(); private HashMap<String, WikiPage> wikiPageHash = new HashMap<String, WikiPage>(); - private long AllNodeNumber; + private long AllVertexNumber; private final double weight1 = 0.85; private final double weight2 = 0.15; public static final String HAS_LINK = "HasLink"; - LinkToVertex(Graph graph) { + public LinkToVertex(Graph graph) { this.graph = graph; - AllNodeNumber = 0; + AllVertexNumber = 0; } - Long getId(String pageTitle) { + public Long getId(String pageTitle) { return pageIdTable.get(pageTitle); } - boolean isHasLink(String label) { + public boolean isHasLink(String label) { return label.equals(HAS_LINK); } - private Vertex createVertex() { + public Vertex createVertex() { return graph.addVertex(null); } - private Vertex createVertex(Object id) { + public Vertex createVertex(Object id) { return graph.addVertex(id); } @@ -51,17 +51,18 @@ return v; } - String getPageTitle(Vertex v) { + public String getPageTitle(Vertex v) { return (String) v.getProperty(PAGE_TITLE); } - Double getPageRank(Vertex v) { + public Double getPageRank(Vertex v) { return (Double) v.getProperty(PAGE_RANK); } Vertex createVertexWithPageTitle(String pageTitle) { Vertex v = createVertexWithProperty(PAGE_TITLE, pageTitle); - pageIdTable.put(pageTitle, (Long) v.getId()); + String id = (String)v.getId(); + pageIdTable.put(pageTitle, Long.parseLong(id) ); return v; } @@ -88,8 +89,8 @@ return setRelationship(v1, v2, HAS_LINK); } - long searchAllNodes() { - AllNodeNumber = 0; + long searchAllVertices() { + AllVertexNumber = 0; for (Vertex v : graph.getVertices()) { if ( (v.getProperty(PAGE_TITLE) != null) && (v.getProperty(PAGE_RANK)) != null ) { @@ -98,10 +99,10 @@ wiki.setInHasLink(computeInHasLink(v)); wiki.setOutHasLink(computeOutHasLink(v)); wikiPageHash.put((String) v.getProperty(PAGE_TITLE), wiki); - AllNodeNumber++; + AllVertexNumber++; } } - return AllNodeNumber; + return AllVertexNumber; } void searchRegiNodes(Vertex v) { @@ -113,7 +114,7 @@ wiki.setInHasLink(computeInHasLink(v)); wiki.setOutHasLink(computeOutHasLink(v)); wikiPageHash.put((String) v.getProperty(PAGE_TITLE), wiki); - AllNodeNumber++; + AllVertexNumber++; } } @@ -125,11 +126,11 @@ return pageIdTable; } - public Iterable<Vertex> getAllNodes() { + public Iterable<Vertex> getAllVertices() { return graph.getVertices(); } - public void printAllNodes() { + public void printAllVertices() { for (Vertex v : graph.getVertices() ) { System.out.println("ID = "+ v.getId()); for (String key: v.getPropertyKeys()) { @@ -163,7 +164,7 @@ public void printOutHasLink(Vertex v, int depth) { int numberOfLinkPages = 0; - String output = v.getProperty(PAGE_TITLE) + " outHasLink pages:"; + String output = "title:"+v.getProperty(PAGE_TITLE) + " outHasLink pages:"; System.out.println(output); for (Edge edge : v.getEdges(Direction.OUT, HAS_LINK)) { Vertex outV = edge.getVertex(Direction.IN); @@ -177,7 +178,7 @@ public void printInHasLink(Vertex v, int depth) { int numberOfLinkPages = 0; - String output = v.getProperty(PAGE_TITLE) + " inHasLink pages:"; + String output = "title:" +v.getProperty(PAGE_TITLE) + " inHasLink pages:"; System.out.println(output); for (Edge edge : v.getEdges(Direction.IN, HAS_LINK)) { Vertex outV = edge.getVertex(Direction.OUT); @@ -202,17 +203,17 @@ if (computeOutHasLink(v) == 0) { pageRank = (double) sum * weight1 - + (double) ((double) 1 / AllNodeNumber * weight2); + + (double) ((double) 1 / AllVertexNumber * weight2); } else { pageRank = (double) ((double)sum / computeOutHasLink(v) * weight1) - + (double) ((double) 1 / AllNodeNumber * weight2); + + (double) ((double) 1 / AllVertexNumber * weight2); } wiki.setRank(pageRank); v.setProperty(PAGE_RANK, pageRank); return pageRank; } - public void printNodeInfo(int nodeId) { + public void printVertexInfo(int nodeId) { Vertex v = graph.getVertex(nodeId); printInHasLink(v, 1); printOutHasLink(v, 1); @@ -224,7 +225,7 @@ System.out.println("id:"+nodeId+" title:"+title+" rank:"+rank); System.out.println("inHasLink:"+inHasLink+" outHasLink:"+outHasLink); - + System.out.println(); }
--- a/src/sample/CreateTinkerGraph.java Wed Sep 05 11:59:02 2012 +0900 +++ b/src/sample/CreateTinkerGraph.java Wed Sep 05 12:44:08 2012 +0900 @@ -2,31 +2,31 @@ import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import pagerank.LinkToVertex; + import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.tg.TinkerGraph; -import com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory; import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader; import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter; public class CreateTinkerGraph { - public static final String filename = "./resources/tinkerpopDB"; + public static final String filename = "./resources/testTinkerpopDB"; public static void main(String[] args) { - try { - outputGraph(); - readGraph(); - +// outputGraph(); +// readGraph(); + readPageRankDB(); + } catch (IOException e) { e.printStackTrace(); } @@ -55,10 +55,7 @@ Vertex v =edge.getVertex(Direction.OUT); System.out.println(v.getProperty("name")); } - - GraphMLWriter.outputGraph(graph, out); - } public static void readGraph() throws IOException { @@ -76,5 +73,22 @@ } + public static void readPageRankDB() throws IOException { + final String pageRankDB = "./resources/tinkerpopDB"; + Graph graph = new TinkerGraph(); + FileInputStream in = new FileInputStream(new File(pageRankDB)); + GraphMLReader.inputGraph(graph, in); + + LinkToVertex ltn = new LinkToVertex(graph); + + System.out.println("print All Vertex "); + for (Vertex v : graph.getVertices() ) { + if ( (ltn.getPageTitle(v) == null ) || + (ltn.getPageRank(v) == null)) continue; + String id = (String) v.getId(); + int nodeId = Integer.parseInt(id); + ltn.printVertexInfo(nodeId); + } + } }