source: branches/localSpectral/src/data/GraphBuilder.java @ 34

Last change on this file since 34 was 34, checked in by toshi, 14 years ago
File size: 2.1 KB
Line 
1
2package data;
3
4import clustering.Edge;
5import clustering.VertexString;
6import edu.uci.ics.jung.graph.DirectedSparseGraph;
7import edu.uci.ics.jung.graph.Graph;
8import edu.uci.ics.jung.graph.SparseGraph;
9import java.io.BufferedInputStream;
10import java.io.BufferedReader;
11import java.io.DataInputStream;
12import java.io.FileInputStream;
13import java.io.FileNotFoundException;
14import java.io.IOException;
15import java.io.InputStreamReader;
16import java.util.Arrays;
17import java.util.HashSet;
18import java.util.Set;
19
20
21public class GraphBuilder {
22
23    Graph<String,Edge<String>> graph;
24
25    public GraphBuilder(boolean directed){
26        if (directed)
27           graph = new DirectedSparseGraph<String, Edge<String>>();
28
29        else
30            graph = new SparseGraph<String, Edge<String>>();
31    }
32
33    public Graph<String, Edge<String>> getGraph() {
34        return graph;
35    }
36
37    public void buildGraphFromARFF(String path, int maxreadline){
38        try {
39            FileInputStream fstream = new FileInputStream(path);
40            DataInputStream in = new DataInputStream(fstream);
41            BufferedReader br = new BufferedReader(new InputStreamReader(in));
42
43            Set<String> vertex = new HashSet<String>();
44
45            String read;
46            int edge=0;
47            int count=0;
48            while(((read = br.readLine()) != null) && (count < maxreadline)){
49                count++;
50                if(!(read.contains("@DATA") || read.contains("@RELATION") || read.contains("@ATTRIBUTE") || read.trim().isEmpty())){
51                    String[] splitted = read.trim().split(",");
52                    vertex.addAll(Arrays.asList(splitted));
53                    graph.addEdge(new Edge<String>(splitted[0], splitted[1]), splitted[0], splitted[1]);
54                    edge++;
55                }
56            }
57            br.close();
58            in.close();
59            fstream.close();
60
61        } catch (FileNotFoundException e) {
62            System.out.println("<GraphBuilder> Error: file not found!");
63        } catch(IOException e){
64            System.out.println("<GraphBuilder> Error: closing FileInputStream");
65        }
66    }
67
68}
Note: See TracBrowser for help on using the repository browser.