From 8b638e83b0cfccb5df1fd7d3eddd6b550df4e2ca Mon Sep 17 00:00:00 2001 From: James Hollway Date: Wed, 21 Aug 2024 16:25:53 +0200 Subject: [PATCH 1/2] Added start of sim_urgency() --- NAMESPACE | 1 + R/urgency.R | 19 +++++++++++++++++++ man/sim_urgency.Rd | 14 ++++++++++++++ poldis.Rproj | 1 + 4 files changed, 35 insertions(+) create mode 100644 man/sim_urgency.Rd diff --git a/NAMESPACE b/NAMESPACE index 118d3f02..b3c31bd4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export(gather_topics) export(get_urgency) export(read_pdf) export(select_priorities) +export(sim_urgency) export(split_text) import(dplyr) import(quanteda) diff --git a/R/urgency.R b/R/urgency.R index dffc43b5..8ab762e8 100644 --- a/R/urgency.R +++ b/R/urgency.R @@ -133,3 +133,22 @@ get_urgency <- function(.data, normalize = "tokens") { stringr::str_count(as.character(v), paste0("\\b", commitment_words$terms[i], "\\b"))* commitment_words$score_commitment_scaled[i]))) } + +#' Simulating urgency +#' +#' @examples +#' sim_urgency() +#' @export +sim_urgency <- function(urgency, + commitment, intensity, timing, frequency, + pronoun = "We"){ + if(!missing(commitment)){ + commit <- comm$word[which.min(abs(abs(commitment) - comm$Rescaled))] + if(commitment<0) commit <- paste(commit, sample(c("not","never"),1)) + if(!missing(intensity)){ + intensifier <- int$word[which.min(abs(abs(intensity) - int$Rescaled))] + out <- paste(pronoun, intensifier, commit, "do this.") + } else out <- paste(pronoun, commit, "do this.") + } else out <- paste(pronoun, "do this.") + out +} diff --git a/man/sim_urgency.Rd b/man/sim_urgency.Rd new file mode 100644 index 00000000..b44ab659 --- /dev/null +++ b/man/sim_urgency.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/urgency.R +\name{sim_urgency} +\alias{sim_urgency} +\title{Simulating urgency} +\usage{ +sim_urgency(urgency, commitment, intensity, timing, frequency, pronoun = "We") +} +\description{ +Simulating urgency +} +\examples{ +sim_urgency() +} diff --git a/poldis.Rproj b/poldis.Rproj index 497f8bfc..270314b8 100644 --- a/poldis.Rproj +++ b/poldis.Rproj @@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace From e18d9eb5e7e4e90406e4b121ebd37f140abf7e94 Mon Sep 17 00:00:00 2001 From: James Hollway Date: Wed, 21 Aug 2024 17:17:38 +0200 Subject: [PATCH 2/2] sim_urgency() now accepts an urgency score and finds a fitting combination of commitment and intensity to fit --- R/urgency.R | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/R/urgency.R b/R/urgency.R index 8ab762e8..13096115 100644 --- a/R/urgency.R +++ b/R/urgency.R @@ -142,7 +142,16 @@ get_urgency <- function(.data, normalize = "tokens") { sim_urgency <- function(urgency, commitment, intensity, timing, frequency, pronoun = "We"){ - if(!missing(commitment)){ + if(!missing(urgency)){ + combins <- expand.grid(c("",int$word), comm$word, stringsAsFactors = FALSE) + combins <- merge(combins, int, by.x = "Var1", by.y = "word") + combins <- combins[,c("Var1","Var2","Rescaled")] + combins <- merge(combins, comm, by.x = "Var2", by.y = "word") + combins <- combins[,c("Var1","Var2","Rescaled.x","Rescaled.y")] + combins$combo <- combins$Rescaled.x * combins$Rescaled.y + intcom <- combins[which.min(abs(urgency - combins$combo)),c("Var1","Var2")] + out <- paste(pronoun, paste(intcom, collapse = " "), "do this.") + } else if(!missing(commitment)){ commit <- comm$word[which.min(abs(abs(commitment) - comm$Rescaled))] if(commitment<0) commit <- paste(commit, sample(c("not","never"),1)) if(!missing(intensity)){