comparison src/wikigraph/ReadWikiLink.java @ 21:f9ef906676eb draft

modify LinkToNode.java
author one
date Tue, 28 Aug 2012 14:04:15 +0900
parents 2c3a10047ec6
children 21902773e530
comparison
equal deleted inserted replaced
20:2c3a10047ec6 21:f9ef906676eb
1 package wikigraph; 1 package wikigraph;
2 2
3 import java.util.HashMap; 3 import java.util.HashMap;
4 import java.util.Map;
4 5
5 import org.neo4j.graphdb.GraphDatabaseService; 6 import org.neo4j.graphdb.GraphDatabaseService;
6 import org.neo4j.graphdb.Node; 7 import org.neo4j.graphdb.Node;
8 import org.neo4j.graphdb.Path;
7 import org.neo4j.graphdb.Relationship; 9 import org.neo4j.graphdb.Relationship;
8 import org.neo4j.graphdb.Transaction; 10 import org.neo4j.graphdb.Transaction;
11 import org.neo4j.graphdb.traversal.Traverser;
9 import org.neo4j.kernel.EmbeddedGraphDatabase; 12 import org.neo4j.kernel.EmbeddedGraphDatabase;
13 import org.neo4j.tooling.GlobalGraphOperations;
14
15 import wikigraph.LinkToNode.RelTypes;
10 16
11 public class ReadWikiLink { 17 public class ReadWikiLink {
12 18
13 19
14 public static void main(String[] args) { 20 public static void main(String[] args) {
15 21
16 22
17 GraphDatabaseService graphDb = new EmbeddedGraphDatabase("wikiLinkDB"); 23 GraphDatabaseService graphDb = new EmbeddedGraphDatabase("wikiLinkDB");
18 LinkToNode ltn = new LinkToNode(graphDb); 24 GlobalGraphOperations graphOpe = GlobalGraphOperations.at(graphDb);
25 LinkToNode ltn = new LinkToNode(graphDb, graphOpe);
19 Transaction tx = graphDb. beginTx(); 26 Transaction tx = graphDb. beginTx();
27
28 try {
29
30 final long AllNodeNumber = ltn.searchPageTitleAllNodes();
20 31
21 // ltn.printAllNodes(); 32 // Key: page_title value: Node ID
33 HashMap<String,Long> pageIdHash = ltn.getPageIdTable();
34 HashMap<String,Long> pageRankHash = ltn.getPageRankTable();
22 35
23 ltn.searchPageTitleAllNodes(); 36 long maxRelCount = 0;
24 HashMap<String,Long> hash = ltn.getNamesTable();
25
26
27 long lastNodeId = 0;
28 // Print out page title and ID of All Node.
29 for (String key : hash.keySet()) {
30 // System.out.println("page title = "+key + "\nID = " + hash.get(key));
31 lastNodeId = Math.max(hash.get(key), lastNodeId);
32 }
33
34 long relCount = 0;
35 String tmpKey=""; 37 String tmpKey="";
36 // relHash record number of relationships of each node. 38 // relHash record number of relationships of each node.
37 // Key: page_title value: number of relationships 39 // Key: page_title value: number of relationships
38 HashMap<String,Long> relHash = new HashMap<String,Long>(); 40 HashMap<String,Long> relHash = new HashMap<String,Long>();
39 41
40 // relKeyHash 42 // relKeyHash
41 // key: number of relationships value: ID 43 // key: number of relationships value: ID
42 HashMap<Long,Long> relKeyHash = new HashMap<Long,Long>(); 44 HashMap<Long,Long> relKeyHash = new HashMap<Long,Long>();
43 45
44 for (String key : hash.keySet()) { 46 for (String key : pageIdHash.keySet()) {
45 long id = hash.get(key); 47 long id = pageIdHash.get(key);
46 Node node = graphDb.getNodeById(id); 48 Node node = graphDb.getNodeById(id);
47 49
48 Iterable<Relationship> relIter = node.getRelationships(); 50 Iterable<Relationship> relIter = node.getRelationships();
49 long count = 0; 51 long count = 0;
50 for (Relationship rel : relIter) { 52 for (Relationship rel : relIter) {
51 count++; 53 if ( ltn.isHasLink(rel))
54 count++;
52 } 55 }
53 relHash.put(key, count); 56 relHash.put(key, count);
54 57
55 if (relCount < count ) { 58 if (maxRelCount < count ) {
56 relCount = count; 59 maxRelCount = count;
57 tmpKey = key; 60 tmpKey = key;
58 } 61 }
59 relKeyHash.put(count, id); 62 relKeyHash.put(count, id);
60 } 63 }
61 64
62 for (String key : relHash.keySet()) { 65 System.out.println("AllNodeNumber = "+ AllNodeNumber);
63 // System.out.println(key + ":"+relHash.get(key)); 66 System.out.println("Most :\n"+tmpKey+":"+maxRelCount);
64 }
65 System.out.println("lastNodeId = "+ lastNodeId);
66 System.out.println("Most :\n"+tmpKey+":"+relCount);
67 67
68 68
69 for (long i = relKeyHash.size()-1 ; i>0 ; i-- ) { 69
70 long id = relKeyHash.get(i); 70 String output = "";
71 Node node = graphDb.getNodeById(id); 71 Node n = graphDb.getNodeById(1);
72 System.out.prinln(); 72 Traverser hasLinkTraverser = ltn.getInHasLink( n );
73 System.out.println(n.getProperty(LinkToNode.PAGE_TITLE));
74 for (Path hasLinkPath : hasLinkTraverser) {
75 output = "At depth" + hasLinkPath.length() + " => "
76 + hasLinkPath.endNode().getProperty(LinkToNode.PAGE_TITLE) + "\n";
77 System.out.println(output);
73 } 78 }
74 79
75 80 System.out.println(output);
76 tx.success(); 81
77 tx.finish(); 82 } catch (Exception e) {
78 graphDb.shutdown(); 83 e.printStackTrace();
84 } finally {
85 tx.success();
86 tx.finish();
87 graphDb.shutdown();
88 }
79 89
80 } 90 }
81 91
82 } 92 }