
# The data frame df has the following columns:
# sign_name: chr: sign names
# type:      chr: grammatical type of the word
# meaning:   chr: the english translation of the word/phrase

beautify_translations <- function(df) {

  # Helper: get the effective base type for a type string
  # For plain types like "S", "Vt" -> base type ("S", "V")
  # For operators like "xS->V" -> result type's base ("V")
  effective_base <- function(type_str) {
    if (is.na(type_str) || type_str == "") return(NA_character_)
    parsed <- parse_type(type_str)
    rt <- get_result_type(parsed)
    if (is.na(rt)) return(NA_character_)
    base_type(rt)
  }

  # Replace operator "S☒→V" with "Vt"
  df$type[df$type == paste0("S\u2612\u2192V")] <- "Vt"

  for (i in seq_len(nrow(df))) {
    eb <- effective_base(df$type[i])
    if (is.na(eb)) next

    meaning <- df$meaning[i]

    if (eb == "S") {
      # Strip leading article from meaning
      meaning <- sub("^(the|a|an|The|A|This|this)\\s+", "", meaning)
      # Strip leading article from inside {} blocks
      meaning <- gsub("\\{(the|a|an|The|A|This|this)\\s+([^}]*)\\}", "{\\2}", meaning)
    } else if (eb == "V") {
      # Strip leading "to" from meaning
      meaning <- sub("^to\\s+", "", meaning)
      # Strip leading "to" from inside {} blocks
      meaning <- gsub("\\{to\\s+([^}]*)\\}", "{\\1}", meaning)
    }

    meaning <- str_replace_all(trimws(meaning), "^,+|,+$", "")

    df$meaning[i] <- trimws(meaning)
  }

  return(df)
}
