-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKristof_content.R
102 lines (59 loc) · 2.41 KB
/
Kristof_content.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
library(readr)
library(tidyverse)
library(tidytext)
library(topicmodels)
content <- read_csv('../../../Downloads/content.csv')
content[3] <- NULL
content[4] <- NULL
train_on <- data.frame(content)
#train_on$text
train_on$text <- gsub("\\s*\\([^\\)]+\\)", " ", train_on$text)
train_on$text <- gsub("\\{.*?\\}", " ", train_on$text)
train_on$text <- gsub("\\(.*?\\)", " ", train_on$text)
train_on$text <- gsub("\\[.*?\\]", " ", train_on$text)
train_on$text <- gsub('[0-9]+', '', train_on$text)
train_on$text <- gsub("\\s+", " ", train_on$text)
train_on$text <- gsub("'.*? ", ' ', train_on$text)
train_on$text <- gsub('[[:punct:]]+', ' ', train_on$text)
train_on$text <- gsub("\\s+", " ", train_on$text)
train_on$text <- tolower(train_on$text)
count_by_url <- train_on %>%
unnest_tokens(word, text) %>%
anti_join(bind_rows(data.frame(word = stop_words$word),
data.frame(word = stopwords::stopwords(source = 'smart')),
data.frame(word = stopwords::stopwords(source = 'marimo')),
data.frame(word = stopwords::stopwords(source = 'snowball')),
data.frame(word = stopwords::stopwords(source = 'nltk')),
data.frame(word = c('yeah', 'gonna', 'uh', 'alright', 'um', 'lot', 'hey')))) %>%
count(url, word, sort = T) %>%
ungroup()
write.csv(count_by_url, "G:/.shortcut-targets-by-id/0B23Ot7AW9q8dTHJCSU1MT1A2SzQ/Common/Homokozó/nlp-hackathon/data/collected/content_unnested.csv")
dtm <- count_by_url %>% cast_dtm(url, word, n)
lda <- dtm %>% LDA(k = 3, control = list(seed = 123))
# betas
lda_betas <- tidy(lda, matrix = 'beta')
lda_betas %>%
group_by(topic) %>%
top_n(15, beta) %>%
ungroup() %>%
mutate(term = as.factor(term),
term = reorder_within(term, beta, topic)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
geom_col(show.legend = F, width = 2/3) +
facet_wrap(~topic, scales = 'free') +
coord_flip() +
scale_x_reordered() +
theme_bw()
# gammas
lda_gammas <- tidy(lda, matrix = 'gamma')
lda_gammas %>%
rename('name' = 'document') %>%
mutate(topic = as.factor(topic),
name = as.factor(name)) %>%
ggplot(aes(topic, gamma, fill = name)) +
geom_point(show.legend = F, color = 'black', shape = 8) +
facet_wrap(~name, scales = 'free') +
theme_bw()
# group_by(url) %>%
# summarize(text = str_c(word, collapse = " ")) %>%
# ungroup()