-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp_techniques.qmd
255 lines (209 loc) · 7.34 KB
/
nlp_techniques.qmd
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
---
title: "Systematic literature review"
bibliography: references.bib
title-block-banner: true
subtitle: "NLP techniques used in marketing"
author:
- name: Olivier Caron
email: olivier.caron@dauphine.psl.eu
affiliations:
name: "Paris Dauphine - PSL"
city: Paris
state: France
- name: Christophe Benavent
email: christophe.benavent@dauphine.psl.eu
affiliations:
name: "Paris Dauphine - PSL"
city: Paris
state: France
date : "last-modified"
toc: true
number-sections: true
number-depth: 5
format:
html:
theme:
light: yeti
dark: darkly
code-fold: true
code-summary: "Display code"
code-tools: true #enables to display/hide all blocks of code
code-copy: true #enables to copy code
grid:
body-width: 1000px
margin-width: 100px
toc: true
toc-location: left
execute:
echo: true
warning: false
message: false
editor: visual
fig-align: "center"
highlight-style: ayu
css: styles.css
reference-location: margin
---
## Libraries R and loading data
```{r}
#| label: load-packages
#| message: false
library(tidyverse)
library(reactable)
library(plotly)
library(wesanderson)
```
```{r}
#load data
data_embeddings <- read.csv("data_for_embeddings.csv")
annotations_stanza <- read.csv("annotated_stanza.csv")
annotations_udpipe <- read.csv("annotated_udpipe.csv")
annotations_trankit <- read.csv("annotated_trankit.csv")
```
## Loading of all annotations (Stanza, UDPipe, Trankit)
```{r}
#| label: list-annotations
#annotation filter on PROPN
annotations_stanza_propn <- annotations_stanza %>% filter(upos == "PROPN")
annotations_udpipe_propn <- annotations_udpipe %>% filter(upos == "PROPN")
annotations_trankit_propn <- annotations_trankit %>% filter(upos == "PROPN")
#list of most frequent words in annotatinos
toppropn_stanza <- annotations_stanza_propn %>% count(lemma, sort = TRUE) %>% drop_na() %>% filter(n > 1)
toppropn_udpipe <- annotations_udpipe_propn %>% count(lemma, sort = TRUE) %>% drop_na() %>% filter(n > 1)
toppropn_trankit <- annotations_trankit_propn %>% count(lemma, sort = TRUE) %>% drop_na() %>% filter(n > 1)
```
### Table of most frequent PROPN words in the Stanza annotations
```{r}
#| label: reactable-stanza
#reactable of most frequent words in annotations_propn
toppropn_stanza %>%
reactable(
searchable = TRUE,
defaultColDef = colDef(
minWidth = 100,
sortable = TRUE
)
)
```
### Table of most frequent PROPN words in the UDPipe annotations
```{r}
#| label: reactable-udpipe
#reactable of most frequent words in annotations_propn
toppropn_udpipe %>%
reactable(
searchable = TRUE,
defaultColDef = colDef(
minWidth = 100,
sortable = TRUE
)
)
```
### Table of most frequent PROPN words in the Trankit annotations
```{r}
#| label: reactable-trankit
#reactable of most frequent words in annotations_propn
toppropn_trankit %>%
reactable(
searchable = TRUE,
defaultColDef = colDef(
minWidth = 100,
sortable = TRUE
)
)
```
## NLP techniques in Marketing
### Generic techniques
We want to provide a list of recurrent techniques in marketing. The tables above clearly show that some of them are often used. Let's focus first on them in no particular order.
- LIWC
- Leximancer
- BERT
- ChatGPT
- PassivePy
```{r}
#i want to get just one number if multiple words of the vector are found in the string
#detect and count the words above in the "combined_text" column of data_embeddings
data_embeddings$combined_text <- tolower(data_embeddings$combined_text)
data_embeddings$liwc <- str_count(data_embeddings$combined_text, "liwc")
data_embeddings$leximancer <- str_count(data_embeddings$combined_text, "leximancer")
data_embeddings$bert <- str_count(data_embeddings$combined_text, "bert")
data_embeddings$chatgpt <- str_count(data_embeddings$combined_text, "chatgpt")
data_embeddings$passivepy <- str_count(data_embeddings$combined_text, "passivepy")
# Group by year and calculate the cumulative sum for each technique
sum_data <- data_embeddings %>%
group_by(year) %>%
summarize(
sum_liwc = sum(liwc),
sum_leximancer = sum(leximancer),
sum_bert = sum(bert),
sum_chatgpt = sum(chatgpt),
sum_passivepy = sum(passivepy)
) %>%
ungroup()
cumulative_data <- sum_data %>%
mutate(
cum_liwc = cumsum(sum_liwc),
cum_leximancer = cumsum(sum_leximancer),
cum_bert = cumsum(sum_bert),
cum_chatgpt = cumsum(sum_chatgpt),
cum_passivepy = cumsum(sum_passivepy)
) %>%
filter(year > 2012)
patterns <- c("linguistic inquiry and word count", "linguistic inquiry", "linguistic inquiry word count", "liwc")
# Create a new column "liwc_alt" with initial empty strings
data_embeddings$liwc_alt <- ""
# Find the column index for "liwc_alt"
ncol <- which(colnames(data_embeddings) == "liwc_alt")
# Loop through each row and pattern to count occurrences and populate "liwc_alt"
for (i in 1:nrow(data_embeddings)) {
counts <- sapply(patterns, function(pattern) {
str_count(data_embeddings$combined_text[i], pattern)
})
data_embeddings$liwc_alt[i] <- sum(counts)
}
liwc_df <- data_embeddings %>% select(combined_text, liwc, liwc_alt)
reactable(liwc_df, searchable = TRUE,
defaultPageSize = 3, # Set the maximum number of rows to display
columns = list(
combined_text = colDef(width = 850),
liwc = colDef(width = 50),
liwc_alt = colDef(width = 50)
))
```
It seems that when researchers talk about LIWC, they always include "liwc" but not always "linguistic inquiry and word count" or other variations. We can therefore concentrate on the unique detection of "liwc", "bert" and other acronyms in the text.
We use the `wesanderson` package to get a nice palette of colors. We pick the `Royal2` palette.
```{r}
#| label: plotly-techniques
Royal2 <- wesanderson::wes_palette("Royal2")
fig <- ggplot(cumulative_data, aes(x = year)) +
geom_line(aes(y = cum_liwc, linetype = "LIWC", color = "LIWC")) +
geom_line(aes(y = cum_leximancer, linetype = "Leximancer", color = "Leximancer")) +
geom_line(aes(y = cum_bert, linetype = "BERT", color = "BERT")) +
geom_line(aes(y = cum_chatgpt, linetype = "ChatGPT", color = "ChatGPT")) +
geom_line(aes(y = cum_passivepy, linetype = "PassivePy", color = "PassivePy")) +
scale_color_manual(
name = "Techniques",
values = Royal2
) +
scale_linetype_manual(
name = "Techniques",
values = c(
"LIWC" = "solid",
"Leximancer" = "dashed",
"BERT" = "dotted",
"ChatGPT" = "dotdash",
"PassivePy" = "longdash"
),
guide = guide_legend(override.aes = list(linetype = "solid")) # Set the legend linetype to "solid"
) +
labs(
title = "Evolution of NLP techniques in marketing",
subtitle = "Cumulative sum of the number of articles mentioning each technique",
x = "",
y = "Cumulative number of occurrences"
) +
scale_x_continuous(breaks = seq(2010, 2023, by = 1), labels = seq(2010, 2023, by = 1)) + # Set breaks and labels
theme_minimal() +
theme(legend.position = "bottom") # Move the legend to the bottom for better visibility
ggplotly(fig)
```
It seems like Leximancer is coming to an end, while BERT and ChatGPT are on the rise. PassivePy is also gaining traction but we can't really conclude anything about it yet since the article has been published in 2022 @(sepehri2022passivepy). One interesthing to notice is that BERT