local-test

1. This one runs without OpenAI API Key. (So, not exactly what you are looking for). But I don't have OpenAI API Key. This is how I debugged the code locally on my work computer. 2. Please change the 'doc_path' 3. Used AI to debug the code to save time.
 avatar
unknown
r
10 months ago
5.1 kB
20
Indexable
library(officer)
library(dplyr)
library(stringr)
library(jsonlite)

# Read Word document
doc_path <- "C:/Users/mi.haque/Desktop/SDW-reflections.docx"
doc <- read_docx(doc_path)

# Extract paragraph text as vector
paragraphs <- docx_summary(doc) %>% 
  filter(content_type == "paragraph") %>% 
  pull(text)

# Improved function to identify student name lines
is_student_name <- function(text) {
  # Remove common non-name all-caps patterns
  if (grepl("^[IVXLC]+\\.?$", text)) return(FALSE)  # Roman numerals (fixed escape)
  if (grepl("^[0-9]+$", text)) return(FALSE)         # Numbers only
  if (grepl("^[A-Z]{1,3}$", text)) return(FALSE)     # Short acronyms (1-3 letters)
  
  txt <- gsub("[[:punct:] ]", "", text)
  nchar(txt) > 3 && txt == toupper(txt)
}

# Find indices of student name lines
name_indices <- which(sapply(paragraphs, is_student_name))

# Verify we found reasonable number of students
print(paste("Found", length(name_indices), "potential student names"))
print("Student names found:")
print(paragraphs[name_indices])

# Group paragraphs into submissions based on names
submissions <- list()
for (i in seq_along(name_indices)) {
  start <- name_indices[i]
  end <- if (i < length(name_indices)) name_indices[i + 1] - 1 else length(paragraphs)
  submission_text <- paste(paragraphs[start:end], collapse = "\n")
  submissions[[paragraphs[start]]] <- submission_text
}

# Mock function to simulate API responses
analyze_reflection_cot <- function(text) {
  # Simulate API delay
  Sys.sleep(0.1)
  
  # Create a mock response with random data for testing
  themes <- c(
    "Anatomical variation",
    "Pathology",
    "Clinical relevance in identification of anatomical variation",
    "Comparison to textbook images",
    "Effect of anatomical variation on development and assessment of pathological conditions",
    "Experience-based learning and peer sharing",
    "Adaptability and flexibility",
    "Encountering previously unknown variations",
    "Development of dissecting skills and techniques"
  )
  
  # Generate random responses
  mock_response <- lapply(themes, function(theme) {
    presence <- sample(c("yes", "no"), 1, prob = c(0.7, 0.3))
    reasoning <- ifelse(presence == "yes",
                        paste("The theme", theme, "is clearly present in the reflection."),
                        paste("The theme", theme, "is not significantly present in the reflection."))
    
    list(theme = theme, presence = presence, reasoning = reasoning)
  })
  
  return(mock_response)
}

# Analyze each reflection and store results
cot_results <- lapply(submissions, analyze_reflection_cot)

# Convert CoT results to numeric data frame
process_cot_results <- function(cot_results) {
  # Define all expected themes
  all_themes <- c(
    "Anatomical variation",
    "Pathology",
    "Clinical relevance in identification of anatomical variation",
    "Comparison to textbook images",
    "Effect of anatomical variation on development and assessment of pathological conditions",
    "Experience-based learning and peer sharing",
    "Adaptability and flexibility",
    "Encountering previously unknown variations",
    "Development of dissecting skills and techniques"
  )
  
  # Create empty matrix
  theme_matrix <- matrix(0, nrow = length(cot_results), ncol = length(all_themes))
  colnames(theme_matrix) <- all_themes
  rownames(theme_matrix) <- names(cot_results)
  
  # Fill matrix with results
  for (i in seq_along(cot_results)) {
    result <- cot_results[[i]]
    if (!is.null(result)) {
      for (j in seq_along(result)) {
        theme_item <- result[[j]]
        if (is.list(theme_item) && 
            "theme" %in% names(theme_item) && 
            "presence" %in% names(theme_item)) {
          
          theme_name <- theme_item$theme
          presence <- tolower(theme_item$presence)
          
          if (theme_name %in% all_themes) {
            theme_matrix[i, theme_name] <- ifelse(grepl("^yes", presence), 1, 0)
          }
        }
      }
    }
  }
  
  return(as.data.frame(theme_matrix))
}

# Process the results
theme_data <- process_cot_results(cot_results)

# Check the structure and contents of the theme data
print("Column names:")
print(colnames(theme_data))

print("Structure:")
str(theme_data)

print("First few rows:")
head(theme_data)

# Save results to CSV for further analysis
write.csv(theme_data, "reflection_theme_analysis_debug.csv", row.names = TRUE)

# Display summary of theme prevalence
theme_summary <- colSums(theme_data)
print("Theme prevalence:")
print(theme_summary)

# Check if all submissions were processed
print(paste("Processed", length(submissions), "submissions"))
print(paste("Created data frame with", nrow(theme_data), "rows and", ncol(theme_data), "columns"))

# Optional: Display a sample of the processed data
print("Sample of processed data:")
sample_idx <- sample(1:min(5, nrow(theme_data)), 1)
print(theme_data[sample_idx, ])
Editor is loading...
Leave a Comment