From 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 Mon Sep 17 00:00:00 2001 From: yzhou Date: Tue, 21 Apr 2015 10:34:27 +0100 Subject: initial version --- src/uk/ac/ox/cs/pagoda/util/TurtleHelper.java | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/uk/ac/ox/cs/pagoda/util/TurtleHelper.java (limited to 'src/uk/ac/ox/cs/pagoda/util/TurtleHelper.java') diff --git a/src/uk/ac/ox/cs/pagoda/util/TurtleHelper.java b/src/uk/ac/ox/cs/pagoda/util/TurtleHelper.java new file mode 100644 index 0000000..6887b9f --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/util/TurtleHelper.java @@ -0,0 +1,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(); + } + +} -- cgit v1.2.3