-
Notifications
You must be signed in to change notification settings - Fork 3
/
glove_BB.R
135 lines (95 loc) · 3.18 KB
/
glove_BB.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
############################################################################################################
### Glove on Bold and Beautifull recaps
library(stringr)
library(text2vec)
library(visNetwork)
library(ggplot2)
library(plotly)
library(stopwords)
#### import recpas of BB ################################################################
AllBB = readRDS("data/AllBB.RDs")
#### transform to lower and create tokens
AllBB$recapsclean = str_replace_all(AllBB$recaps, "\n", "") %>% tolower
AllBB$id = 1:dim(AllBB)[1]
stopw = c(stopwords::stopwords(), letters)
####### tokenize, iterate and create vocab ###################################
it_train = AllBB$recapsclean %>%
word_tokenizer() %>%
itoken(
ids = AllBB$id,
progressbar = TRUE
)
pruned_vocab = it_train %>%
create_vocabulary(
ngram = c(ngram_min = 1L, ngram_max = 1L),
stopwords = stopw
) %>%
prune_vocabulary(
term_count_min = 5 ,
doc_proportion_max = 0.95
)
vectorizer <- vocab_vectorizer(
pruned_vocab
)
#### Create the so-called term co-occurence matrix ############################
tcm <- create_tcm(
it_train,
vectorizer,
skip_grams_window = 5L
)
## first two words in the tcm matrix are "miraculously" and fearfully
tcm[1:2,1:2]
## words that occur often in the neighborhood of 'miraculously'
x = tcm[1,]
x [x > 0]
####### Glove word embeddings
## This can take some time, about an hour on my little 4 core server.
t0 = proc.time()
glove = GlobalVectors$new(
word_vectors_size = 250,
vocabulary = pruned_vocab,
x_max = 10,
learning_rate = 0.07
)
word_vectors = glove$fit_transform(tcm, n_iter = 30)
dim (word_vectors)
t1 = proc.time()
t1-t0
## save the wordvectors
saveRDS(word_vectors, "data/word_vectors_BB.RDs")
word_vectors = readRDS("data/word_vectors_BB.RDs")
###### distances between some characters ################################################
BBchars = c("quinn", "eric", "steffy", "ridge", "bill", "brooke", "caroline", "liam", "thomas", "taylor", "rick", "bridget")
ff = function(word)
{
WV <- word_vectors[word, , drop = FALSE]
cos_sim = sim2(x = word_vectors, y = WV, method = "cosine", norm = "l2")
tmp = head(sort(cos_sim[,1], decreasing = TRUE), 8)
tibble::tibble(from = word, to = names(tmp), value = tmp)
}
BBcharsDistances = purrr::map_dfr(BBchars, ff)
## subtract mean just for plotting purposes....
## remove distance "one"
BBcharsDistances$value2 = BBcharsDistances$value - mean( BBcharsDistances$value)
BBcharsDistances = BBcharsDistances %>% dplyr::filter(value < 0.99)
#### create plot with character distances ###############################################
p = ggplot(
BBcharsDistances, aes(x = to)
) +
geom_bar(
aes(weight=value2), color="black"
) +
facet_wrap( ~from ) +
coord_flip() +
labs( y = "person similarity") +
ggtitle("Word-embedding distances between Bold & Beautiful characters")
p
ff("steffy")
### word minus other word linguistic regularities
twowords = function(w1,w2){
out = word_vectors[w1, , drop = FALSE] -
word_vectors[w2, , drop = FALSE]
cos_sim = sim2(x = word_vectors, y = out, method = "cosine", norm = "l2")
head(sort(cos_sim[,1], decreasing = TRUE), 7)
}
twowords("steffy", "liam")