[英]Tidytext error '~/Library/Caches/textdata/nrc/NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt' does not exist
我嘗試使用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")
我仍然收到同樣的錯誤。 誰能幫我這個? 謝謝!
我遇到了同樣的錯誤,因為文件下載到了另一個文件夾中,而不是子函數中指定的文件夾。 因此,改變路徑為我解決了這個問題。
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 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.