pythonrdflib通过subject和predi获取对象值

2024-06-16 10:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个rdf文件,如下所示:

<rdf:Description rdf:about="http://sentic.net/api/en/concept/a_little">
        <rdf:type rdf:resource="http://sentic.net/api/concept"/>
        <text xmlns="http://sentic.net/api">a little</text>
        <semantics xmlns="http://sentic.net/api" rdf:resource="http://sentic.net/api/en/concept/least"/>
        <semantics xmlns="http://sentic.net/api" rdf:resource="http://sentic.net/api/en/concept/little"/>
        <semantics xmlns="http://sentic.net/api" rdf:resource="http://sentic.net/api/en/concept/small_amount"/>
        <semantics xmlns="http://sentic.net/api" rdf:resource="http://sentic.net/api/en/concept/shortage"/>
        <semantics xmlns="http://sentic.net/api" rdf:resource="http://sentic.net/api/en/concept/scarce"/>
        <pleasantness xmlns="http://sentic.net/api" rdf:datatype="http://www.w3.org/2001/XMLSchema#float">-0.99</pleasantness>
        <attention xmlns="http://sentic.net/api" rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0</attention>
        <sensitivity xmlns="http://sentic.net/api" rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0</sensitivity>
        <aptitude xmlns="http://sentic.net/api" rdf:datatype="http://www.w3.org/2001/XMLSchema#float">-0.709</aptitude>
        <polarity xmlns="http://sentic.net/api" rdf:datatype="http://www.w3.org/2001/XMLSchema#float">-0.566</polarity>
    </rdf:Description>

我怎样才能得到宾语,例如谓词“极性”(在本例中是-0.566)?在


Tags: orgapihttpnetwwwrdfresourceconcept
1条回答
网友
1楼 · 发布于 2024-06-16 10:32:51

您可能需要查看RDFLib的getting started docs。在

您的代码可能如下所示:

import rdflib

g = rdflib.Graph()

result = g.parse("yourfile.rdf")

pred = URIRef("http://sentic.net/apipoloarity")

for polarity in g.objects(predicate=pred):
    print(polarity)

相关问题 更多 >