-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVtoRDF.java
More file actions
78 lines (66 loc) · 2.3 KB
/
Copy pathCSVtoRDF.java
File metadata and controls
78 lines (66 loc) · 2.3 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package Assignment1;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import java.io.*;
import java.util.ArrayList;
public class CSVtoRDF {
private static final String file = "C:\\Users\\priya\\OneDrive\\Desktop\\WebSemantics\\src\\data.csv";
private ArrayList<String> data, actor, movie, country;
public static void main(String[] args) {
CSVtoRDF ttlObject = new CSVtoRDF();
ttlObject.readCSV();
ttlObject.convertToRDF();
}
void readCSV(){
BufferedReader reader = null;
String line = "";
data = new ArrayList<String>();
actor = new ArrayList<String>();
movie = new ArrayList<String>();
country = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(file));
while((line = reader.readLine()) != null) {
String[] row = line.split(",(?![ ])");
actor.add(row[0]);
movie.add(row[1]);
country.add(row[2]);
data.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void convertToRDF() {
Model model = ModelFactory.createDefaultModel();
String resource = "http:/moviesDB.com/resource/";
String ontology = "http:/moviesDB.com/ontology/";
Resource ActorResource = model.createResource(resource + "actor");
Property ActedIn = model.createProperty(ontology + "starred-in");
Property ReleasedIn = model.createProperty(ontology + "released-in");
Property rdfType = model.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#:type");
for(int i = 0; i<data.size()-1; i++) {
Resource actorResource = model.createResource(resource+actor.get(i).replaceAll(" ", "-"));
actorResource.addProperty(rdfType, ActorResource);
actorResource.addProperty(ActedIn, movie.get(i));
actorResource.addProperty(ReleasedIn, country.get(i));
}
try {
Writer writer = new FileWriter("C:\\Users\\priya\\OneDrive\\Desktop\\WebSemantics\\assignment-Par\\output.ttl");
model.write(writer,"TURTLE");
model.write(System.out,"TURTLE");}
catch(Exception exception) {
exception.printStackTrace();
}
}
}