Mercurial > hg > Members > nobuyasu > TPPageRank
view src/pagerank/LinkToVertex.java @ 2:1744340f8be6 draft
add some java files
author | one |
---|---|
date | Wed, 05 Sep 2012 11:56:21 +0900 |
parents | |
children | dcd59917a2dd |
line wrap: on
line source
package pagerank; import java.util.HashMap; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; import pagerank.WikiPage; public class LinkToVertex { Graph graph; public final static String PAGE_TITLE = "pageTitle"; public final static String PAGE_RANK = "pageRank"; private HashMap<String, Long> pageIdTable = new HashMap<String, Long>(); private HashMap<String, WikiPage> wikiPageHash = new HashMap<String, WikiPage>(); private long AllNodeNumber; private final double weight1 = 0.85; private final double weight2 = 0.15; public static final String HAS_LINK = "HasLink"; LinkToVertex(Graph graph) { this.graph = graph; AllNodeNumber = 0; } Long getId(String pageTitle) { return pageIdTable.get(pageTitle); } boolean isHasLink(String label) { return label.equals(HAS_LINK); } private Vertex createVertex() { return graph.addVertex(null); } private Vertex createVertex(Object id) { return graph.addVertex(id); } private Vertex createVertexWithProperty(String key, Object value) { Vertex v = graph.addVertex(null); v.setProperty(key,value); return v; } String getPageTitle(Vertex v) { return (String) v.getProperty(PAGE_TITLE); } 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()); return v; } Vertex setPageRank(Vertex v, Double rank) { v.setProperty(PAGE_RANK, rank); return v; } Vertex getVertex(String name) { long id = pageIdTable.get(name); return graph.getVertex(id); } Vertex getNode(int nodeId) { return graph.getVertex(nodeId); } Edge setRelationship(Vertex v1, Vertex v2, String label) { Edge e = graph.addEdge(null, v1, v2, label); return e; } Edge setHasLink(Vertex v1, Vertex v2) { return setRelationship(v1, v2, HAS_LINK); } long searchAllNodes() { AllNodeNumber = 0; for (Vertex v : graph.getVertices()) { if ( (v.getProperty(PAGE_TITLE) != null) && (v.getProperty(PAGE_RANK)) != null ) { WikiPage wiki = new WikiPage(v); pageIdTable.put((String) v.getProperty(PAGE_TITLE), (Long) v.getId()); wiki.setInHasLink(computeInHasLink(v)); wiki.setOutHasLink(computeOutHasLink(v)); wikiPageHash.put((String) v.getProperty(PAGE_TITLE), wiki); AllNodeNumber++; } } return AllNodeNumber; } void searchRegiNodes(Vertex v) { if ( (v.getProperty(PAGE_TITLE) != null) && (v.getProperty(PAGE_RANK) != null)) { WikiPage wiki = new WikiPage(v); pageIdTable.put((String) v.getProperty(PAGE_TITLE), (Long) v.getId()); wiki.setInHasLink(computeInHasLink(v)); wiki.setOutHasLink(computeOutHasLink(v)); wikiPageHash.put((String) v.getProperty(PAGE_TITLE), wiki); AllNodeNumber++; } } HashMap<String, WikiPage> getWikiPageHash() { return wikiPageHash; } HashMap<String, Long> getPageIdTable() { return pageIdTable; } public Iterable<Vertex> getAllNodes() { return graph.getVertices(); } public void printAllNodes() { for (Vertex v : graph.getVertices() ) { System.out.println("ID = "+ v.getId()); for (String key: v.getPropertyKeys()) { System.out.println(key + "=" + v.getProperty(key)); } /* for (Edge e : v.getEdges(Direction.IN, HAS_LINK) ) { System.out.println(); } */ } System.out.println("--"); } public long computeOutHasLink(Vertex v) { long count = 0; for (Edge edge : v.getEdges(Direction.OUT, HAS_LINK)) { count++; } return count; } public long computeInHasLink(Vertex v) { long count = 0; for (Edge edge : v.getEdges(Direction.IN, HAS_LINK)) { count++; } return count; } public void printOutHasLink(Vertex v, int depth) { int numberOfLinkPages = 0; String output = 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); String str = (String) outV.getProperty(PAGE_TITLE); System.out.println(str); numberOfLinkPages++; } String numOutput = "Number of outHaslink pages: " + numberOfLinkPages; System.out.println(numOutput); } public void printInHasLink(Vertex v, int depth) { int numberOfLinkPages = 0; String output = 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); String str = (String) outV.getProperty(PAGE_TITLE); System.out.println(str); numberOfLinkPages++; } String numOutput = "Number of inHaslink pages: " + numberOfLinkPages + "\n"; System.out.println(numOutput); } public double computePageRank(Vertex v) { double sum = 0; double pageRank = 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 (computeOutHasLink(v) == 0) { pageRank = (double) sum * weight1 + (double) ((double) 1 / AllNodeNumber * weight2); } else { pageRank = (double) ((double)sum / computeOutHasLink(v) * weight1) + (double) ((double) 1 / AllNodeNumber * weight2); } wiki.setRank(pageRank); v.setProperty(PAGE_RANK, pageRank); return pageRank; } public void printNodeInfo(int nodeId) { Vertex v = graph.getVertex(nodeId); printInHasLink(v, 1); printOutHasLink(v, 1); String title = getPageTitle(v); double rank = getPageRank(v); long inHasLink = computeInHasLink(v); long outHasLink = computeOutHasLink(v); System.out.println("id:"+nodeId+" title:"+title+" rank:"+rank); System.out.println("inHasLink:"+inHasLink+" outHasLink:"+outHasLink); } }