Sentiment Analysis with Word Clouds: Understanding Two Outputs in R
This R script performs text preprocessing and generates two word clouds to visualize sentiment analysis. The word clouds represent the most frequent words in tweet text, separated by their sentiment label (positive or negative).
The script first loads a dataset containing tweet text and corresponding sentiment labels. It then preprocesses the text by removing punctuation, special characters, and digits, converting everything to lowercase. The script then utilizes the 'drawWordCloud' function (found in the 'src/text.preprocessing.R' file) to create two word clouds: one for positive sentiment and one for negative sentiment.
Understanding the Two Word Clouds:
- Positive Sentiment Word Cloud: Displays the most frequent words found in tweets with a positive sentiment. Larger words indicate higher frequency.
- Negative Sentiment Word Cloud: Displays the most frequent words found in tweets with a negative sentiment. Larger words indicate higher frequency.
By comparing the two word clouds, we can identify key words associated with positive and negative sentiment within the tweet data. For example, words like 'love' or 'happy' might appear frequently in the positive word cloud, while words like 'sad' or 'disappointed' might dominate the negative word cloud.
R Script:
# Map 1-based optional input ports to variables
dataset <- maml.mapInputPort(1) # class: data.frame
# Separate the label and tweet text
sentiment_label <- dataset[[1]]
tweet_text <- dataset[[2]]
# Replace punctuation, special characters and digits with space
tweet_text <- gsub('[^a-z]', ' ', tweet_text, ignore.case = TRUE)
# Convert to lowercase
tweet_text <- sapply(tweet_text, tolower)
data.set <- as.data.frame(cbind(sentiment_label,tweet_text),
stringsAsFactors=FALSE)
# Load the R script from the Zip port in ./src/
source('src/text.preprocessing.R');
drawWordCloud(tweet_text, sentiment_label, maxWords=50)
# Select data.frame to be sent to the output Dataset port
maml.mapOutputPort('data.set')
In conclusion, the two word cloud outputs provide a visual representation of the most frequent words associated with positive and negative sentiment in the tweet text, aiding in the understanding of the overall sentiment expressed in the data.
原文地址: https://www.cveoy.top/t/topic/lfdZ 著作权归作者所有。请勿转载和采集!