简体   繁体   English

Tidytext 错误“~/Library/Caches/textdata/nrc/NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt”不存在

[英]Tidytext error '~/Library/Caches/textdata/nrc/NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt' does not exist

I tried to use tidytext to do sentiment analysis我尝试使用tidytext做情感分析

library(tidytext)
get_sentiments("nrc")

but it gives me an error:但它给了我一个错误:

Error: '~/Library/Caches/textdata/nrc/NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt' does not exist.错误:“~/Library/Caches/textdata/nrc/NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt”不存在。

I have then tried to install the following packages from github然后我尝试从 github 安装以下软件包

library(remotes)
install_github("EmilHvitfeldt/textdata")
install_github("juliasilge/tidytext")

and I still receive the same error.我仍然收到同样的错误。 Can anyone help me with this?谁能帮我这个? Thank you!谢谢!

I had the same error because the file downloaded in another folder than the one specified in the subfunction.我遇到了同样的错误,因为文件下载到了另一个文件夹中,而不是子函数中指定的文件夹。 Thus, changing the path solved it for me.因此,改变路径为我解决了这个问题。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM