How to Get Genre Info with Dbpedia's Ruby Gem

I am trying to get information about an artist from wikipedia using the Dbpedia gem https://github.com/farbenmeer/dbpedia

But I can't figure out what is the genre of the result.

Basically I want to change the following function to find out which result is the artist and then return its url:

  def self.get_slug(q)
    results = Dbpedia.search(q)
    result  = # Do something to find out the result that is an artist
    uri   = result.uri rescue ""
    return uri
  end

      

The last resort would be for me to scrape each result url and then find out if it is an artist or not based on the availability of genre information available.

+3


source to share


1 answer


You can use DBpedia SPARQL endpoint instead of breaking all results.

Let's say you want a list of everything it has genre

. You can request:

SELECT DISTINCT ?thing WHERE {
  ?thing dbpedia-owl:genre ?genre
}
LIMIT 1000

      

But say you don't want everything, you only look at artists. It can be a musician, artist, actor, etc.

SELECT DISTINCT ?thing WHERE {
  ?thing dbpedia-owl:genre ?genre ;
         rdf:type          dbpedia-owl:Artist

}
LIMIT 1000

      

Or maybe you just want OR musicians :

SELECT DISTINCT ?thing WHERE {
  {
    ?thing dbpedia-owl:genre ?genre ;
           rdf:type          dbpedia-owl:Band
  }
  UNION
  {
    ?thing dbpedia-owl:genre ?genre ;
           a                 dbpedia-owl:MusicalArtist # `a` is a shortcut for `rdf:type`
  } 
}
LIMIT 1000

      



Ultimately, you want musicians or bands to have "mega" in their names, for example. Megadeath or Megan White along with the resource URL.

SELECT DISTINCT ?thing, ?url, ?genre WHERE {
  ?thing foaf:name             ?name ;
         foaf:isPrimaryTopicOf ?url .
  ?name  bif:contains "'mega*'" .
  {
    ?thing dbpedia-owl:genre ?genre ;
           a                 dbpedia-owl:Band
  }
  UNION
  {
    ?thing dbpedia-owl:genre ?genre ;
           a                 dbpedia-owl:MusicalArtist
  }
  UNION
  {
    ?thing a <http://umbel.org/umbel/rc/MusicalPerformer>
  }
}
LIMIT 1000

      

Try these queries using DBpedia SPARQL Query Editor .

the dbpedia gem you listed shows sparql-client in its API. So, I think you should be able to run all these requests using the method#query

Dbpedia.sparql.query(query_string)

      

Good luck!

+3


source







All Articles