Извлечение расчета эмоций для каждого ряда данных - PullRequest
0 голосов
/ 30 сентября 2018

У меня есть датафрейм со строками текста.Я хотел бы выделить для каждой строки текста вектор определенной эмоции, который будет двоичным 0, не существует, эта эмоция или 1 существует.
В целом это 5 эмоций, но я хотел бы иметь 1 только дляэмоции, которые кажутся наиболее.

Пример того, что я пробовал:

library(tidytext)
text = data.frame(id = c(11,12,13), text=c("bad movie","good movie","I think it would benefit religious people to see things like this, not just to learn about our home, the Universe, in a fun and easy way, but also to understand that non- religious explanations don't leave people hopeless and",))
nrc_lexicon <- get_sentiments("nrc")

Пример ожидаемого результата:

    id text sadness anger joy love neutral
11 "bad movie" 1 0 0 0 0
12 "good movie" 0 0 1 0 0 

Любые подсказки будут полезны дляme.

Пример для каждой строки, каков следующий шаг?
Как я могу вызвать каждую строку с анализом лексики nrc?

for (i in 1:nrow(text)) {
(text$text[i], nrc_lexicon)
}

1 Ответ

0 голосов
/ 30 сентября 2018

Как насчет этого:

library(tidytext)   # library for text
library(dplyr)

# your data
text <- data.frame(id = c(11,12,13),
 text=c("bad movie","good movie","I think it would benefit religious
 people to see things like this, not just to learn about our home, 
the Universe, in a fun and easy way, but also to understand that non- religious
 explanations don't leave people hopeless and"), stringsAsFactors = FALSE)  # here put this option, stringAsFactors = FALSE!

# the lexicon
nrc_lexicon <- get_sentiments("nrc")

# now the job
unnested <- text %>%
             unnest_tokens(word, text) %>%  # unnest the words
             left_join(nrc_lexicon) %>%     # join with the lexicon to have sentiments
             left_join(text)                # join with your data to have titles

Здесь вывод с id, вы можете иметь его также с заголовками, но я не поставил его из-за длинного третьего заголовка, вы можете легко поставитьэто как unnested$text вместо unnested$id:

table_sentiment <- table(unnested$id, unnested$sentiment)
table_sentiment
     anger anticipation disgust fear joy negative positive sadness surprise trust
  11     1            0       1    1   0        1        0       1        0     0
  12     0            1       0    0   1        0        1       0        1     1
  13     0            1       0    1   1        2        3       2        1     0

И если вы хотите это как data.frame:

 df_sentiment <- as.data.frame.matrix(table_sentiment)

Теперь вы можете делать все, что хотите, напримерЕсли я хорошо помню, вы хотите двоичный вывод, если есть или не настроение:

df_sentiment[df_sentiment>1]<-1
df_sentiment
   anger anticipation disgust fear joy negative positive sadness surprise trust
11     1            0       1    1   0        1        0       1        0     0
12     0            1       0    0   1        0        1       0        1     1
13     0            1       0    1   1        1        1       1        1     0
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...