|
| 1 | +#' read_1file |
| 2 | +#' |
| 3 | +#' Reads pre-formatted dyadic (2 interlocutor) conversation transcript already imported into your R environment. |
| 4 | +#' |
| 5 | +#' @name read_1file |
| 6 | +#' @param my_dat conversation transcript in csv or txt format |
| 7 | +#' @return a dataframe formatted with 'Event_ID', "Participant_ID", "RawText" -- ready for clean_dyads() |
| 8 | +#' @export |
| 9 | + |
| 10 | +read_1file <- function(my_dat) { |
| 11 | + #returns name not contents of mydat |
| 12 | + object_name <- deparse(substitute(my_dat)) |
| 13 | + |
| 14 | + # Convert to data frame if not already |
| 15 | + if (!is.data.frame(my_dat)) { |
| 16 | + my_dat <- as.data.frame(my_dat) |
| 17 | + } |
| 18 | + |
| 19 | + # Store original column names for reference |
| 20 | + original_cols <- colnames(my_dat) |
| 21 | + |
| 22 | + # Standardize column names (case-insensitive) |
| 23 | + colnames(my_dat) <- tolower(colnames(my_dat)) |
| 24 | + |
| 25 | + # Initialize standardized columns |
| 26 | + standardized_cols <- colnames(my_dat) |
| 27 | + |
| 28 | + # Participant ID detection and standardization |
| 29 | + participant_pattern <- "speaker|speaker_names_raw|participant|interlocutor|patient|person|partner|source|pid|talker" |
| 30 | + participant_idx <- grepl(participant_pattern, colnames(my_dat)) |
| 31 | + if (sum(participant_idx) > 0) { |
| 32 | + standardized_cols[participant_idx] <- "Participant_ID" |
| 33 | + } |
| 34 | + |
| 35 | + # RawText detection and standardization |
| 36 | + text_pattern <- "text|turn|talker|mytext|utterance|my_text" |
| 37 | + text_idx <- grepl(text_pattern, colnames(my_dat)) |
| 38 | + if (sum(text_idx) > 0) { |
| 39 | + standardized_cols[text_idx] <- "RawText" |
| 40 | + } |
| 41 | + |
| 42 | + # Apply standardized names |
| 43 | + colnames(my_dat) <- standardized_cols |
| 44 | + |
| 45 | + # Check required columns exist |
| 46 | + required_cols <- c("Participant_ID", "RawText") |
| 47 | + missing_cols <- setdiff(required_cols, colnames(my_dat)) |
| 48 | + |
| 49 | + if (length(missing_cols) > 0) { |
| 50 | + stop(paste("Missing required columns:", |
| 51 | + paste(missing_cols, collapse = ", "), |
| 52 | + "\nAvailable columns:", |
| 53 | + paste(original_cols, collapse = ", "), |
| 54 | + "\nExpected participant columns should match:", participant_pattern, |
| 55 | + "\nExpected text columns should match:", text_pattern), |
| 56 | + call. = FALSE) |
| 57 | + } |
| 58 | + |
| 59 | + # Add Event_ID using the object's name |
| 60 | + my_dat$Event_ID <- object_name |
| 61 | + |
| 62 | + # Convert ID columns to factors |
| 63 | + id_cols <- c("Event_ID", "Participant_ID") |
| 64 | + for (col in id_cols) { |
| 65 | + if (col %in% colnames(my_dat)) { |
| 66 | + my_dat[[col]] <- as.factor(my_dat[[col]]) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + # Reorder columns to put standard ones first |
| 71 | + standard_cols <- c("Event_ID", "Participant_ID", "RawText") |
| 72 | + other_cols <- setdiff(colnames(my_dat), standard_cols) |
| 73 | + my_dat <- my_dat[, c(standard_cols, other_cols)] |
| 74 | + |
| 75 | + return(my_dat) |
| 76 | +} |
0 commit comments