# Tidytext

> R tidytext package for text mining. Use for tokenization, sentiment analysis, TF-IDF, and tidy text analysis.

- Skill: `leolin990405/tidytext` (Agent Skill)
- Install (CLI): `npx get-skill add leolin990405/tidytext`
- Raw SKILL.md: https://api.skillmd.com/api/skills/leolin990405/tidytext/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: LeoLin990405 (https://skillmd.com/u/leolin990405)
- Updated: 2026-11-14
- Page: https://skillmd.com/skills/leolin990405/tidytext

---


# tidytext

Tidy text mining.

## Tokenization

```r
library(tidytext)
library(dplyr)

# Tokenize to words
df %>% unnest_tokens(word, text)

# Tokenize to sentences
df %>% unnest_tokens(sentence, text, token = "sentences")

# Tokenize to ngrams
df %>% unnest_tokens(bigram, text, token = "ngrams", n = 2)
df %>% unnest_tokens(trigram, text, token = "ngrams", n = 3)

# Tokenize to characters
df %>% unnest_tokens(char, text, token = "characters")

# Custom tokenizer
df %>% unnest_tokens(word, text, token = "regex", pattern = "\\s+")
```

## Stop Words

```r
# Remove stop words
df %>%
  unnest_tokens(word, text) %>%
  anti_join(stop_words)

# Custom stop words
custom_stops <- tibble(word = c("custom", "words"))
df %>%
  unnest_tokens(word, text) %>%
  anti_join(stop_words) %>%
  anti_join(custom_stops)

# Different lexicons
stop_words %>% filter(lexicon == "snowball")
stop_words %>% filter(lexicon == "onix")
stop_words %>% filter(lexicon == "SMART")
```

## Word Frequencies

```r
# Count words
word_counts <- df %>%
  unnest_tokens(word, text) %>%
  anti_join(stop_words) %>%
  count(word, sort = TRUE)

# By document
word_counts <- df %>%
  unnest_tokens(word, text) %>%
  count(document, word, sort = TRUE)
```

## TF-IDF

```r
# Calculate TF-IDF
tfidf <- df %>%
  unnest_tokens(word, text) %>%
  count(document, word) %>%
  bind_tf_idf(word, document, n)

# Top TF-IDF words per document
tfidf %>%
  group_by(document) %>%
  slice_max(tf_idf, n = 10)
```

## Sentiment Analysis

```r
# Get sentiments
get_sentiments("afinn")   # Score -5 to 5
get_sentiments("bing")    # positive/negative
get_sentiments("nrc")     # Multiple emotions

# Join sentiments
df %>%
  unnest_tokens(word, text) %>%
  inner_join(get_sentiments("bing")) %>%
  count(sentiment)

# Sentiment by document
df %>%
  unnest_tokens(word, text) %>%
  inner_join(get_sentiments("afinn")) %>%
  group_by(document) %>%
  summarize(sentiment = sum(value))

# NRC emotions
df %>%
  unnest_tokens(word, text) %>%
  inner_join(get_sentiments("nrc")) %>%
  count(sentiment, sort = TRUE)
```

## N-grams

```r
# Bigrams
bigrams <- df %>%
  unnest_tokens(bigram, text, token = "ngrams", n = 2)

# Separate bigrams
bigrams %>%
  separate(bigram, c("word1", "word2"), sep = " ")

# Filter bigrams
bigrams %>%
  separate(bigram, c("word1", "word2"), sep = " ") %>%
  filter(!word1 %in% stop_words$word,
         !word2 %in% stop_words$word) %>%
  unite(bigram, word1, word2, sep = " ")
```

## Document-Term Matrix

```r
# Create DTM
dtm <- df %>%
  unnest_tokens(word, text) %>%
  count(document, word) %>%
  cast_dtm(document, word, n)

# Create sparse matrix
sparse <- df %>%
  unnest_tokens(word, text) %>%
  count(document, word) %>%
  cast_sparse(document, word, n)

# From DTM to tidy
tidy(dtm)
```

## Topic Modeling

```r
library(topicmodels)

# Create DTM
dtm <- df %>%
  unnest_tokens(word, text) %>%
  anti_join(stop_words) %>%
  count(document, word) %>%
  cast_dtm(document, word, n)

# LDA
lda <- LDA(dtm, k = 5, control = list(seed = 1234))

# Tidy topics
topics <- tidy(lda, matrix = "beta")

# Top words per topic
topics %>%
  group_by(topic) %>%
  slice_max(beta, n = 10)

# Document-topic probabilities
gamma <- tidy(lda, matrix = "gamma")
```

