rdflib的存储后端,允许读取和查询HDT文档

rdflib-hdt的Python项目详细描述


rdflib-htd logo

Build StatusPyPI version

rdflib的存储后端,允许读取和查询HDT文档。在

Online Documentation

要求

  • Pythonversion 3.6.4或更高版本

  • pip

  • gcc/clang支持c++11

  • Python开发头文件 .. 在

    You should have the ^{tt1}$ header available on your system.For example, for Python 3.6, install the ^{tt2}$ package on Debian/Ubuntu systems.

安装

{a8}强烈建议安装{a7}!

手动安装

要求:pipenv

^{pr2}$

入门

您可以在两种模式下使用rdflib-hdt库:作为rdflib图或作为原始HDT文档。在

HDT文档使用

fromrdflib_hdtimportHDTDocument# Load an HDT file. Missing indexes are generated automatically.# You can provide the index file by putting them in the same directory than the HDT file.document=HDTDocument("test.hdt")# Display some metadata about the HDT document itselfprint(f"Number of RDF triples: {document.total_triples}")print(f"Number of subjects: {document.nb_subjects}")print(f"Number of predicates: {document.nb_predicates}")print(f"Number of objects: {document.nb_objects}")print(f"Number of shared subject-object: {document.nb_shared}")# Fetch all triples that matches { ?s foaf:name ?o }# Use None to indicates variablestriples,cardinality=document.search_triples((None,FOAF("name"),None))print(f"Cardinality of (?s foaf:name ?o): {cardinality}")fors,p,ointriples:print(triple)# The search also support limit and offsettriples,cardinality=document.search_triples((None,FOAF("name"),None),limit=10,offset=100)# etc ...

HDT文档还支持在一组三元组模式上评估连接。在

fromrdflib_hdtimportHDTDocumentfromrdflibimportVariablefromrdflib.namespaceimportFOAF,RDFdocument=HDTDocument("test.hdt")# find the names of two entities that know each othertp_a=(Variable("a"),FOAF("knows"),Variable("b"))tp_b=(Variable("a"),FOAF("name"),Variable("name"))tp_c=(Variable("b"),FOAF("name"),Variable("friend"))query=set([tp_a,tp_b,tp_c])iterator=document.search_join(query)print(f"Estimated join cardinality: {len(iterator)}")# Join results are produced as ResultRow, like in the RDFlib SPARQL APIforrowiniterator:print(f"{row.name} knows {row.friend}")

在python中处理非UTF-8字符串

如果HDT文档是用非UTF-8编码编码的,则前面的代码将无法正常工作,并将导致UnicodeDecodeError。 有关如何将字符串从C++转换为Python ^ {A11}

的更多细节

为了解决这个问题,我们将HDT文档的API增加了一倍:

  • search_triples_bytes(...)返回一个三元组的迭代器(py::bytes,py::bytes,py::bytes)
  • search_join_bytes(...)返回映射为py::set(py::bytes,py::bytes)
  • convert_tripleid_bytes(...)返回三元组为:(py::bytes,py::bytes,py::bytes)
  • convert_id_bytes(...)返回一个py::bytes

参数和文档与标准版本相同

fromrdflib_hdtimportHDTDocumentdocument=HDTDocument("test.hdt")it=document.search_triple_bytes("","","")fors,p,oinit:print(s,p,o)# print b'...', b'...', b'...'# now decode it, or handle any errortry:s,p,o=s.decode('UTF-8'),p.decode('UTF-8'),o.decode('UTF-8')exceptUnicodeDecodeErroraserr:# try another other codecs, ignore error, etcpass

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java广播接收器未检测到按键事件   java不为特定列更改创建修订   java采用了更实用的方法   java桌面窗格HTML呈现   java处理internet连接丢失   java在调整JFrame的大小时,如何防止JTable的列调整大小?   如何用Java中的“Scanner.hasNext”完成程序   具有模块名称的java停止IntelliJ项目工具窗口?   执行已编译的Rails-Warbler JAR文件时发生java加载错误   java Spring数据redis存储库不支持集合查询?   每个客户端的java队列请求   获取Java中的JSON嵌套数组元素   java GWT,Vaadin ConcurrentModificationException   firebase存储中的java在保存照片之前,我需要发送两次照片   JavaSpringEleaf如何在容器外使用变量