【问题标题】:Storing triples efficiently有效存储三元组
【发布时间】:2026-02-16 08:20:10
【问题描述】:

我有一个 Tera Byte 大文件。我想将它从nt转换为n3。这样做的原因是,我有一个大文件,由于附加的命名空间而占用了大量空间:

# <1>
<file:///home//uniprot/uniprot.rdf>    <http://www.w3.org/2002/07/owl#imports> <http://purl.uniprot.org/core/> .
# <2>
<http://purl.uniprot.org/uniprot/Q6GZX4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.uniprot.org/core/Protein> .
# <3>
<http://purl.uniprot.org/uniprot/Q6GZX4> <http://purl.uniprot.org/core/reviewed> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .

现在我想以压缩形式有效地存储这个文件:

@fileuniprot: <file:///home//uniprot/>.
@owl: <http://www.w3.org/2002/07/owl#>.
@purlUniprot: <http://purl.uniprot.org/>.
@rdfs: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@xsd: <http://www.w3.org/2001/XMLSchema#>.
@xsd: 
# <1>
fileuniprot:uniprot.rdf    owl:imports purlUniprot:core .
# <2>
purlUniprot:uniprot/Q6GZX4 rdfs:type purlUniprot:core/Protein .
# <3>
purlUniprot:Q6GZX4 purlUniprot:core/reviewed "true"^^ xsd:boolean .

即我不希望命名空间附加相应的三元组。虽然我想保留cmets。有没有可能这样做。如果是,那么有人可以建议一个有效的工具来做同样的事情。

如果我能在 python 或 java 中找到一些适用于 linux 的工具,那就太好了? 以上都是手动完成的,如果能自动转换就好了。

【问题讨论】:

  • 你真的只是一个字符串 => 字符串映射。其实很简单的东西。由于您将&lt;http://www.w3.org/2002/07/owl#imports&gt; 映射到&lt;imports&gt;&lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&gt;&lt;type&gt;
  • 如果 xml 中除了简单的字符串转换之外还有其他更改,您可以使用 XSLT transformerSAX parser。也看看这个问题:*.com/questions/2773733/…
  • @Cruncher 实际上我必须找到常见的模式然后替换它们。有什么方法可以让我找到常见的模式并以编程方式替换它们
  • 这似乎是文本压缩通常可以很好地处理的那种转换。您可以根据需要使用 gzip 和 zcat 吗?
  • @loopasam,是的,但我回答了下面的问题;)

标签: java python linux semantics semantic-web


【解决方案1】:

您可能需要考虑hdt 以获得非常好的压缩效果。 您可以将 uniprot 文件改回使用 gzip 压缩的 rdf/xml,并将大小减少至少 25 倍。(bzip2 将给出 30)我建议使用 pbzip2 以获得最佳效果。

如果您确实想使用海龟语法进行某些压缩,则使用来自sesame RIOjena RIOT 的现有代码或来自librdf 的说唱歌手

问题是您为什么要以 nt 开头的文件?

您实际考虑使用的文件格式称为turtle。 N3 是海龟加规则,这个规则部分实际上并没有在 UniProt 数据集中使用,并且不属于 RDF/triples。

rapper -i ntriples -o turtle ~/uniprot.nt  > ~/uniprot.ttl

忘记 N3 阅读海龟吧。

【讨论】:

  • 感谢您的回复。当然我可以使用 rdflib 中的 rapper。但我不知道如何使用说唱歌手从 nt 转换为 .n3