Created
May 26, 2020 23:57
-
-
Save tmastny/cd2f3c507f31264979eec7e4ae9f339c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(janeaustenr) | |
library(tidytext) | |
library(tidylo) | |
library(ggplot2) | |
tidy_bigrams <- austen_books() %>% | |
unnest_tokens(bigram, text, token = "ngrams", n = 2) | |
# bigrams across books | |
tidy_bigrams | |
bigram_counts <- tidy_bigrams %>% | |
count(book, bigram, sort = TRUE) | |
bigram_counts | |
bigram_log_odds <- bigram_counts %>% | |
bind_log_odds(book, bigram, n) | |
bigram_log_odds | |
bigram_log_odds %>% | |
group_by(book) %>% | |
slice_max(log_odds_weighted, n = 10) %>% | |
ungroup() %>% | |
mutate(bigram = reorder(bigram, log_odds_weighted)) %>% | |
ggplot(aes(bigram, log_odds_weighted, fill = book)) + | |
geom_col(show.legend = FALSE) + | |
facet_wrap(~book, scales = "free") + | |
coord_flip() + | |
labs(x = NULL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment