如何使用Wikir从wikidata检索电影类型
我想从wikidata中检索信息并将其存储在数据框中。为了简单起见,我假设我想要获得以下电影的类型,然后过滤那些属于科幻的电影:如何使用Wikir从wikidata检索电影类型,r,wikidata,R,Wikidata,我想从wikidata中检索信息并将其存储在数据框中。为了简单起见,我假设我想要获得以下电影的类型,然后过滤那些属于科幻的电影: movies = c("Star Wars Episode IV: A New Hope", "Interstellar", "Happythankyoumoreplease") 我知道有一个叫做WikidataR的包。如果我没有错,根据我的说法,有两个命令可能很有用:find_item和find_property允许您检索一组别名或描述与特定搜索词
movies = c("Star Wars Episode IV: A New Hope", "Interstellar",
"Happythankyoumoreplease")
我知道有一个叫做WikidataR的包。如果我没有错,根据我的说法,有两个命令可能很有用:find_item和find_property允许您检索一组别名或描述与特定搜索词匹配的Wikidata项或属性。显然他们对我很好,所以我想做一些类似的事情
for (i in movies) {
info = find_item(i)
}
这是我从每个项目中得到的:
> find_item("Interstellar")
Wikidata item search
Number of results: 10
Results:
1 Interstellar (Q13417189) - 2014 US science fiction film
2 Interstellar (Q6057099)
3 interstellar medium (Q41872) - matter and fields (radiation) that exist in the space between the star systems in a galaxy;includes gas in ionic, atomic or molecular form, dust and cosmic rays. It fills interstellar space and blends smoothly into the surrounding intergalactic space
4 space colonization (Q686876) - concept of permanent human habitation outside of Earth
5 rogue planet (Q167910) - planetary-mass object that orbits the galaxy directly
6 interstellar cloud (Q1054444) - accumulation of gas, plasma and dust in a galaxy
7 interstellar travel (Q834826) - term used for hypothetical manned or unmanned travel between stars
8 Interstellar Boundary Explorer (Q835898)
9 starship (Q2003852) - spacecraft designed for interstellar travel
10 interstellar object (Q2441216) - astronomical object in interstellar space, such as a comet
>
不幸的是,我从下面的find_item中获得的信息有两个问题:
它不是包含项目I的所有wikidata信息的数据框
我正在搜索一个列表,里面似乎是维基数据的id,
链接
它没有我需要的wikidata的信息
每个特定wikidata项的属性。
类似地,find_属性提供特定属性的元数据。find_propertygenre检索以下信息:
> find_property("genre")
Wikidata property search
Number of results: 4
Results:
1 genre (P136) - a creative work's genre or an artist's field of work (P101). Use main subject (P921) to relate creative works to their topic
2 radio format (P415) - describes the overall content broadcast on a radio station
3 sex or gender (P21) - sexual identity of subject: male (Q6581097), female (Q6581072), intersex (Q1097630), transgender female (Q1052281), transgender male (Q2449503). Animals: male animal (Q44148), female animal (Q43445). Groups of same gender use "subclass of" (P279)
4 gender of a scientific name of a genus (P2433) - determines the correct form of some names of species and subdivisions of species, also subdivisions of a genus
这也有类似的问题:
它不是数据帧
它只存储有关属性的元数据
我找不到任何方法将每个属性与movies vector中的每个对象链接起来。
有没有办法最终得到一个包含这些电影类型的数据框?或者是一个包含所有wikidata信息的数据框,为了过滤或选择所需数据,我必须对其进行操作。这些只是列表。例如,你可以用strfind_item Interstellar拍摄一张照片
然后,您可以浏览列表中的每个元素并选择所需的项目。例如获取标题和标签
a <- find_item("Interstellar")
b <- Reduce(rbind,lapply(a, function(x) cbind(x$title,x$label)))
data.frame(b)
## X1 X2
## 1 Q13417189 Interstellar
## 2 Q6057099 Interstellar
## 3 Q41872 interstellar medium
## 4 Q686876 space colonization
## 5 Q167910 rogue planet
## 6 Q1054444 interstellar cloud
## 7 Q834826 interstellar travel
## 8 Q835898 Interstellar Boundary Explorer
## 9 Q2003852 starship
## 10 Q2441216 interstellar object
非常感谢您的回答,@DJJ。不幸的是,您的答案只解决了一个数据结构问题,而不是主要的数据内容问题:如何从Wikidata检索有关某些电影的数据。我已经更新了我的问题,希望它现在更清楚。似乎你需要得到所有项目的标题第一。然后您可以使用get_itemQ13417189,然后检查Q471839科幻小说的id是否在其中一个标签中。例如get_itemQ13417189[[1]]$claims$P136[[1]][,datavalue][,value][,id]将是科幻小说冒险的id,我相信。
Reduce("rbind",lapply(a,
function(x) cbind(x$title,
x$label,
ifelse(length(x$description)==0,NA,x$description))))