-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTidyText_Chapter3_TF_IDF_Pipeline.R
More file actions
67 lines (44 loc) · 1.31 KB
/
TidyText_Chapter3_TF_IDF_Pipeline.R
File metadata and controls
67 lines (44 loc) · 1.31 KB
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
62
library(tidytext)
library(tidyverse)
library(ggthemes)
library(ggplot2)
#Input Text
text <- read.csv("~/Desktop/result_clean.csv")
text <- mutate(text, text = as.character(word))
text <- text %>%
select(-word)
text <- as.data.frame(text)
text <- text %>%
mutate(linenumber = row_number())
#Separate into single words
single_words <- text %>%
unnest_tokens(word, text)
#Filter Words
Total_Corpus <- single_words %>%
add_count(word) %>%
filter(n > 300) %>%
select(-n)
#Make TF_IDF table
#use year if you have dates or use linenumber if not
#better to use by year if possible
Total_Corpus_tf_idf <- Total_Corpus %>%
count(year, word, sort = TRUE) %>%
bind_tf_idf(word, year, n) %>%
arrange(-tf_idf) %>%
group_by(year) %>%
top_n(10) %>%
ungroup
Total_Corpus_tf_idf %>%
mutate(word = reorder_within(word, tf_idf, year)) %>%
ggplot(aes(word, tf_idf, fill = year)) +
geom_col(show.legend = FALSE) +
theme_bw() +
facet_wrap(~ year, scales = "free_y", ncol = 3) +
scale_x_reordered() +
scale_y_continuous(
labels = scales::number_format(accuracy = 0.001)) +
coord_flip() +
theme(strip.text=element_text(size=11)) +
labs(x = NULL, y = "tf-idf",
title = "Highest tf-idf words in Gene Drive Corpus",
subtitle = "Separated by each time segment, totaling in 6 segments")