首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不存在文本错误'~/Library/Caches/textdata/nrc/NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt‘

不存在文本错误'~/Library/Caches/textdata/nrc/NRC-Emotion-Lexicon/NRC-Emotion-Lexicon-v0.92/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt‘
EN

Stack Overflow用户
提问于 2021-11-15 21:54:40
回答 1查看 202关注 0票数 0

我试着用tidytext做情感分析

代码语言:javascript
复制
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安装以下软件包

代码语言:javascript
复制
library(remotes)
install_github("EmilHvitfeldt/textdata")
install_github("juliasilge/tidytext")

我仍然收到同样的错误。有人能帮我吗?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-08-17 07:05:09

我也犯了同样的错误,因为文件下载在另一个文件夹中,而不是在子函数中指定的文件。因此,改变道路为我解决了它。

代码语言:javascript
复制
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 :)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69981357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档