爲了完整(因爲OP問幾個API)來我重複@Joshua泰勒的解決方案,但使用OpenRDF芝麻API,而不是耶拿:
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.openrdf.model.Model;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.LinkedHashModel;
import org.openrdf.model.impl.ValueFactoryImpl;
import org.openrdf.model.util.Literals;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.RDFS;
import org.openrdf.model.vocabulary.XMLSchema;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.Rio;
public class OpenIETripletConversionExample {
public static void main(String[] args) throws UnsupportedEncodingException, RDFHandlerException {
// Create the model and define some prefixes (for nice serialization in RDF/XML and TTL)
Model model = new LinkedHashModel();
String NS = "http://stackoverflow.com/q/24897405/1281433/";
model.setNamespace("rdf", RDF.NAMESPACE);
model.setNamespace("rdfs", RDFS.NAMESPACE);
model.setNamespace("xsd", XMLSchema.NAMESPACE);
model.setNamespace("", NS);
// Create a ValueFactory we can use to create resources and statements
ValueFactory vf = ValueFactoryImpl.getInstance();
// Preserve the confidence level (optional).
URI confidence = vf.createURI(NS, "confidence");
// Define some triplets to convert.
Object[][] triplets = {
{ 0.57, "The quick brown fox", "jumped", "over the lazy dog." },
{ 0.93, "The rail launchers", "are", "conceptually similar to the underslung SM-1." }
};
// For each triplet, create a resource representing the sentence, as well as the subject,
// predicate, and object, and then add the triples to the model.
for (Object[] triplet : triplets) {
Resource sentence = vf.createBNode();
Resource subject = vf.createBNode();
URI predicate = vf.createURI(NS, URLEncoder.encode((String) triplet[2], "utf-8"));
Resource object = vf.createBNode();
model.add(subject, RDFS.LABEL, Literals.createLiteral(vf, triplet[1]));
model.add(object, RDFS.LABEL, Literals.createLiteral(vf, (String)triplet[3]));
model.add(sentence, confidence, Literals.createLiteral(vf, triplet[0]));
model.add(sentence, RDF.SUBJECT, subject);
model.add(sentence, RDF.PREDICATE, predicate);
model.add(sentence, RDF.OBJECT, object);
}
// Show the model in a few different formats.
Rio.write(model, System.out, RDFFormat.TURTLE);
Rio.write(model, System.out, RDFFormat.RDFXML);
Rio.write(model, System.out, RDFFormat.NTRIPLES);
}
}
我看到了你以前的問題,但這裏沒有足夠的自足。例如,你說你想要「爲文件中的每個不同主體創建一個空白節點標識符」,但是你沒有說明「文件中的不同:主體」是什麼。由於您在詢問如何使用其中一個庫生成某些特定輸出,因此首先生成一些示例輸出。例如,你給我們看的句子應該產生的RDF是什麼?是(......; ...; ...)應該映射到三元組,還是僅僅是這個特定數據的幸運事故? –
你想要輸出或多或少像http://pastebin.com/sFwfqLFM? –
我的意思是應該爲上例中的句子生成的RDF。 –