---
title: "Abstract Example"
author: "Rachel Rogers"
date: "2024-08-28"
output: html_document
---

# Fuzzy Collocation of length 5
```{r echo=FALSE, warning=FALSE, message=FALSE}
library(highlightr) 
library(readr)
library(dplyr)
library(ggplot2)
library(stringr)
library(plotly)

# Read in abstract data frame
abstract <- readr::read_csv("../data/abstracts.csv")

# calculate collocation frequencies using fuzzy matching and mapping to source document
merged_frequency <- collocation_frequency(abstract, source_row=which(abstract$Conference=="Dissertation"), text_column = "page_notes", fuzzy=TRUE)

# create ggplot object of frequencies in the source document
freq_plot <- collocation_plot(merged_frequency)

# add html tags to create highlighted testimony
page_highlight <- highlighted_text(freq_plot)
```

`r page_highlight`

# Fuzzy Collocation of length 5 with n_gram_width of 2 instead of 4
```{r echo=FALSE, warning=FALSE, message=FALSE}

# use fuzzy matching to calculate collocation frequency between transcripts
# here, the n_gram_width is reduced from the default of 4 to 2
merged_frequency <- collocation_frequency(abstract, source_row=which(abstract$Conference=="Dissertation"), text_column = "page_notes", fuzzy=TRUE, n_gram_width = 2)

# create ggplot object of frequencies in the source document
freq_plot <- collocation_plot(merged_frequency)

# add html tags to create highlighted testimony
page_highlight <- highlighted_text(freq_plot)
```

`r page_highlight`

# Non-Fuzzy Collocation of length 5
```{r echo=FALSE, warning=FALSE, message=FALSE}

# use nonfuzzy matching to calculate collocation frequency between transcripts
merged_frequency <- collocation_frequency(abstract, source_row=which(abstract$Conference=="Dissertation"), text_column = "page_notes", fuzzy=FALSE)

# create ggplot object of frequencies in the source document
freq_plot <- collocation_plot(merged_frequency)

# add html tags to create highlighted testimony
page_highlight1 <- highlighted_text(freq_plot)
```

`r page_highlight1`

```{r echo=FALSE, message=FALSE, warning=FALSE, eval=FALSE}

# Evaluation of collocation length and its effect on the sum of the adjacent word frequency distance squared

# Creating a data frame for collocation lengths from 2 to 20
collocate_distance_abstract <- data.frame(collocate_length = 2:20, distance=NA)

# For each collocation length, use fuzzy matching and calculate frequency, as well as frequency distance squared
for (j in 2:20){

  # collocate_length is used to specify the length of the collocations
  frequency_abs <- collocation_frequency(abstract, source_row=which(abstract$Conference=="Dissertation"), text_column = "page_notes", fuzzy=TRUE, collocate_length = j)

  # Save frequency values for plotting reasons
  if (j == 2){
    collocate_frequency_data <- frequency_abs %>% select(words, word_num, Freq)
    collocate_frequency_data$collocation_length <- 2
  }else{
    frequency_temp <- frequency_abs %>% select(words, word_num, Freq)
    frequency_temp$collocation_length <- j
    collocate_frequency_data <- rbind(collocate_frequency_data, frequency_temp)
  }

  # Remove formatting rows that do not contain collocations
  na_removed <- frequency_abs[!is.na(frequency_abs$collocation),]

  # For each row in the dataframe, calculate and square the difference in frequency between the current and next row, then add to the sum
  sum <- 0
  for (i in 1:(length(na_removed$Freq)-1)){
    sum <- sum+(na_removed$Freq[i]-na_removed$Freq[i+1])^2
  }
  # Save the sum in the appropriate row of the collocation data frame
  collocate_distance_abstract[collocate_distance_abstract$collocate_length==j,]$distance<-sum

}

# Save data frame as a .csv for reference in the main article
write.csv(collocate_distance_abstract, "../data/abstract_collocation_comparison.csv")

write.csv(collocate_frequency_data, "../data/abstract_collocation_frequency.csv")

# Label add word label to sequential words
word_label <- collocate_frequency_data %>% select(word_num, words) %>% distinct()

# Plot frequency by word for each collocation
colplot <- ggplot(filter(collocate_frequency_data, collocation_length<=10), 
                  aes(x=as.factor(word_num), y=Freq, color=collocation_length, group=collocation_length, key=words))+geom_line()+
  scale_color_continuous(palette=c("grey80", "black"))+
  theme_bw()+
  geom_line(data=filter(collocate_frequency_data, collocation_length==5), 
            aes(x=word_num, y=Freq), color="red")+
  xlim(1,max(collocate_frequency_data$word_num))+
  scale_x_discrete(labels=word_label$words, breaks=word_label$word_num, guide = guide_axis(check.overlap = TRUE))

# create interactive plot
ggplotly(colplot) %>%
  rangeslider(start=269, end=276)

```

