forked from johnmackintosh/FNM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfidf.R
61 lines (50 loc) · 1.76 KB
/
tfidf.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#term frequency by album
#shows many words mentioned a few times, few words mentioned often
album_words <- data %>%
unnest_tokens(word, lyrics) %>%
anti_join(stop_words) %>%
filter(stringr::str_detect(word,"[a-z`]$"),
!word %in% stop_words$word) %>%
filter(word %notin% c("b","e","a","g","r","s","i","v","la")) %>%
count(Album, word, sort = TRUE) %>%
ungroup()
total_words <- album_words %>%
group_by(Album) %>%
summarize(total = sum(n))
all_album_words <- left_join(album_words, total_words)
all_album_words
knitr::kable(head(all_album_words))
ggplot(all_album_words, aes(n/total, fill = Album)) +
geom_histogram(show.legend = FALSE) +
xlim(NA,0.08)+
facet_wrap(~Album, ncol = 2, scales = "free_y")+
theme_ipsum()+
scale_fill_ipsum()+
ggtitle("Term Frequency Distribution by Album")
ggsave("2017-10-22-Term-Frequency-by-Album.png",width= 8, height = 6)
#tf - idf
album_tfidf <- all_album_words %>%
bind_tf_idf(word, Album, n)
knitr::kable(album_tfidf)
album_tfidf %>%
select(-total) %>%
arrange(desc(tf_idf))
album_tfidf
plot_album_tfidf <- album_tfidf %>%
group_by(Album) %>%
arrange(desc(tf_idf)) %>%
mutate(word = factor(word, levels = rev(unique(word))))
plot_album_tfidf %>%
group_by(Album) %>%
top_n(10) %>%
ungroup() %>%
ggplot(aes(x=reorder(word,tf_idf), tf_idf, fill = Album)) +
ggtitle("Highest tf_idf words in Faith No More Album Lyrics",
subtitle = "Studio albums only - Excludes stop words")+
labs(x = NULL, y = NULL)+
geom_col(show.legend = FALSE) +
facet_wrap(~Album,scales = "free_y")+
coord_flip()+
theme_ipsum()+
scale_fill_ipsum()
ggsave("2017-10-22-Inverse-Term-Document-Frequency-by-Album.png",width=10, height = 11)