library(Rmisc)
library(tidyverse)
library(stringr)
library(scales)
library(grid)
library(ggpubr)
library(MASS)
# library(lmerTest)
library(rsample)
library(lme4)
library(brms)
library(ppcor)
library(stats)
library(modelr)
library(plotrix)
library(mgcv)
library(hexbin)
library(formattable)

rm(list=ls())


# Read the raw lines
lines <- readLines("DillonEtAl2013_Experiment1.txt", warn = FALSE)

# Drop trailing spaces and blank lines (the file has both)
lines <- trimws(lines)
lines <- lines[lines != ""]

# Split each line into: number, number, rest of line
parts <- regmatches(lines, regexec("^(\\d+)\\s+(\\d+)\\s+(.*)$", lines))

stim <- data.frame(
  Condition = as.integer(sapply(parts, `[`, 2)),
  Item      = as.integer(sapply(parts, `[`, 3)),
  Sentence  = sapply(parts, `[`, 4),
  stringsAsFactors = FALSE
)

stim$Condition <- letters[stim$Condition]

stim <- stim %>%
  mutate(
    Dep = ifelse(Condition %in% c('a', 'b', 'c', 'd'), "agreement", "reflexives"),
    Gram = ifelse(Condition %in% c('a', 'b', 'e', 'f'), "grammatical", "ungrammatical"),
    Attr = ifelse(Condition %in% c('a', 'c', 'e', 'g'), "singular", "plural")
  ) %>%
  dplyr::select(Item, Condition, Dep, Gram, Attr, Sentence)

# Quick check: should be 384 rows, 8 conditions x 48 items
str(stim)
table(stim$Condition)


write.csv(stim, "DillonEtAl2013_Experiment1.csv", row.names = FALSE)



