Wednesday 15 August 2012

json - How can I split a line and assign index number for a graph's links connection in java? -



json - How can I split a line and assign index number for a graph's links connection in java? -

i need help generate graph's link connection in json format index numbers. can manage generate 1st part of nodes index numbers can't 2nd part of links index numbers. nodes index number should plotted links index no. please help.

input file:

abdelaziz bouteflika,bush,1

albert ii of belgium,bush,1

albert wehrer,bush,1

berlusconi,bush,1

bernard-montgomery,bush,1

bush,fidel-castro,1

bernard-montgomery,albert wehrer,5

expected output file:

{ "nodes":[ {"name":"bush","id":0}, {"name":"abdelaziz bouteflika","id":1}, {"name":"albert ii of belgium","id":2}, {"name":"albert wehrer","id":3}, {"name":"berlusconi","id":4}, {"name":"bernard-montgomery","id":5}, {"name":"fidel-castro","id":6} ], "links":[ {"source":1,"target":0}, {"source":2,"target":0}, {"source":3,"target":0}, {"source":4,"target":0}, {"source":5,"target":0}, {"source":6,"target":0}, {"source":5,"target":3} ] }

my code:

public class link_of_index { list<string> linklist1 = new arraylist<string>(); list<string> finallist = new arraylist<string>(); public void getindexno() throws ioexception{ bufferedreader reader = new bufferedreader(new filereader("e:/workspace/entity_graph_creation/webcontent/graph_nodes_1.csv")); filewriter fw = new filewriter(new file("e:/workspace/entity_graph_creation/input/links.json")); try{ string line = null; int index=0; while (( line = reader.readline()) != null) { string[] splits = line.split(","); linklist1.add(splits[0]); linklist1.add(splits[1]); linklist1.add(splits[2]); } (string s: linklist1) { if (!finallist.contains(s)) { finallist.add(s); jsonobject obj = new jsonobject(); obj.put("id", index); obj.put("name", s); fw.write(obj.tostring()+ ","+ "\n"); index ++; } fw.flush(); } } grab (ioexception ex){ ex.printstacktrace(); } } public static void main(string[] args) throws ioexception { link_of_index inx = new link_of_index(); inx.getindexno(); } }

edit: rewrote entire reply reflect new requirements. next time, should mention in first place, or create 2 seperate questions of it.

public class graphfileio { private static final comparator<node> node_comparator = new comparator<node>() { @override public int compare(node node1, node node2) { homecoming node1.compareto(node2); } }; private map<node, list<edge>> graph; private final file sourcefile; public graphfileio(final file psource) throws ioexception { if (psource.exists()) { sourcefile = psource; } else { throw new ioexception(); } } public void readgraph() throws ioexception { int index = 1; graph = new treemap<>(node_comparator); (string line : files.readalllines(sourcefile.topath(), charset.defaultcharset())) { if (line.trim().isempty()) { continue; // skip blank lines } // csv columns: // node 1, node 2, weight, event string[] splits = line.split(","); node n = new node(index, splits[0]); if (!graph.containskey(n)) { graph.put(n, new arraylist<edge>()); } n = new node(index, splits[0]); if (!graph.containskey(n)) { graph.put(n, new arraylist<edge>()); } border edge = new edge(splits[3]); (entry<node, list<edge>> entry : graph.entryset()) { node node = entry.getkey(); if (node.getname().equals(splits[0])) { edge.setsource(node.getid()); entry.getvalue().add(edge); } else if (node.getname().equals(splits[1])) { edge.settarget(node.getid()); // if edges bi-directional, uncomment next line of // code /* entry.getvalue().add(edge); */ } } } } public void writegraphtofile(final file targetfile) throws ioexception { jsonobject obj = new jsonobject(); jsonarray nodelist = new jsonarray(); jsonarray edgelist = new jsonarray(); (entry<node, list<edge>> entry : graph.entryset()) { jsonobject jsonnode = new jsonobject(); jsonnode.put("name", entry.getkey().getname()); jsonnode.put("id", entry.getkey().getid()); jsonnode.put("event", entry.getvalue()); nodelist.add(jsonnode); (edge link : entry.getvalue()) { jsonobject link = new jsonobject(); link.put("source", link.getsourceid()); link.put("target", link.gettargetid()); edgelist.add(link); } } obj.put("nodes", nodelist); obj.put("links", edgelist); filewriter fw = new filewriter(targetfile); fw.write(obj.tojsonstring()); fw.flush(); fw.close(); } public static void main(final string[] args) { file source = new file("c:\\sandbox\\src\\foo\\test.csv"); file target = new file("c:\\sandbox\\src\\foo\\testresult.csv"); graphfileio g; seek { g = new graphfileio(source); g.readgraph(); g.writegraphtofile(target); } grab (ioexception e) { e.printstacktrace(); } } } public class node implements comparable<node> { private final integer id; public integer getid() { homecoming id; } public string getname() { homecoming name; } private final string name; private final collection<string> events; public node(integer id, string name) { super(); this.id = id; this.name = name; this.events = new hashset<>(); } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashcode()); homecoming result; } public collection<string> getevents() { homecoming events; } @override public boolean equals(object obj) { if (this == obj) { homecoming true; } if (obj == null) { homecoming false; } if (getclass() != obj.getclass()) { homecoming false; } node other = (node) obj; if (name == null) { if (other.name != null) { homecoming false; } } else if (!name.equals(other.name)) { homecoming false; } homecoming true; } @override public int compareto(node o) { homecoming id.compareto(o.id); } } public class border { private final string event; private integer sourceid; private integer targetid; public edge(string string) { event = string; } public void setsource(integer id) { sourceid = id; } public void settarget(integer id) { targetid = id; } @override public string tostring() { homecoming event; } public integer getsourceid() { homecoming sourceid; } public integer gettargetid() { homecoming targetid; } public string getevent() { homecoming event; } }

java json

No comments:

Post a Comment