我试着用tidytext做情感分析
library(tidytext)
get_sentiments("nrc")但它给了我一个错误:
'~/Library/Caches/textdata/nrc/NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt‘错误:
不存在。
然后,我尝试从github安装以下软件包
library(remotes)
install_github("EmilHvitfeldt/textdata")
install_github("juliasilge/tidytext")我仍然收到同样的错误。有人能帮我吗?谢谢!
发布于 2022-08-17 07:05:09
我也犯了同样的错误,因为文件下载在另一个文件夹中,而不是在子函数中指定的文件。因此,改变道路为我解决了它。
library(tidyverse)
library(tidytext)
library(textdata)
library(readr)
library(utils)
# check the error
get_sentiments("nrc") # select 1: will throw error but data still has been downloaded
# where is the file, then?
textdata::lexicon_nrc(return_path = T) # it's here
folder_path <- "~/Library/Caches/textdata/nrc"
# the problem is that the default path is wrong, so we have to adjust it
system(paste0("mkdir ", file.path(folder_path, "NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92")))
system(paste0("cp ", file.path(folder_path, "NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt"), " ", file.path(folder_path, "NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/")))
# now we have to process the nrc data using a slightly modified version of the subfunction detailed in the original function from the textdata-package: https://github.com/EmilHvitfeldt/textdata/blob/main/R/lexicon_nrc.R
name_path <- file.path(folder_path, "NRCWordEmotion.rds")
# slightly modified version:
process_nrc <- function(folder_path, name_path) {
data <- read_tsv(file.path(
folder_path,
"NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt"
),
col_names = FALSE, col_types = cols(
X1 = col_character(),
X2 = col_character(),
X3 = col_double()
)
)
data <- data[data$X3 == 1, ]
data <- tibble(
word = data$X1,
sentiment = data$X2
)
write_rds(data, name_path)
}
process_nrc(folder_path, name_path) # process
# check if you now have access to the lexicon
get_sentiments("nrc")
# now you can load it with tidytext :)https://stackoverflow.com/questions/69981357
复制相似问题