aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/util/TurtleHelper.java
blob: 6887b9f27dd5fae400140843089516fa61eeece1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package uk.ac.ox.cs.pagoda.util;

import java.io.*;

public class TurtleHelper {

	public static void simplify(String tempFile, String outputPath) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath)));

		String line, sub = null, pred = null, obj = null;
		char lastSymbol = '.', symbol; 
		String[] seg;
		while ((line = reader.readLine()) != null) {
			if (line.trim().isEmpty() || line.startsWith("#") || line.startsWith("@base")) 
				continue;
			
			if (line.startsWith("@")) {
				writer.write(line);
				writer.newLine();
				continue;
			}
				
			
			symbol = line.charAt(line.length() - 1);
			
			if (lastSymbol == '.') {
				seg = line.split(" ");
				sub = seg[0];
				pred = seg[1];
				obj = seg[2];
			}
			else if (lastSymbol == ';') {
				line = line.substring(sub.length() + 1);
				seg = line.split(" ");
				pred = seg[0];
				obj = seg[1];
			}
			else if (lastSymbol == ',') {
				line = line.substring(sub.length() + pred.length() + 2);
				obj = line.substring(0, line.lastIndexOf(' '));
			}
			else Utility.logError("ERROR");

			lastSymbol = symbol;
			if (pred.equals("rdf:type") && obj.startsWith("owl:"))
				continue;
			
			writer.write(sub + " " + pred + " " + obj + " .\n");
		}
		
		reader.close();
		writer.close();
	}

}