将RDF格式的字符串写入芝麻存储库



我不知道怎么把几个RDF N3格式的字符串写进我的芝麻仓库。我有以下ArrayList<String>,它包含每个索引的一行以下文本(一行以;结尾):

@prefix gstruct: <http://cs.lth.se/ontologies/gstruct.owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
gstruct:sentence_1  rdf:type    gstruct:Sentence ;
    gstruct:inx 1 ;
    gstruct:words
        [gstruct:id 1 ; gstruct:form "The" ; gstruct:lemma "the" ; gstruct:pos "DT" ; gstruct:head "4" ; gstruct:deprel "NMOD"],
        [gstruct:id 2 ; gstruct:form "Series" ; gstruct:lemma "series" ; gstruct:pos "NNP" ; gstruct:head "3" ; gstruct:deprel "NMOD"],
        [gstruct:id 3 ; gstruct:form "800" ; gstruct:lemma "800" ; gstruct:pos "CD" ; gstruct:head "4" ; gstruct:deprel "NMOD"],
        [gstruct:id 4 ; gstruct:form "Terminator" ; gstruct:lemma "terminator" ; gstruct:pos "NNP" ; gstruct:head "5" ; gstruct:deprel "SBJ"],
        [gstruct:id 5 ; gstruct:form "is" ; gstruct:lemma "be" ; gstruct:pos "VBZ" ; gstruct:head "0" ; gstruct:deprel "ROOT"],
        [gstruct:id 6 ; gstruct:form "a" ; gstruct:lemma "a" ; gstruct:pos "DT" ; gstruct:head "7" ; gstruct:deprel "NMOD"],
        [gstruct:id 7 ; gstruct:form "type" ; gstruct:lemma "type" ; gstruct:pos "NN" ; gstruct:head "5" ; gstruct:deprel "PRD"],
        [gstruct:id 8 ; gstruct:form "of" ; gstruct:lemma "of" ; gstruct:pos "IN" ; gstruct:head "7" ; gstruct:deprel "NMOD"],
        [gstruct:id 9 ; gstruct:form "Terminator" ; gstruct:lemma "terminator" ; gstruct:pos "NN" ; gstruct:head "10" ; gstruct:deprel "NMOD"],
        [gstruct:id 10 ; gstruct:form "mass" ; gstruct:lemma "mass" ; gstruct:pos "NN" ; gstruct:head "8" ; gstruct:deprel "PMOD"],
        [gstruct:id 11 ; gstruct:form "produced" ; gstruct:lemma "produce" ; gstruct:pos "VBN" ; gstruct:head "7" ; gstruct:deprel "APPO"],
        [gstruct:id 12 ; gstruct:form "by" ; gstruct:lemma "by" ; gstruct:pos "IN" ; gstruct:head "11" ; gstruct:deprel "LGS"],
        [gstruct:id 13 ; gstruct:form "Skynet" ; gstruct:lemma "skynet" ; gstruct:pos "NNP" ; gstruct:head "12" ; gstruct:deprel "PMOD"],
        [gstruct:id 14 ; gstruct:form "." ; gstruct:lemma "." ; gstruct:pos "." ; gstruct:head "5" ; gstruct:deprel "P"];
    gstruct:predicates
        [gstruct:id 7 ; gstruct:predsense "type.01" ; gstruct:args
            [gstruct:id 7 ;  gstruct:argtype "A2"],
            [gstruct:id 8 ;  gstruct:argtype "A1"]],
        [gstruct:id 10 ; gstruct:predsense "mass.02" ; gstruct:args
            [gstruct:id 9 ;  gstruct:argtype "A1"]],
        [gstruct:id 11 ; gstruct:predsense "produce.01" ; gstruct:args
            [gstruct:id 7 ;  gstruct:argtype "A1"],
            [gstruct:id 12 ;  gstruct:argtype "A0"]].

我想使用RepositoryConnection添加方法来添加这个,还添加一个URI和URL形式的上下文。我设法弄清楚如何创建URI,但我不能设法弄清楚我上面提到的内容,以及如何使我的URL是一个字符串到资源对象。

我不知道为什么您将其作为List,每行作为单独的条目,因为这是处理它的一种相当尴尬的方式。首先,正如约书亚所说,行是相互依赖的:虽然整体可能是合法的N3语法,但每一行本身并不是合法的N3字符串。

将这些数据添加到Sesame存储库的最简单方法可能是将其转换为单个String,然后按如下方式加载:

String data = "@prefix gstruct: ..."; // full N3 data
conn.add(new StringReader(data), "", RDFFormat.N3);

如果您想将数据添加到存储库中的特定上下文中:

 URI context = conn.getValueFactory().createURI("http://example.org/context");
 conn.add(new StringReader(data), "", RDFFormat.N3, context);

假设N3数据实际上来自磁盘上的文件,您可以进一步优化:而不是先创建String对象,然后使用StringReader,您可以只输入文件对象本身:

File file = new File("/path/to/file.n3");
conn.add(file, "", RDFFormat.N3, context);
顺便说一句:N3语法是一种不完全标准化的格式,您可能会发现不同的解析器/编写器支持的N3语法子集略有不同。您发布的数据看起来并不奇怪,所以您应该对此感到满意,但如果您有选择,我建议您切换到更完全标准化的不同语法格式,如Turtle或N-Triples。

Turtle语法实际上与N3非常相似(您发布的数据是有效的Turtle as-is),因此切换应该只需要很少的更改。

最新更新