How can I query a country's property like language from DBpedia?

前端 未结 3 1970
青春惊慌失措
青春惊慌失措 2020-12-22 13:16

How can I query a country profile with DBPedia like http://dbpedia.org/page/France and get a property like language?

相关标签:
3条回答
  • 2020-12-22 13:47

    Unless they provide an API of some kind your only choice would be to "screen scrape" the page - in other words load the page using curl (or some other method) and then search for "dbpedia-owl:language" and get the string that follows (dbpedia:French_language) and parse it to make it nicer looking.

    0 讨论(0)
  • 2020-12-22 13:53

    Let's start from the beginning, as you don't say what you have tried.

    DBpedia is a database of information about so-called resources: facts derived from Wikipedia articles stored as RDF triples. Resources are identified by URIs; DBpedia uses the form http://dbpedia.org/resource/* where * is the same as * in http://en.wikipedia.org/wiki/*.

    So DBpedia has facts about the resource http://dbpedia.org/resource/France. If you look up this resource in your browser, you will be redirected to http://dbpedia.org/page/France, because the country France cannot be displayed in your browser but the description of it can.

    Among the facts DBpedia knows is

    <http://dbpedia.org/resource/France> <http://dbpedia.org/ontology/language> <http://dbpedia.org/resource/French_language>
    

    which basically says "France['s] language [is] the French language."

    To get this fact via an API, you can use the standard RDF query language and protocol SPARQL. The DBpedia SPARQL endpoint, which is where you send SPARQL queries to DBpedia, has a web form to let you enter and submit queries. If you just want an HTML table showing what language is spoken in France, leave the form's settings at the defaults and use:

    select ?language ?languageName
    where {
      dbpedia:France dbpedia-owl:language ?language .
      ?language rdfs:label ?languageName .
    }
    

    which means: "Give me the resource(s) that France uses as a language, and the name(s)."
    dbpedia:France is short for <http://dbpedia.org/resource/France>, and dbpedia-owl:language is short for <http://dbpedia.org/ontology/language>.

    If you want countries and the languages that are spoken in those countries, use:

    select distinct ?country ?language
    where {
      ?country a dbpedia-owl:Country .
      ?country dbpedia-owl:language ?language .
    } 
    LIMIT 100
    

    which means: "Give me 100 combinations of resources that are countries and the resources that these countries use as language."

    There are nuances that I left out, but this should get you started.

    0 讨论(0)
  • 2020-12-22 14:04

    As @Bergi points out in the comments, see http://wiki.dbpedia.org/OnlineAccess for list of possible on-line access methods.

    0 讨论(0)
提交回复
热议问题