以我可以使用RDFlib的方式从RDF图中获取事实

2024-05-29 06:59:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习使用RDF,并试图从dbpedia中提取一组事实作为我的学习练习。下面的代码示例在某种程度上是可行的,但是对于像配偶这样的主题,它总是会把他们自己拉出来。在

问题:

  1. get_name_from_uri()提取uri的最后一部分并删除下划线-必须有更好的方法来获取人名
  2. 结果显示,配偶拉回了配偶,但也拉回了数据主体-不知道那里发生了什么
  3. 一些结果以URI格式和文本项的形式将数据拉回-

这是代码块的输出,显示了我得到的一些奇怪的结果(参见属性中的混合输出,他与自己结婚的事实,以及约瑟芬的错误名字?在

Accessing facts for Napoleon  held at  http://dbpedia.org/resource/Napoleon

There are  800  facts about Napoleon stored at the URI
http://dbpedia.org/resource/Napoleon

Here are a few:-
Ontology:deathdate

Napoleon died on 1821-05-05

Ontology:birthdate
Napoleon was born on 1769-08-15

Property:spouse retruns the person themslves twice !
Napoleon was married to  Marie Louise, Duchess of Parma
Napoleon was married to  Napoleon
Napoleon was married to  Jos%C3%A9phine de Beauharnais
Napoleon was married to  Napoleon

Property:title retruns text and uri's
Napoleon  Held the title:  "The Death of Napoleon"
Napoleon  Held the title: http://dbpedia.org/resource/Emperor_of_the_French
Napoleon  Held the title: http://dbpedia.org/resource/King_of_Italy
Napoleon  Held the title:  First Consul of France
Napoleon  Held the title:  Provisional Consul of France
Napoleon  Held the title:  http://dbpedia.org/resource/Napoleon
Napoleon  Held the title:  Emperor of the French
Napoleon  Held the title: http://dbpedia.org/resource/Co-Princes_of_Andorra
Napoleon  Held the title:  from the Memoirs of Bourrienne, 1831
Napoleon  Held the title:  Protector of the Confederation of the Rhine

Ontology birth place returns three records
Napoleon was born in  Ajaccio
Napoleon was born in  Corsica
Napoleon was born in  Early modern France

这就是产生上述输出的python,它需要rdflib,而且还需要大量的工作。在

^{pr2}$

Tags: ofthetoorghttptitleuriresource
1条回答
网友
1楼 · 发布于 2024-05-29 06:59:36

获取姓名

*get_name_from_uri*正在对该uri执行某些操作。由于DBpedia数据几乎所有的内容都有rdfs:labels,所以最好请求rdfs:label并将其用作值。E、 g.,看看这个SPARQL查询运行的结果the DBpedia SPARQL endpoint

select ?spouse ?spouseName where {
  dbpedia:Napoleon dbpedia-owl:spouse ?spouse .
  ?spouse rdfs:label ?spouseName .
  filter( langMatches(lang(?spouseName),"en") )
}
^{pr2}$

意料之外的配偶

subject_objects的文档说明

subject_objects(self, predicate=None)

A generator of (subject, object) tuples for the given predicate

您看到了,正确的,DBpedia中有四个三元组具有谓词dbpprop:spouse(顺便问一下,您没有使用dbpedia-owl:spouse有什么原因吗?)并将Napoleon作为主语或宾语:

Napoleon                       spouse Marie Louise, Duchess of Parma
Marie Louise, Duchess of Parma spouse Napoleon 
Napoleon                       spouse Jos%C3%A9phine de Beauharnais
Jos%C3%A9phine de Beauharnais  spouse Napoleon

每一个你都要打印出来

"Napoleon was married to X"

其中X是三元组的对象。也许您应该改为使用^{}

objects(self, subject=None, predicate=None)

A generator of objects with the given subject and predicate

URI与文本(文本)结果

DBpedia本体属性(其uri以http://dbpedia.org/ontology/开头,通常缩写为dbpedia-owl:)描述的数据比DBpedia原始数据属性(uri以http://dbpedia.org/property/开头,通常缩写为dbpprop:)描述的数据要“干净”得多。E、 g.当您查看标题时,您使用的是属性dbpprop:title,并且uri和文本都作为值。不过,看起来并没有dbpedia-owl:title,所以在这种情况下,您只需要处理它。但很容易过滤出其中一种:

select ?title where {
  dbpedia:Napoleon dbpprop:title ?title
  filter isLiteral(?title)
}
title
================================================
"Emperor of the French"@en
"Protector of the Confederation of the Rhine"@en
"First Consul of France"@en
"Provisional Consul of France"@en
""The Death of Napoleon""@en
"from the Memoirs of Bourrienne, 1831"@en
select ?title where {
  dbpedia:Napoleon dbpprop:title ?title
  filter isURI(?title)
}
title
=================================================
http://dbpedia.org/resource/Co-Princes_of_Andorra
http://dbpedia.org/resource/Emperor_of_the_French
http://dbpedia.org/resource/King_of_Italy

相关问题 更多 >

    热门问题