Python RDFLib按主题和谓词获取对象值

时间:2016-02-06 14:13:30

标签: python rdf predicate rdflib

我有一个看起来像这样的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>

我如何获得例如谓词极性&#39; (在这种情况下是-0.566)?

1 个答案:

答案 0 :(得分:0)

您可能想要查看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)