#' Rename speakers in an interview transcript
#'
#' This function replaces specified speaker identifiers with new names 
#' throughout an interview transcript. It is useful for anonymizing 
#' data, standardizing speaker names, or correcting transcription 
#' errors. The replacement is performed using regular expressions to 
#' ensure exact matching of the speaker tag format "\[Name\]".
#'
#' @param interview A character vector representing the interview 
#'   transcript lines. Each line should start with a speaker tag in 
#'   the format `[Speaker Name]`.
#' @param current_speaker A character vector containing the current 
#'   speaker names to be replaced. These should not include the square 
#'   brackets or trailing space; the function handles the formatting 
#'   automatically.
#' @param new_speaker A character vector containing the replacement 
#'   speaker names. This vector must have the same length as 
#'   `current_speaker`, with each element corresponding to the speaker 
#'   at the same position in `current_speaker`.
#'
#' @return A character vector with the same structure as the input 
#'   `interview`, but with the specified speaker names replaced.
#'
#' @examples
#' speaker = retrieve_speaker(intrw)
#' intrw_renamed = rename_speaker(
#'     interview = intrw, 
#'     current_speaker = speaker[!speaker == "UNKNOWN"],
#'     new_speaker = c("Mary", "Marylou")
#' )
#'
#' @family process interview
#' 
#' @export



rename_speaker = function(interview, current_speaker, new_speaker){
    current_speaker = sprintf("\\[%s\\] ", current_speaker)
    new_speaker = sprintf("[%s] ", new_speaker)
    result = interview
    for(i in seq_along(current_speaker)) {
        result = gsub(current_speaker[i], new_speaker[i], result)
    }
    return(result)
}