gpt4 book ai didi

r - 如何将多个 qdap 转换链接在一起以进行 R 中的文本挖掘/情感(极性)分析

转载 作者:行者123 更新时间:2023-12-05 06:43:16 25 4
gpt4 key购买 nike

我有一个 data.frame,其中包含周数 week 和文本评论 text。我想将 week 变量视为我的分组变量,并对它运行一些基本的文本分析(例如 qdap::polarity)。一些评论文本有多个句子;但是,我只关心本周的“整体”极性。

如何在运行 qdap::polarity 之前将多个文本转换链接在一起并遵守其警告消息?我能够将转换与 tm::tm_maptm::tm_reduce 链接在一起——在 qdap 中有类似的东西吗?在运行 qdap::polarity 和/或 qdap::sentSplit 之前预处理/转换此文本的正确方法是什么?

以下代码/可重现示例中的更多详细信息:

library(qdap)
library(tm)

df <- data.frame(week = c(1, 1, 1, 2, 2, 3, 4),
text = c("This is some text. It was bad. Not good.",
"Another review that was bad!",
"Great job, very helpful; more stuff here, but can't quite get it.",
"Short, poor, not good Dr. Jay, but just so-so. And some more text here.",
"Awesome job! This was a great review. Very helpful and thorough.",
"Not so great.",
"The 1st time Mr. Smith helped me was not good."),
stringsAsFactors = FALSE)

docs <- as.Corpus(df$text, df$week)

funs <- list(stripWhitespace,
tolower,
replace_ordinal,
replace_number,
replace_abbreviation)

# Is there a qdap function that does something similar to the next line?
# Or is there a way to pass this VCorpus / Corpus directly to qdap::polarity?
docs <- tm_map(docs, FUN = tm_reduce, tmFuns = funs)


# At the end of the day, I would like to get this type of output, but adhere to
# the warning message about running sentSplit. How should I pre-treat / cleanse
# these sentences, but keep the "week" grouping?
pol <- polarity(df$text, df$week)

## Not run:
# check_text(df$text)

最佳答案

您可以按照警告中的建议运行 sentSplit,如下所示:

df_split <- sentSplit(df, "text")
with(df_split, polarity(text, week))

## week total.sentences total.words ave.polarity sd.polarity stan.mean.polarity
## 1 1 5 26 -0.138 0.710 -0.195
## 2 2 6 26 0.342 0.402 0.852
## 3 3 1 3 -0.577 NA NA
## 4 4 2 10 0.000 0.000 NaN

请注意,我有一个突破情绪包 sentimentr在 github 上可用,与 qdap 版本相比,它在速度、功能和文档方面都有所改进。这会在 sentiment_by 函数内部进行句子拆分。下面的脚本允许您安装并使用该包:

if (!require("pacman")) install.packages("pacman")
p_load_gh("trinker/sentimentr")

with(df, sentiment_by(text, week))

## week word_count sd ave_sentiment
## 1: 2 25 0.7562542 0.21086408
## 2: 1 26 1.1291541 0.05781106
## 3: 4 10 NA 0.00000000
## 4: 3 3 NA -0.57735027

关于r - 如何将多个 qdap 转换链接在一起以进行 R 中的文本挖掘/情感(极性)分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34023200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com