Хорошо, используя пример текста из syuzhet
виньетки .
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
# ...