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如何将HashMap<String,Object>从一个活动传递到另一个活动   java如何手动加密socket连接的流量?   java正则表达式生成一个不正确的结果   Java方法引用具有泛型参数的方法   java app setBackground()错误:不兼容的类型:int无法转换为Drawable   java是启动Spring引导而不是SpringApplication的其他方法。跑   无法打开java类路径资源[org/quartz/impl/jdbcjobstore/tables_h2.sql],因为它不存在   spring使用Java,如何确定来自tomcat Web服务器的出站服务调用?   java获取多个同名的XML元素JAXB   java使用Ant从同一代码库构建Swing和Android应用程序   JComponent的java重绘方法不起作用   java目标不可访问,标识符“beanName”解析为null   smtp是否有支持esmtp管道的java api?   java如何在Spring中自动连接业务对象   java在Hibernate中没有其他保存实体的方法吗?   针对两个客户机的SpringJavaWeb应用程序项目开发   使用split的java标记化输入