Skip to content

Remove function reorder_3 #13

@adrbmdns

Description

@adrbmdns

I have rewrite the function, new function called reorder.

Keeping a record of reorder_3 here for now incase any issues.

# works for all
reorder_3 <- function(lvls, chat = NULL, ...) {

  chat$clone()
  chat$set_turns(list())

  out <- chat$chat(paste0(
    "Rank the sentiment scores for each level of the input: ",
    paste(lvls, collapse = ", "), ". ",
    "Positive connotations like satisfied or likely should have positive scores.
                    Negative connotations like unsatisfied or unlikely should have negative scores.
                    Satisfied should have a higher score than somewhat satisfied.
                    Agree should have a higher score than somewhat agree.
                    Neutral elements should have a score of 0.
                    Just give the scores. Return result only in JSON object.
                    Return a valid JSON object without any backticks around it.
                    No commentary. "))
  out_json <- jsonlite::fromJSON(out)
  vec <- unlist(out_json)
  # break ties
  if(any(duplicated(vec))) {
    dups <- vec[duplicated(vec)]
    out2 <- chat$chat(paste0(
      "Rank the sentiment scores for each level of the input: ",
      paste(lvls[vec %in% dups], collapse = ", "), ". ",
      "Positive connotations like satisfied or likely should have positive scores.
                    Negative connotations like unsatisfied or unlikely should have negative scores.
                    Satisfied should have a higher score than somewhat satisfied.
                    Agree should have a higher score than somewhat agree.
                    Neutral elements should have a score of 0.
                    Just give the scores. Return result only in JSON object.
                    Return a valid JSON object without any backticks around it.
                    No commentary.  ",
      ...
    ))
    out2_json <- jsonlite::fromJSON(out2)
    vec2 <- unlist(out2_json)
    vec2 <- vec2 / sum(vec2)
    vec[vec %in% dups] <- vec[vec %in% dups] + vec2
  }
  res <- names(sort(vec))
  if(length(vec) != length(lvls)) {
    cli::cli_warn("Could not reorder the levels meaningfully.")
    return(lvls)
  }
  if(!all(res %in% lvls)) {
    res <- names(sort(setNames(vec, lvls)))
  }
  res
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions