becerra_posts <- read.csv("becerra_posts.csv")
hilton_posts <- read.csv("hilton_posts.csv")

Reflection

For this lab, I used AI to help clean my Bluesky data and complete the text and sentiment analysis. My first prompt was very general, and the results included unnecessary words like the candidates’ names and “www.” I improved my second prompt by being more specific about what I wanted removed and what type of analysis I needed. This helped give me better results.

I learned that even though AI can help write the code, I still need to check the results myself. For example, “xavierbecerra” was still included after cleaning, and some of the Bing sentiment classifications did not completely make sense without context. Overall, I liked this lab because it showed me that AI can make the process faster, but human judgment is still important when reviewing and understanding the results.

Data Cleaning

I started by cleaning the Bluesky data to remove missing, blank, and duplicate posts before completing the text and sentiment analysis.

becerra_clean <- becerra_posts %>%
  filter(!is.na(text), str_trim(text) != "") %>%
  distinct(text, .keep_all = TRUE)

hilton_clean <- hilton_posts %>%
  filter(!is.na(text), str_trim(text) != "") %>%
  distinct(text, .keep_all = TRUE)

nrow(becerra_clean)
## [1] 125
nrow(hilton_clean)
## [1] 159

The cleaned data included 125 posts about Xavier Becerra and 159 posts about Steve Hilton.

Text Analysis

I combined the two cleaned datasets and labeled each post by candidate. I then separated the post text into individual words and removed common stop words to identify the most frequently used words.

becerra_clean <- becerra_clean %>%
  mutate(candidate = "Xavier Becerra")

hilton_clean <- hilton_clean %>%
  mutate(candidate = "Steve Hilton")

bluesky_data <- bind_rows(becerra_clean, hilton_clean)

words <- bluesky_data %>%
  select(candidate, text) %>%
  unnest_tokens(word, text)

data("stop_words")

words_clean <- words %>%
  anti_join(stop_words, by = "word")

top_words <- words_clean %>%
  count(candidate, word, sort = TRUE) %>%
  group_by(candidate) %>%
  slice_max(n, n = 10, with_ties = FALSE) %>%
  ungroup()

top_words
## # A tibble: 20 × 3
##    candidate      word           n
##    <chr>          <chr>      <int>
##  1 Steve Hilton   hilton       137
##  2 Steve Hilton   steve        127
##  3 Steve Hilton   california    70
##  4 Steve Hilton   becerra       39
##  5 Steve Hilton   xavier        34
##  6 Steve Hilton   governor      30
##  7 Steve Hilton   republican    19
##  8 Steve Hilton   www           18
##  9 Steve Hilton   news          17
## 10 Steve Hilton   vote          17
## 11 Xavier Becerra becerra      114
## 12 Xavier Becerra xavier       101
## 13 Xavier Becerra california    96
## 14 Xavier Becerra governor      57
## 15 Xavier Becerra vote          43
## 16 Xavier Becerra hilton        34
## 17 Xavier Becerra social        32
## 18 Xavier Becerra bsky          30
## 19 Xavier Becerra steve         27
## 20 Xavier Becerra voted         23

The most common words were mainly the candidates’ names and election-related words such as “California,” “governor,” and “vote.” I also noticed that “www” appeared as a common word, which was not meaningful for the analysis. This showed me that the text needed additional cleaning before interpreting the results.

Improved Text Analysis

After reviewing the first results, I removed the candidates’ names and URL-related words because they did not provide useful information about what people were discussing.

words_improved <- words_clean %>%
  filter(!word %in% c(
    "xavier", "becerra", "steve", "hilton",
    "www", "https", "http", "com"
  ))

top_words_improved <- words_improved %>%
  count(candidate, word, sort = TRUE) %>%
  group_by(candidate) %>%
  slice_max(n, n = 10, with_ties = FALSE) %>%
  ungroup()

top_words_improved
## # A tibble: 20 × 3
##    candidate      word              n
##    <chr>          <chr>         <int>
##  1 Steve Hilton   california       70
##  2 Steve Hilton   governor         30
##  3 Steve Hilton   republican       19
##  4 Steve Hilton   news             17
##  5 Steve Hilton   vote             17
##  6 Steve Hilton   candidate        16
##  7 Steve Hilton   election         13
##  8 Steve Hilton   bill             12
##  9 Steve Hilton   ai               11
## 10 Steve Hilton   la               11
## 11 Xavier Becerra california       96
## 12 Xavier Becerra governor         57
## 13 Xavier Becerra vote             43
## 14 Xavier Becerra social           32
## 15 Xavier Becerra bsky             30
## 16 Xavier Becerra voted            23
## 17 Xavier Becerra 2026             22
## 18 Xavier Becerra xavierbecerra    18
## 19 Xavier Becerra corruption       17
## 20 Xavier Becerra hope             16

After removing these words, the results were easier to understand. Some of the most common words included “California,” “governor,” and “vote.” I also noticed that “xavierbecerra” was still included as a common word, showing that the AI-generated cleaning code did not catch every unnecessary word.

Sentiment Analysis

I used the Bing sentiment lexicon to identify positive and negative words in the Bluesky posts.

bing <- get_sentiments("bing")

sentiment_words <- words_improved %>%
  inner_join(bing, by = "word")

sentiment_summary <- sentiment_words %>%
  count(candidate, sentiment) %>%
  pivot_wider(
    names_from = sentiment,
    values_from = n,
    values_fill = 0
  ) %>%
  mutate(net_sentiment = positive - negative)

sentiment_summary
## # A tibble: 2 × 4
##   candidate      negative positive net_sentiment
##   <chr>             <int>    <int>         <int>
## 1 Steve Hilton        127       74           -53
## 2 Xavier Becerra       87       51           -36

The Bing sentiment analysis found 127 negative words and 74 positive words in the Steve Hilton posts and 87 negative words and 51 positive words in the Xavier Becerra posts. Both groups had more negative than positive sentiment words. However, these results only measure the words used in the posts and do not necessarily show whether the post supports or opposes the candidate.

Interactive Plotly Chart

I created an interactive chart to compare the number of positive and negative sentiment words for each candidate.

sentiment_plot <- ggplot(
  sentiment_words,
  aes(x = candidate, fill = sentiment)
) +
  geom_bar(position = "dodge") +
  labs(
    title = "Sentiment in Bluesky Posts",
    x = "Candidate",
    y = "Number of Sentiment Words"
  ) +
  theme_minimal()

ggplotly(sentiment_plot)

Verification

I checked whether the improved cleaning successfully removed the candidate names and URL-related words.

words_improved %>%
  filter(word %in% c(
    "xavier", "becerra", "steve", "hilton", "www"
  )) %>%
  count(word)
## [1] word n   
## <0 rows> (or 0-length row.names)

I also checked the most common words classified by the Bing sentiment lexicon to see if the classifications made sense.

verification_results <- sentiment_words %>%
  count(word, sentiment, sort = TRUE) %>%
  head(20)

print(verification_results)
##           word sentiment  n
## 1   corruption  negative 18
## 2      cruelty  negative 15
## 3         safe  positive 15
## 4         rail  negative 12
## 5        trump  positive 11
## 6     dishonor  negative  8
## 7          win  positive  7
## 8         hack  negative  6
## 9      hopeful  positive  6
## 10 astonishing  positive  4
## 11    breaking  negative  4
## 12        fast  positive  4
## 13        lead  positive  4
## 14       rival  negative  4
## 15      attack  negative  3
## 16    endorsed  positive  3
## 17 endorsement  positive  3
## 18 fascinating  positive  3
## 19    friendly  positive  3
## 20      maniac  negative  3

Some classifications made sense, such as “cruelty” being negative and “safe” being positive. However, other words depended more on context. For example, Bing classified “trump” as positive and “rail” as negative. This showed me why I still needed to review the AI-assisted analysis instead of assuming that every result was correct.