如何使用Dbpedia ruby gem获得类型信息

时间:2023-01-14 22:52:59

I am trying to fetch artist info from wikipedia using Dbpedia gem https://github.com/farbenmeer/dbpedia

我正在使用Dbpedia gem https://github.com/farbenmeer/dbpedia从wikipedia获取艺术家信息

But I am unable to figure out what is the genre of a result item.

但是我不知道结果项的类型是什么。

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

基本上我想修改如下的函数,找出哪个结果是艺术家,然后返回它的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 will be for me to scrape each result url and then find out if it is an artist or not based on if there is genre info available.

最后的办法是我把每个结果url都找出来,然后根据是否有类型信息来判断它是否是一个艺术家。

1 个解决方案

#1


3  

You could leverage from DBpedia's SPARQL endpoint, rather than scrapping over all results.

您可以利用DBpedia的SPARQL端点,而不是放弃所有结果。

Suppose you want a list of everything that has a genre. You could query:

假设您想要一个包含所有类型的列表。你可以查询:

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

But say you don't want everything, you're looking just for artists. It could be a musician, a painter, an actor, etc.

但是如果你不想要所有的东西,你只是在寻找艺术家。可以是音乐家、画家、演员等等。

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

}
LIMIT 1000

Or maybe you just want musicians OR bands:

或者你只是想要音乐家或乐队:

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 that have "mega" in their names, e.g. Megadeath or Megan White, along with the URL of the resource.

最终,您希望音乐家或乐队的名字中包含“mega”,例如megadeth或Megan White,以及资源的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

Give it a try to this queries using the DBpedia's SPARQL Query Editor.

使用DBpedia的SPARQL查询编辑器尝试这个查询。

The dbpedia gem you pointed out, reveals the sparql-client in its API. So, I think you will be able to run all this queries using the #query method

您指出的dbpedia是在其API中显示sparql客户机的。因此,我认为您将能够使用#查询方法运行所有这些查询。

Dbpedia.sparql.query(query_string)

Best luck!

最好的运气!

#1


3  

You could leverage from DBpedia's SPARQL endpoint, rather than scrapping over all results.

您可以利用DBpedia的SPARQL端点,而不是放弃所有结果。

Suppose you want a list of everything that has a genre. You could query:

假设您想要一个包含所有类型的列表。你可以查询:

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

But say you don't want everything, you're looking just for artists. It could be a musician, a painter, an actor, etc.

但是如果你不想要所有的东西,你只是在寻找艺术家。可以是音乐家、画家、演员等等。

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

}
LIMIT 1000

Or maybe you just want musicians OR bands:

或者你只是想要音乐家或乐队:

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 that have "mega" in their names, e.g. Megadeath or Megan White, along with the URL of the resource.

最终,您希望音乐家或乐队的名字中包含“mega”,例如megadeth或Megan White,以及资源的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

Give it a try to this queries using the DBpedia's SPARQL Query Editor.

使用DBpedia的SPARQL查询编辑器尝试这个查询。

The dbpedia gem you pointed out, reveals the sparql-client in its API. So, I think you will be able to run all this queries using the #query method

您指出的dbpedia是在其API中显示sparql客户机的。因此,我认为您将能够使用#查询方法运行所有这些查询。

Dbpedia.sparql.query(query_string)

Best luck!

最好的运气!