如何使用R从wikidata中检索电影的类型

问题描述 投票:2回答:1

我想从wikidata检索信息并将其存储在数据帧中。为了简单起见,我将假设我想获得以下电影的类型,然后过滤属于科幻小说的那些:

movies = c("Star Wars Episode IV: A New Hope", "Interstellar", 
       "Happythankyoumoreplease")

我知道有一个名为WikidataR的包。如果我没有错,根据its vignettes,有两个命令可能有用:find_itemfind_property允许您检索一组维基数据项或属性,其中别名或描述与特定搜索词匹配。显然他们对我很好,所以我想做点什么

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获得的信息(见下文)有两个问题:

  1. 它不是包含我正在搜索的项目的所有wikidata信息的数据框,而是一个似乎是元数据的列表(wikidata的id,链接......)。
  2. 它没有我需要的信息(来自每个特定wikidata项目的wikidata属性)。

同样,find_property提供某个属性的元数据。 find_property("genre")检索以下信息:

> 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 

这有类似的问题:

  1. 它不是数据帧
  2. 它只存储有关该属性的元数据
  3. 我没有找到任何方法将每个属性与movies向量中的每个对象链接。

有没有办法最终得到一个包含这些电影类型的数据帧? (或者包含所有wikidata信息的数据框,为了过滤或选择我想要的数据,我必须操纵这些信息?)

r wikidata
1个回答
1
投票

这些只是lists。你可以用str(find_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

这对于常规数据很容易,如果缺少某些元素,那么你将不得不处理它,例如某些项目没有描述。所以你可以解决以下问题。

Reduce("rbind",lapply(a, 
                      function(x) cbind(x$title,
                                        x$label,
                                        ifelse(length(x$description)==0,NA,x$description))))
© www.soinside.com 2019 - 2024. All rights reserved.