0

我正在尝试检查某些单词是否是名词、动词等。

所以我的最终输出应该是一个单词列表及其分类。

考虑这个脚本:

library(data.table)
library(xml2)
random_words_2 <- c("aa","ab","ac")
dic <- list()
dics <- list()
for (i in 1:3){
h <-     paste0("http://www.oxforddictionaries.com/definition/english/",random_words_2[i])
html <- read_html(h)
oxford <- html_nodes(html, css = ".partOfSpeech")
n <- length(oxford)
for (m in 1:n) {
word <- as_list(oxford[[m]])
w <-  unlist(word[1])
dic[[m]] <- data.table(as.character(w))
}
dics <- rbindlist(dics, dic,use.names = TRUE,fill=FALSE)
}

有些词有不止一种分类,如动词、副词等。因此,列表会有不同的大小。我尝试了上面的代码,但是应该给我解决方案的 dics 变量是:

空数据表(0 行和 0 列)

但是, dic 变量给出:

[[1]] V1 1:名词

[[2]] V1 1:缩写

有人可以解释为什么会发生这种情况,还有没有更有效的方法来解决它?

谢谢

4

1 回答 1

1

用这个替换 for 循环:

dics <- list()
for (i in 1:3){ 
h <- paste0("http://www.oxforddictionaries.com/definition/english/",random_words_2[i]) 
html <- read_html(h) 
oxford <- html_nodes(html, css = ".partOfSpeech") 
n <- length(oxford) 
dic <- list() 
for (m in 1:n) 
{ 
    word <- as_list(oxford[[m]]) 
    w <- unlist(word[1]) 
    dic[[m]] <- data.table(as.character(w)) 
} 
dics <- c(dics, setNames(list(dic),random_words_2[i])) 
}
于 2016-04-07T21:13:26.857 回答