伙计们,我一直致力于一个与语义网相关的项目,我现在真的需要帮助,因为我在阅读高级奖rdf数据集的RDF N-triples文件时完全被击中。
数据集来自以下链接。
Nobel prize rdf data description
我想使用jena从这个N-triples rdf数据文件中读取姓名,性别,国籍,纪律和年份,并希望以乌龟格式保存。
到目前为止,我能够阅读姓名和性别,但在阅读其他属性时却受到了打击。
我的java代码是这样的:
package com.tausy.rdf.query;
import java.io.InputStream;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.RDFS;
public class ParseNTripleRdf {
public static void main(String[] args) {
String fileNameOrUri = "././Nobeldump.nt";
Model model = ModelFactory.createDefaultModel();
InputStream is = FileManager.get().open(fileNameOrUri);
if (is != null) {
model.read(is, null, "N-TRIPLE");
//Resource person = model.getResource("http://xmlns.com/foaf/0.1/Person");
//Property firstName = model.createProperty("http://xmlns.com/foaf/0.1/name");
//String firstNameValue = person.getProperty(firstName).getString();
//System.out.println(firstNameValue);
modelStatements(model);
//model.write(System.out, "TURTLE");
} else {
System.err.println("cannot read " + fileNameOrUri);
}
}
public static void modelStatements(Model model){
// list the statements in the Model
StmtIterator iter = model.listStatements();
// print out the predicate, subject and object of each statement
while (iter.hasNext()) {
Statement stmt = iter.nextStatement(); // get next statement
Resource subject = stmt.getSubject(); // get the subject
Property predicate = stmt.getPredicate(); // get the predicate
RDFNode object = stmt.getObject(); // get the object
// Create some properties in advance for convenience.
final Property name = model.createProperty("http://xmlns.com/foaf/0.1/name");
final Property gender = model.createProperty("http://xmlns.com/foaf/0.1/gender");
final Property nationality = model.createProperty("http://dbpedia.org/ontology/country");
final Property discipline = model.createProperty("http://data.nobelprize.org/terms/category");
final Property year = model.createProperty("http://data.nobelprize.org/terms/year");
//System.out.println("Subject: "+subject.getURI());
//System.out.println("Predicate: " + predicate.getLocalName());
//System.out.println("Object: " + object.toString());
if (object instanceof Resource) {
Resource res = (Resource)object;
Statement s = res.getProperty(name);
final String nameVal = s==null ? null : s.getString();
s = res.getProperty(gender);
final String genderVal = s==null ? null : s.getString();
//s = res.getProperty(nationality);
//final String nationalityVal = s==null ? null : s.getString();
//s = res.getProperty(discipline);
//final String disciplineVal = s==null ? null : s.getString();
System.out.println(nameVal);
System.out.println(genderVal);
//System.out.println(nationalityVal);
//System.out.println(disciplineVal);
} else {
// object is a literal
System.out.print(" \"" + object.toString() + "\"");
}
}
}
}
我想查询这个turtle文件以获取简单的信息,如:
任何形式的帮助都将受到高度赞赏。谢谢!