-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example_BM25.R
162 lines (126 loc) · 5.01 KB
/
Example_BM25.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
## BM25
# https://www.tidytextmining.com/tfidf.html
rm(list = ls()) # Clean variable
memory.limit(150000)
##### version info #####
# platform x86_64-w64-mingw32
# arch x86_64
# os mingw32
# system x86_64, mingw32
# status
# major 4
# minor 1.1
# year 2021
# month 08
# day 10
# svn rev 80725
# language R
# version.string R version 4.1.1 (2021-08-10)
# nickname Kick Things
##### 3.1 Term frequency in Jane Austen s novels #####
library(dplyr)
library(janeaustenr)
library(tidytext)
book_words <- austen_books() %>%
unnest_tokens(word, text) %>%
count(book, word, sort = TRUE)
total_words <- book_words %>%
group_by(book) %>%
summarise(total = sum(n)) # https://github.com/tidyverse/dplyr/issues/505
book_words <- left_join(book_words, total_words)
book_words
library(ggplot2)
ggplot(book_words, aes(n/total, fill = book)) +
geom_histogram(show.legend = FALSE) +
xlim(NA, 0.0009) +
facet_wrap(~book, ncol = 2, scales = "free_y")
##### 3.2 Zipf s law #####
freq_by_rank <- book_words %>%
group_by(book) %>%
mutate(rank = row_number(),
`term frequency` = n/total) %>%
ungroup()
freq_by_rank
freq_by_rank %>%
ggplot(aes(rank, `term frequency`, color = book)) +
geom_line(size = 1.1, alpha = 0.8, show.legend = FALSE) +
scale_x_log10() +
scale_y_log10()
rank_subset <- freq_by_rank %>%
filter(rank < 500,
rank > 10)
lm(log10(`term frequency`) ~ log10(rank), data = rank_subset)
freq_by_rank %>%
ggplot(aes(rank, `term frequency`, color = book)) +
geom_abline(intercept = -0.62, slope = -1.1,
color = "gray50", linetype = 2) +
geom_line(size = 1.1, alpha = 0.8, show.legend = FALSE) +
scale_x_log10() +
scale_y_log10()
##### 3.3 The BM25() function #####
source("FUN_BM25.R")
book_BM25 <- book_words %>%
BM25Score(word, book, n)
plot(book_BM25$tf_idf ,book_BM25$bm25)
# ## Z-score
# # https://www.r-bloggers.com/2020/02/how-to-compute-the-z-score-with-r/
# book_BM25_zscore <- book_BM25
#
# book_BM25_zscore <- book_BM25 %>%
# mutate(BM25_zscore = (book_BM25$bm25 -
# mean(book_BM25$bm25))/sd(book_BM25$bm25))
# # book_BM25_zscore_Pos <- book_BM25_zscore %>% mutate(BM25_zscore_Pos =
# # (book_BM25_zscore$BM25_zscore-min(book_BM25_zscore$BM25_zscore)))
##### 3.4 A corpus of physics texts #####
library(gutenbergr)
physics <- gutenberg_download(c(37729, 14725, 13476, 30155),
meta_fields = "author")
physics_words <- physics %>%
unnest_tokens(word, text) %>%
count(author, word, sort = TRUE)
physics_words
plot_physics <- physics_words %>%
BM25Score(word, author, n) %>%
mutate(author = factor(author, levels = c("Galilei, Galileo",
"Huygens, Christiaan",
"Tesla, Nikola",
"Einstein, Albert")))
plot_physics %>%
group_by(author) %>%
slice_max(bm25, n = 15) %>%
ungroup() %>%
mutate(word = reorder(word, bm25)) %>%
ggplot(aes(bm25, word, fill = author)) +
geom_col(show.legend = FALSE) +
labs(x = "BM25", y = NULL) +
facet_wrap(~author, ncol = 2, scales = "free")
## Remove Stop Word
library(stringr)
physics %>%
filter(str_detect(text, "_k_")) %>%
select(text)
physics %>%
filter(str_detect(text, "RC")) %>%
select(text)
mystopwords <- tibble(word = c("eq", "co", "rc", "ac", "ak", "bn",
"fig", "file", "cg", "cb", "cm",
"ab", "_k", "_k_", "_x"))
physics_words <- anti_join(physics_words, mystopwords,
by = "word")
plot_physics <- physics_words %>%
BM25Score(word, author, n) %>%
mutate(word = str_remove_all(word, "_")) %>%
group_by(author) %>%
slice_max(bm25, n = 15) %>%
ungroup() %>%
mutate(word = reorder_within(word, bm25, author)) %>%
mutate(author = factor(author, levels = c("Galilei, Galileo",
"Huygens, Christiaan",
"Tesla, Nikola",
"Einstein, Albert")))
ggplot(plot_physics, aes(word, bm25, fill = author)) +
geom_col(show.legend = FALSE) +
labs(x = NULL, y = "BM25") +
facet_wrap(~author, ncol = 2, scales = "free") +
coord_flip() +
scale_x_reordered()