在Python中查询rdf/xml

在Python中查询rdf/xml,python,xml,rdf,sparql,Python,Xml,Rdf,Sparql,以下代码: import rdflib.graph as g graph = g.Graph() graph.parse('C:\\Python27\\phyton projects\\senticnet-3.0\\senticnet3.rdf.xml', format='xml') print graph.serialize(format='pretty-xml') 以Python格式返回此数据: <?xml version="1.0" encoding="utf-8"?> &l

以下代码:

import rdflib.graph as g
graph = g.Graph()
graph.parse('C:\\Python27\\phyton projects\\senticnet-3.0\\senticnet3.rdf.xml', format='xml')
print graph.serialize(format='pretty-xml')
以Python格式返回此数据:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
  xmlns:ns1="http://sentic.net/"
  xmlns:ns2="http://sentic.net/api/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
 <ns1:apisemantics>
      <ns2:concept rdf:about="http://sentic.net/api/en/concept/love">
        <ns1:apipolarity rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.667</ns1:apipolarity>
        <ns1:apiattention rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0</ns1:apiattention>
        <ns1:apipleasantness rdf:datatype="http://www.w3.org/2001/XMLSchema#float">1</ns1:apipleasantness>
        <ns1:apiaptitude rdf:datatype="http://www.w3.org/2001/XMLSchema#float">1</ns1:apiaptitude>
        <ns1:apisemantics rdf:resource="http://sentic.net/api/en/concept/love_another_person"/>
        <ns1:apisemantics rdf:resource="http://sentic.net/api/en/concept/beloved"/>
        <ns1:apisemantics rdf:resource="http://sentic.net/api/en/concept/lust"/>
        <ns1:apisemantics rdf:resource="http://sentic.net/api/en/concept/show_empathy"/>
        <ns1:apisemantics rdf:resource="http://sentic.net/api/en/concept/sexuality"/>
        <ns1:apisensitivity rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0</ns1:apisensitivity>
        <ns1:apitext>love</ns1:apitext>
      </ns2:concept>
    </ns1:apisemantics>
返回:

http://sentic.net/api/en/concept/a_lot_of_study http://sentic.net/apitext a lot of study
http://sentic.net/api/en/concept/a_lot_of_study http://sentic.net/apiaptitude -0.111
http://sentic.net/api/en/concept/a_lot_of_study http://sentic.net/apiattention -0.005
http://sentic.net/api/en/concept/a_lot_of_study http://sentic.net/apipolarity -0.064
http://sentic.net/api/en/concept/a_lot_of_study http://sentic.net/apipleasantness -0.074

我现在如何将我的问题缩小到一个特定的概念,并说它的极性

检索概念“a_little”极性的示例查询:

qres=result.query(
“”“选择”对象
在哪里{
主语、谓语、宾语
过滤器(?主题=)
过滤器(?谓词=)
}""")
对于qres中的r。结果:
打印str(r[0])
编辑

根据Joshua的建议改进了解决方案:

qres = result.query(
         """
         prefix concept: <http://sentic.net/api/en/concept/>
         prefix api: <http://sentic.net/api/>
         SELECT ?polarity
           WHERE { 
              concept:%s api:polarity ?polarity
            }
         """%concept) #concept is passed through as a function parameter
    for r in qres.result:
        return str(r[0]) 
qres=result.query(
"""
前缀概念:
前缀api:
选择?极性
何处{
概念:%s api:极性?极性
}
“”%concept)#概念作为函数参数传递
对于qres中的r。结果:
返回str(r[0])

怎么样?还是那辆车?如果这些还不够,那么您需要提供更多关于为什么这些不适合您的信息。我已经添加了这里,因为您找到了使用它的答案。但是,不要使用
filter(?subject=…)
。只要把URI放在那里。例如,
选择极性,其中{polarity}
。看到有人引用了一种新的工具或语言,并看到他们使用它找到了解决方案,这是非常令人鼓舞的。如果将来要大量使用SPARQL,那么有很多关于堆栈溢出的问题和答案。您还可以使用一些前缀使查询更干净。这是一个清理后的版本,应该会返回相同的结果:。很高兴提供帮助!如果您最终使用的是经过清理的查询版本(或它的某些变体),请随意将其编辑到您的答案中。别忘了接受你的答案(如果允许的话;我记得有时间限制)。
qres = result.query(
     """SELECT ?object
       WHERE {
          ?subject ?predicate ?object
          FILTER (?subject = <http://sentic.net/api/en/concept/a_little>)
          FILTER (?predicate = <http://sentic.net/apipolarity>)
        }""")

for r in qres.result:
    print str(r[0])
qres = result.query(
         """
         prefix concept: <http://sentic.net/api/en/concept/>
         prefix api: <http://sentic.net/api/>
         SELECT ?polarity
           WHERE { 
              concept:%s api:polarity ?polarity
            }
         """%concept) #concept is passed through as a function parameter
    for r in qres.result:
        return str(r[0])