user425727
user425727

Reputation:

querying rdf/xml in Python

The following code:

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')

returns this data in 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>

How can I query the various elements of this output?

For example, how can i retrieve the value of apipolarity?

OR, how can i retrieve all the apisemanticse.g. "love_another_person", "beloved"...?

EDIT

I am able to retrieve data as follows:

qres = result.query(
    """SELECT ?subject ?predicate ?object
        WHERE {
           ?subject ?predicate ?object.
        }""")

for r in qres.result:
    print str(r[0]), str(r[1]), str(r[2])

which returns:

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

How can I now narrow down my query to a specific concept and say, its polarity?

Upvotes: 0

Views: 974

Answers (1)

user425727
user425727

Reputation:

Example query to retrieve the polarity of concept "a_little":

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])

EDIT

improved solution as per Joshua's suggestion:

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]) 

Upvotes: 2

Related Questions