Code
library(tidyverse)
library(reactable)
library(plotly)
library(wesanderson)
library(tidyverse)
library(reactable)
library(plotly)
library(wesanderson)
#load data
<- read.csv("data_for_embeddings.csv")
data_embeddings <- read.csv("annotated_stanza.csv")
annotations_stanza <- read.csv("annotated_udpipe.csv")
annotations_udpipe <- read.csv("annotated_trankit.csv") annotations_trankit
#annotation filter on PROPN
<- annotations_stanza %>% filter(upos == "PROPN")
annotations_stanza_propn <- annotations_udpipe %>% filter(upos == "PROPN")
annotations_udpipe_propn <- annotations_trankit %>% filter(upos == "PROPN")
annotations_trankit_propn
#list of most frequent words in annotatinos
<- annotations_stanza_propn %>% count(lemma, sort = TRUE) %>% drop_na() %>% filter(n > 1)
toppropn_stanza <- annotations_udpipe_propn %>% count(lemma, sort = TRUE) %>% drop_na() %>% filter(n > 1)
toppropn_udpipe <- annotations_trankit_propn %>% count(lemma, sort = TRUE) %>% drop_na() %>% filter(n > 1) toppropn_trankit
#reactable of most frequent words in annotations_propn
%>%
toppropn_stanza reactable(
searchable = TRUE,
defaultColDef = colDef(
minWidth = 100,
sortable = TRUE
) )
#reactable of most frequent words in annotations_propn
%>%
toppropn_udpipe reactable(
searchable = TRUE,
defaultColDef = colDef(
minWidth = 100,
sortable = TRUE
) )
#reactable of most frequent words in annotations_propn
%>%
toppropn_trankit reactable(
searchable = TRUE,
defaultColDef = colDef(
minWidth = 100,
sortable = TRUE
) )
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
#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
$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")
data_embeddings
# Group by year and calculate the cumulative sum for each technique
<- data_embeddings %>%
sum_data 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()
<- sum_data %>%
cumulative_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)
<- c("linguistic inquiry and word count", "linguistic inquiry", "linguistic inquiry word count", "liwc")
patterns
# Create a new column "liwc_alt" with initial empty strings
$liwc_alt <- ""
data_embeddings
# Find the column index for "liwc_alt"
<- which(colnames(data_embeddings) == "liwc_alt")
ncol
# Loop through each row and pattern to count occurrences and populate "liwc_alt"
for (i in 1:nrow(data_embeddings)) {
<- sapply(patterns, function(pattern) {
counts str_count(data_embeddings$combined_text[i], pattern)
})$liwc_alt[i] <- sum(counts)
data_embeddings
}
<- data_embeddings %>% select(combined_text, liwc, liwc_alt)
liwc_df 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.
<- wesanderson::wes_palette("Royal2")
Royal2
<- ggplot(cumulative_data, aes(x = year)) +
fig 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