# =============================================================================
# HSCI 841 Qualitative Research Methods and Analysis in Public Health  -  Lesson 9: Schema and Narrative Analysis
# Answer key for the in-lesson R activities
# Data file(s): term projects/HSCI_841/taguette_loneliness_highlights.csv
#               (download from the lesson page; paths are relative to the course
#                repository root, so set your working directory there first)
# Packages: tidyverse (dplyr, tidyr, readr, stringr)
#           (install once with install.packages("tidyverse"))
# Reproduces the lesson's code block and reports the cross-case narrative summary.
# =============================================================================

suppressPackageStartupMessages({
  library(tidyverse)
  library(stringr)
})

# ==== Section 1: Reading Taguette exports of narrative fragments into a tidy dataframe ====

# Read the Taguette highlights export
# NOTE (answer key): the file lives with the rest of the HSCI 841 material, so the
# path is "term projects/HSCI_841/taguette_loneliness_highlights.csv", not a bare
# filename. Columns: id, document, tag, content, start, end.
narratives <- read_csv("term projects/HSCI_841/taguette_loneliness_highlights.csv",
                       show_col_types = FALSE) %>%
  filter(str_starts(tag, "narrative_")) %>%
  mutate(
    frank_type    = str_remove(tag, "narrative_"),
    participant   = str_extract(document, "P\\d{2}_[A-Za-z]+"),
    fragment_chars = nchar(content)
  ) %>%
  select(participant, frank_type, content, fragment_chars)

cat("fragments:", nrow(narratives), "across",
    n_distinct(narratives$participant), "participants\n")
print(narratives %>% count(frank_type))
print(narratives %>% summarise(shortest = min(fragment_chars),
                               median   = median(fragment_chars),
                               longest  = max(fragment_chars)))

# Cross-case summary: how many fragments of each type per participant?
cross_case <- narratives %>%
  count(participant, frank_type) %>%
  pivot_wider(names_from = frank_type, values_from = n, values_fill = 0) %>%
  arrange(desc(quest + restitution + chaos))
print(cross_case, n = 25)

# A working "dominant type" column for each participant
participant_dominant <- narratives %>%
  count(participant, frank_type) %>%
  group_by(participant) %>%
  slice_max(n, n = 1, with_ties = FALSE) %>%
  ungroup() %>%
  rename(dominant_type = frank_type)
print(participant_dominant, n = 25)
cat("\nDominant type across the corpus:\n")
print(participant_dominant %>% count(dominant_type))

# Pull all quest fragments for close re-reading
quest_fragments <- narratives %>%
  filter(frank_type == "quest") %>%
  arrange(participant)

writeLines(paste0("--- ", quest_fragments$participant, " ---\n",
                quest_fragments$content, "\n"))

# ==== Section 2: What the cross-case table shows ====
# Chaos fragments are the most common in this corpus and every participant has at
# least two narrative types on record, so no transcript is a pure Frank type. The
# participants whose only fragments are quest fragments are the ones who have had
# the longest time to make sense of the loss.
cat("\nParticipants with a quest fragment:",
    paste(sort(unique(quest_fragments$participant)), collapse = ", "), "\n")

cat("\nDone: Lesson 9 answer key completed.\n")
