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

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