伙计们,我一直在从事一个与语义网相关的项目,我现在真的需要帮助,因为我在阅读贵族奖品 rdf 数据集的 RDF N-triples 文件时完全被震惊了。
数据集取自以下链接。
我想使用 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() + "\"");
}
}
}
}
我想查询这个海龟文件以获取简单的信息,例如:
- 查找来自英国的所有诺贝尔奖获得者;
- 找出所有 1949 年以后出生的女性诺贝尔奖获得者;
- 列出所有诺贝尔奖获得者,按他们获奖的学科排序。按字母顺序列出姓名;
- 查找所有在美国出生并与他人分享该奖项的诺贝尔奖获得者;
任何形式的帮助将不胜感激。谢谢!