Mercurial > hg > Members > nobuyasu > TestNeo4j
changeset 23:21902773e530 draft
fix ReadWikiLink.java
author | one |
---|---|
date | Tue, 28 Aug 2012 14:38:13 +0900 |
parents | 2be3358689cb |
children | 71fe482aaf32 |
files | src/wikigraph/LinkToNode.java src/wikigraph/ReadWikiLink.java src/wikigraph/WikiPage.java |
diffstat | 3 files changed, 107 insertions(+), 58 deletions(-) [+] |
line wrap: on
line diff
--- a/src/wikigraph/LinkToNode.java Tue Aug 28 14:04:46 2012 +0900 +++ b/src/wikigraph/LinkToNode.java Tue Aug 28 14:38:13 2012 +0900 @@ -24,10 +24,11 @@ public final static String PAGE_TITLE = "page_title"; public final static String PAGE_RANK = "page_rank"; -// private HashSet<String> names = new HashSet<String>(); private HashMap<String,Long> pageIdTable = new HashMap<String,Long>(); private HashMap<String,Long> pageRankTable = new HashMap<String,Long>(); + private HashMap<String,WikiPage> wikiPageHash = new HashMap<String,WikiPage>(); + public enum RelTypes implements RelationshipType { HAS_LINK } @@ -117,6 +118,18 @@ return AllNodeNumber; } + long searchAllNodes() { + long AllNodeNumber = 0; + for (Node n: graphOpe.getAllNodes()) { + if (n.hasProperty(PAGE_TITLE)) { + WikiPage wiki = new WikiPage(n); + wikiPageHash.put((String)n.getProperty(PAGE_TITLE), wiki); + AllNodeNumber++; + } + } + return AllNodeNumber; + } + HashMap<String,Long> getPageIdTable() { return pageIdTable; } @@ -125,6 +138,10 @@ return pageRankTable; } + HashMap<String,WikiPage> getWikiPageHash() { + return wikiPageHash; + } + public void printAllNodes() { for (Node n: graphOpe.getAllNodes()) { System.out.println("ID="+ n.getId());
--- a/src/wikigraph/ReadWikiLink.java Tue Aug 28 14:04:46 2012 +0900 +++ b/src/wikigraph/ReadWikiLink.java Tue Aug 28 14:38:13 2012 +0900 @@ -16,68 +16,76 @@ public class ReadWikiLink { - public static void main(String[] args) { - GraphDatabaseService graphDb = new EmbeddedGraphDatabase("wikiLinkDB"); - GlobalGraphOperations graphOpe = GlobalGraphOperations.at(graphDb); + GlobalGraphOperations graphOpe = GlobalGraphOperations.at(graphDb); LinkToNode ltn = new LinkToNode(graphDb, graphOpe); - Transaction tx = graphDb. beginTx(); + Transaction tx = graphDb.beginTx(); try { - - final long AllNodeNumber = ltn.searchPageTitleAllNodes(); - - // Key: page_title value: Node ID - HashMap<String,Long> pageIdHash = ltn.getPageIdTable(); - HashMap<String,Long> pageRankHash = ltn.getPageRankTable(); - - long maxRelCount = 0; - String tmpKey=""; - // relHash record number of relationships of each node. - // Key: page_title value: number of relationships - HashMap<String,Long> relHash = new HashMap<String,Long>(); - - // relKeyHash - // key: number of relationships value: ID - HashMap<Long,Long> relKeyHash = new HashMap<Long,Long>(); - - for (String key : pageIdHash.keySet()) { - long id = pageIdHash.get(key); - Node node = graphDb.getNodeById(id); + +// final long AllNodeNumber = ltn.searchPageTitleAllNodes(); + final long AllNodeNumber = ltn.searchAllNodes(); + + long maxRelCount = 0; + String tmpKey = ""; + // relHash record number of relationships of each node. + // Key: page_title value: number of relationships + HashMap<String, Long> relHash = new HashMap<String, Long>(); + + // relKeyHash + // key: number of relationships value: ID + HashMap<Long, Long> relKeyHash = new HashMap<Long, Long>(); + + HashMap<String,WikiPage> wikiHash = ltn.getWikiPageHash(); + + for (String title: wikiHash.keySet()) { + WikiPage w = wikiHash.get(title); + long id = w.getId(); + + Node node = graphDb.getNodeById(id); + + Iterable<Relationship> relIter = node.getRelationships(); + long count = 0; + // compute number of relationship. + for (Relationship rel : relIter) { + if (ltn.isHasLink(rel)) + count++; + } + w.setOutLink(count); + + relHash.put(title, count); + + if (maxRelCount < count) { + maxRelCount = count; + tmpKey = title; + } + relKeyHash.put(count, id); + } - Iterable<Relationship> relIter = node.getRelationships(); - long count = 0; - for (Relationship rel : relIter) { - if ( ltn.isHasLink(rel)) - count++; - } - relHash.put(key, count); - - if (maxRelCount < count ) { - maxRelCount = count; - tmpKey = key; + for (String title: wikiHash.keySet()) { + System.out.println(title); } - relKeyHash.put(count, id); - } - - System.out.println("AllNodeNumber = "+ AllNodeNumber); - System.out.println("Most :\n"+tmpKey+":"+maxRelCount); - + + System.out.println("AllNodeNumber = " + AllNodeNumber); + System.out.println("Most :\n" + tmpKey + ":" + maxRelCount); - - String output = ""; - Node n = graphDb.getNodeById(1); - Traverser hasLinkTraverser = ltn.getInHasLink( n ); - System.out.println(n.getProperty(LinkToNode.PAGE_TITLE)); - for (Path hasLinkPath : hasLinkTraverser) { - output = "At depth" + hasLinkPath.length() + " => " - + hasLinkPath.endNode().getProperty(LinkToNode.PAGE_TITLE) + "\n"; + String output = ""; + Node n = graphDb.getNodeById(1); + Traverser hasLinkTraverser = ltn.getInHasLink(n); + System.out.println(n.getProperty(LinkToNode.PAGE_TITLE)); + for (Path hasLinkPath : hasLinkTraverser) { + if (hasLinkPath.length() > 1) break; + output = "At depth" + + hasLinkPath.length() + + " => " + + hasLinkPath.endNode().getProperty( + LinkToNode.PAGE_TITLE) + "\n"; + System.out.println(output); + } + System.out.println(output); - } - - System.out.println(output); } catch (Exception e) { e.printStackTrace(); @@ -86,7 +94,7 @@ tx.finish(); graphDb.shutdown(); } - + } - + }
--- a/src/wikigraph/WikiPage.java Tue Aug 28 14:04:46 2012 +0900 +++ b/src/wikigraph/WikiPage.java Tue Aug 28 14:38:13 2012 +0900 @@ -7,23 +7,31 @@ private String title; private long id; private long rank; + private long outLink; + private long inLink; WikiPage() { - this.title = ""; - this.id = 0; - this.rank = 0; + this.title = null; + this.id = -1; + this.rank = -1; + this.outLink = 0; + this.inLink = 0; } WikiPage(Node node) { this.title = (String) node.getProperty(LinkToNode.PAGE_TITLE); this.id = node.getId(); this.rank = (Long) node.getProperty(LinkToNode.PAGE_RANK); + this.outLink = 0; + this.inLink = 0; } WikiPage(String title, long id, long rank) { this.title = title; this.id = id; this.rank = rank; + this.outLink = 0; + this.inLink = 0; } String getTitle() { @@ -38,6 +46,14 @@ return rank; } + long getOutLink() { + return outLink; + } + + long getInLink() { + return inLink; + } + void setTitle(String title) { this.title = title; } @@ -49,5 +65,13 @@ void setRank(long rank) { this.rank = rank; } + + void setOutLink(long num) { + this.outLink = num; + } + + void setInLink(long num) { + this.inLink = num; + } }