```{r echo=FALSE, message=FALSE, warning=FALSE, eval=FALSE}

# Evaluation of threshold value and its effect on frequency

# For each threshold value, use fuzzy matching and calculate frequency
for (j in seq(from=0.5, to=0.95, by=0.05)){

  # collocate_length is used to specify the length of the collocations
  frequency_abs <- collocation_frequency(abstract, source_row=which(abstract$Conference=="Dissertation"), text_column = "page_notes", fuzzy=TRUE, threshold = j)

  # Save frequency values for plotting reasons
  if (j == 0.5){
    collocate_threshold_data <- frequency_abs %>% select(words, word_num, Freq)
    collocate_threshold_data$threshold <- 0.5
  }else{
    frequency_temp <- frequency_abs %>% select(words, word_num, Freq)
    frequency_temp$threshold <- j
    collocate_threshold_data <- rbind(collocate_threshold_data, frequency_temp)
  }

}

# Save data frame as a .csv for reference in the main article
write.csv(collocate_threshold_data, "../data/abstract_threshold_frequency.csv")

word_label <- collocate_threshold_data %>% select(word_num, words) %>% distinct()

colplot <- ggplot(filter(collocate_threshold_data), 
                  aes(x=as.factor(word_num), y=Freq, color=threshold,
                      group=threshold, key=words))+geom_line()+
  scale_color_continuous(palette=c("grey80", "black"))+
  theme_bw()+
  geom_line(data=filter(collocate_threshold_data, threshold==0.7), 
            aes(x=word_num, y=Freq), color="red")+
  xlim(1,max(collocate_threshold_data$word_num))+
  scale_x_discrete(labels=word_label$words, breaks=word_label$word_num, guide = guide_axis(check.overlap = TRUE))

ggplotly(colplot) %>%
  rangeslider(start=1, end=10)

```

```{r echo=FALSE, message=FALSE, warning=FALSE, eval=FALSE}

# Evaluation of ngram width value and its effect on frequency

# For each ngram width, use fuzzy matching and calculate frequency
for (j in 2:10){

  # collocate_length is used to specify the length of the collocations
  frequency_abs <- collocation_frequency(abstract, source_row=which(abstract$Conference=="Dissertation"), text_column = "page_notes", fuzzy=TRUE, n_gram_width = j)

  # Save frequency values for plotting reasons
  if (j == 2){
    collocate_ngram_data <- frequency_abs %>% select(words, word_num, Freq)
    collocate_ngram_data$ngram <- 2
  }else{
    frequency_temp <- frequency_abs %>% select(words, word_num, Freq)
    frequency_temp$ngram <- j
    collocate_ngram_data <- rbind(collocate_ngram_data, frequency_temp)
  }

}

# Save data frame as a .csv for reference in the main article
write.csv(collocate_ngram_data, "../data/abstract_ngram_frequency.csv")

word_label <- collocate_ngram_data %>% select(word_num, words) %>% distinct()

colplot <- ggplot(filter(collocate_ngram_data), 
                  aes(x=as.factor(word_num), y=Freq, color=ngram,
                      group=ngram, key=words))+geom_line()+
  scale_color_continuous(palette=c("grey80", "black"))+
  theme_bw()+
  geom_line(data=filter(collocate_ngram_data, ngram==4), 
            aes(x=word_num, y=Freq), color="red")+
  xlim(1,max(collocate_ngram_data$word_num))+
  scale_x_discrete(labels=word_label$words, breaks=word_label$word_num, guide = guide_axis(check.overlap = TRUE))

ggplotly(colplot) %>%
  rangeslider(start=1, end=10)

```

# Fuzzy Collocation of length 3

```{r echo=FALSE, warning=FALSE, message=FALSE}

# use fuzzy matching for collocations of length 3
merged_frequency3 <- collocation_frequency(abstract, source_row=which(abstract$Conference=="Dissertation"), text_column = "page_notes", fuzzy=TRUE, collocate_length = 3)

# create ggplot object of frequencies in the source document
freq_plot3 <- collocation_plot(merged_frequency3)

# add html tags to create highlighted testimony
page_highlight3 <- highlighted_text(freq_plot3)
```

`r page_highlight3`

# Fuzzy Collocation of length 4
```{r echo=FALSE, warning=FALSE, message=FALSE}

# use fuzzy matching for collocations of length 4
merged_frequency4 <- collocation_frequency(abstract, source_row=which(abstract$Conference=="Dissertation"), text_column = "page_notes", fuzzy=TRUE, collocate_length = 4)

# create ggplot object of frequencies in the source document
freq_plot4 <- collocation_plot(merged_frequency4)

# add html tags to create highlighted testimony
page_highlight4 <- highlighted_text(freq_plot4)
```

`r page_highlight4`
