Позитивные и негативные Wordcloud - PullRequest
1 голос
/ 10 марта 2019

Это образец моих данных в R представлении.Со счетом сбоку Я новичок в использовании R, и я хотел бы узнать, как создать положительное и отрицательное облако слов.

Уже загрузили в лексикон и забили мои твиты.Вот мой сценарий:

   library(twitteR)
library(stringr)
library(tm)
library(ggplot2)
library(wordcloud)
library(SnowballC)
library(wordcloud2)

tweets <- readRDS("IKEAUKSupport_tweets.az")

tweet_clean <- tm_map(tweet_clean, stemDocument)
DTM <- DocumentTermMatrix(tweet_clean)
DTM
saveRDS(DTM, file = "DTM.az")
inspect(DTM[1:10, 1:20])
m <- as.matrix(DTM)
write.csv(m, file = "DTM.csv")

pos.words <- scan("positive-words.txt",what="character", comment.char="")
neg.words <- scan("negative-words.txt",what="character", comment.char="")

score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
  require(plyr)
  require(stringr)

  scores = laply(sentences, function(sentence, pos.words, neg.words) {

    # cleaning up sentences with R's regex-driven global substitute, gsub()
    sentence = gsub('[[:punct:]]', '', sentence)
    sentence = gsub('[[:cntrl:]]', '', sentence)
    sentence = gsub('\\d+', '', sentence)
    # and convert to lower case
    sentence = tolower(sentence)

    # splitting into words. str_split is in the stringr package
    word.list = str_split(sentence, '\\s+')
    # sometimes a list() is one level of hierarchy too much
    words = unlist(word.list)

    # compare our words to the dictionaries of positive & negative terms
    pos.matches = match(words, pos.words)
    neg.matches = match(words, neg.words)

    # match() returns the position of the matched term or NA
    # we just want a TRUE/FALSE:
    pos.matches = !is.na(pos.matches)
    neg.matches = !is.na(neg.matches)

    # and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
    score = sum(pos.matches) - sum(neg.matches)

    return(score)
  }, pos.words, neg.words, .progress=.progress )

  scores.df = data.frame(score=scores, text=sentences)
  return(scores.df)
}

tweets.scores = score.sentiment(tweet_text, pos.words, neg.words, .progress='text')
write.csv(tweets.scores, "tweets_scores.csv")
table(tweets.scores$score)
traffic_light <- ifelse(tweets.scores$score<0, "negative", NA) 
traffic_light <- ifelse(tweets.scores$score==0, "neutral", traffic_light) 
traffic_light <- ifelse(tweets.scores$score>0, "postive", traffic_light)
table(traffic_light)
round(prop.table(table(traffic_light)), 2)*100

Очень хотелось бы знать, как я могу создать облако слов с метками для разделения положительных и отрицательных слов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...