我如何让 R 显示它的计算(情感分析)

How do i make R show its computations (sentiment analysis)

当 R 正在计算情绪值(包 syuzhet)时,我能否以某种方式让程序显示准确的计算结果,比如值 2?

我知道如果单词 "good" 出现在某个地方,它将(根据我们的词典)分配给句子值,例如 +1,如果找到 "bad",它会减去 1。但我希望每个句子的输出都是这样的:

sentence [1]
The movie was very good, it had fun dialogue and great acting, but the ending was sad.
sentiment value [1]
"the" 0, "movie" 0, "was" 0, "very" 0, "good"+1 ... "fun"+1 ... "great"+1 ... "sad"-1 = 2

必须有(?)一个命令,但我找不到它。

好的,使用 syuzhet vignette 中的示例文本。

library(syuzhet)

my_example_text <- "I begin this story with a neutral statement.  
  Basically this is a very silly test.  
  You are testing the Syuzhet package using short, inane sentences.  
  I am actually very happy today. 
  I have finally finished writing this package.  
  Tomorrow I will be very sad. 
  I won't have anything left to do. 
  I might get angry and decide to do something horrible.  
  I might destroy the entire package and start from scratch.  
  Then again, I might find it satisfying to have completed my first R package. 
  Honestly this use of the Fourier transformation is really quite elegant.  
  You might even say it's beautiful!"

使用 sapply()

很容易获得每个句子的情感值
sapply(get_sentences(my_example_text), get_sentiment)
# I begin this story with a neutral statement. 
#                                         0.00 
#         Basically this is a very silly test. 
#                                        -0.25 
#                                          ...

并且可以使用 get_sent_values()

来获取每个词的情感值
get_sent_values("happy")
# [1] 0.75

但要获得您所描述的输出,我们必须稍微修改一下

wordsentiments <- function(x, method="syuzhet") {
    word_l <- strsplit(tolower(x), "[^A-Za-z']+")[[1]]
    val <- sapply(word_l, get_sent_values, method)
    l <- length(word_l) + 1
    word_l[l] <- "TOTAL"
    val[l] <- sum(val)
    names(val) <- NULL
    data.frame(value=val, word=word_l, stringsAsFactors=FALSE)
}

lapply(get_sentences(my_example_text), wordsentiments)
# [[1]]
#   value      word
# 1     0         i
# 2     0     begin
# 3     0      this
# 4     0     story
# 5     0      with
# 6     0         a
# 7     0   neutral
# 8     0 statement
# 9     0     TOTAL

# [[2]]
#   value      word
# 1  0.00 basically
# 2  0.00      this
# 3  0.00        is
# 4  0.00         a
# 5  0.00      very
# 6 -0.25     silly
# 7  0.00      test
# 8 -0.25     TOTAL

# ...