# Load necessary libraries
library(tidyverse)
library(tidytext)
library(lubridate)

# Sample text data with dates
feedback <- data.frame(
  text = c("I love this product!", "Terrible service.", "Okay experience.",
           "Wonderful!", "Worst support ever."),
  date = as.Date(c("2024-01-10", "2024-01-12", "2024-01-15", "2024-01-18", "2024-01-20"))
)

# Tokenize, clean, and assign sentiment
data("stop_words")
sentiment_data <- feedback %>%
  unnest_tokens(word, text) %>%
  anti_join(stop_words, by = "word") %>%
  inner_join(get_sentiments("bing"), by = "word") %>%
  count(date, sentiment) %>%
  pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) %>%
  mutate(score = positive - negative,
         sentiment_label = case_when(
           score > 0 ~ "Positive",
           score < 0 ~ "Negative",
           TRUE ~ "Neutral"
         ))

# Trend visualization (bar plot over time)
ggplot(sentiment_data, aes(x = date, y = score, fill = sentiment_label)) +
  geom_col() +
  scale_fill_manual(values = c("Positive" = "green", "Negative" = "red", "Neutral" = "gray")) +
  labs(title = "Sentiment Trend Over Time", x = "Date", y = "Sentiment Score") +
  theme_minimal()

# Distribution visualization (pie chart)
ggplot(sentiment_data, aes(x = "", fill = sentiment_label)) +
  geom_bar(width = 1) +
  coord_polar("y") +
  theme_void() +
  labs(title = "Overall Sentiment Distribution")