Skip to content

Commit 30e1bd2

Browse files
committed
use cli functions
1 parent 60999a6 commit 30e1bd2

24 files changed

+84
-104
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ Config/testthat/edition: 3
6464
Encoding: UTF-8
6565
LazyData: true
6666
Roxygen: list(markdown = TRUE)
67-
RoxygenNote: 7.3.1
67+
RoxygenNote: 7.3.2
6868
SystemRequirements: "GNU make"

R/aaa.R

+1-10
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@ factor_to_text <- function(data, names) {
1212

1313
check_possible_tokenizers <- function(x, dict, call = caller_env(2)) {
1414
if (!(x %in% dict)) {
15-
possible_tokenizers <- glue::glue_collapse(
16-
dict,
17-
sep = ", ", last = ", or "
18-
)
19-
rlang::abort(
20-
glue(
21-
"token should be one of the supported: {possible_tokenizers}"
22-
),
23-
call = call
24-
)
15+
cli::cli_abort("Token should be one of {dict}.", call = call)
2516
}
2617
}
2718

R/lda.R

+4-5
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,11 @@ check_lda_character <- function(dat) {
267267
all_good <- character_ind | factor_ind
268268

269269
if (any(all_good)) {
270-
rlang::abort(
271-
glue(
270+
cli::cli_abort(
271+
c(
272272
"All columns selected for this step should be tokenlists.",
273-
"\n",
274-
"See https://github.com/tidymodels/textrecipes#breaking-changes",
275-
" for more information."
273+
"i" = "See {.url https://github.com/tidymodels/textrecipes#breaking-changes}
274+
for more information."
276275
)
277276
)
278277
}

R/lemma.R

+4-6
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,10 @@ bake.step_lemma <- function(object, new_data, ...) {
115115
variable <- new_data[[col_name]]
116116

117117
if (is.null(maybe_get_lemma(variable))) {
118-
rlang::abort(
119-
glue(
120-
"`{col_name}` doesn't have a lemma attribute. ",
121-
"Make sure the tokenization step includes lemmatization."
122-
)
123-
)
118+
cli::cli_abort(c(
119+
"{.code {col_name}} doesn't have a lemma attribute.",
120+
"i" = "Make sure the tokenization step includes lemmatization."
121+
))
124122
} else {
125123
lemma_variable <- tokenlist_lemma(variable)
126124
}

R/pos_filter.R

+4-5
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,10 @@ bake.step_pos_filter <- function(object, new_data, ...) {
121121
variable <- new_data[[col_name]]
122122

123123
if (is.null(maybe_get_pos(variable))) {
124-
rlang::abort(
125-
glue(
126-
"`{col_name}` doesn't have a pos attribute. ",
127-
"Make sure the tokenization step includes ",
128-
"part of speech tagging."
124+
cli::cli_abort(
125+
c(
126+
"{.arg {col_name}} doesn't have a pos attribute.",
127+
"i" = "Make sure the tokenization step includes part of speech tagging."
129128
)
130129
)
131130
}

R/sequence_onehot.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ step_sequence_onehot <-
8585
skip = FALSE,
8686
id = rand_id("sequence_onehot")) {
8787
if (length(padding) != 1 || !(padding %in% c("pre", "post"))) {
88-
rlang::abort("`padding` should be one of: 'pre', 'post'")
88+
cli::cli_abort("{.arg padding} should be one of: {.val pre}, {.val post}")
8989
}
9090

9191
if (length(truncating) != 1 || !(truncating %in% c("pre", "post"))) {
92-
rlang::abort("`truncating` should be one of: 'pre', 'post'")
92+
cli::cli_abort("{.code truncating} should be {.val pre} or {.val post}.")
9393
}
9494

9595
add_step(

R/text_normalization.R

+5-7
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,13 @@ bake.step_text_normalization <- function(object, new_data, ...) {
127127
nfkd = stringi::stri_trans_nfkd,
128128
nfkc = stringi::stri_trans_nfkc,
129129
nfkc_casefold = stringi::stri_trans_nfkc_casefold,
130-
rlang::abort(
131-
glue(
132-
"'normalization_form' must be one of",
133-
"'nfc', 'nfd', 'nfkd', 'nfkc', or 'nfkc_casefold'",
134-
"but was {object$normalization_form}."
135-
)
130+
cli::cli_abort(
131+
"{.arg normalization_form} must be one of {.val nfc}, {.val nfd},
132+
{.val nfkd}, {.val nfkc}, or {.val nfkc_casefold} but was
133+
{.val {object$normalization_form}}."
136134
)
137135
)
138-
136+
139137
for (col_name in col_names) {
140138
new_data[[col_name]] <- normalization_fun(new_data[[col_name]])
141139
new_data[[col_name]] <- factor(new_data[[col_name]])

R/textfeature.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ validate_string2num <- function(fun) {
208208

209209
out <- fun(string)
210210
if (!(is.numeric(out) | is.logical(out))) {
211-
rlang::abort(paste0(deparse(substitute(fun)), " must return a numeric."))
211+
cli::cli_abort("Function {.fn {fun}} must return a numeric.")
212212
}
213213

214214
if (length(string) != length(out)) {
215-
rlang::abort(paste0(
216-
deparse(substitute(fun)),
217-
" must return the same length output as its input."
218-
))
215+
cli::cli_abort(
216+
"{.fn {deparse(substitute(fun))}} must return the same length output as
217+
its input."
218+
)
219219
}
220220
}
221221

R/tfidf.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ dtm_to_tfidf <- function(dtm, idf_weights, smooth_idf, norm, sublinear_tf) {
271271
dtm@x <- 1 + log(dtm@x)
272272
}
273273
if (is.character(idf_weights)) {
274-
rlang::warn(
274+
cli::cli_warn(
275275
c(
276276
"Please retrain this recipe with version 0.5.1 or higher.",
277-
"A data leakage bug has been fixed for `step_tfidf()`."
277+
"i" = "A data leakage bug has been fixed for {.fn step_tfidf}."
278278
)
279279
)
280280
idf_weights <- log(smooth_idf + nrow(dtm) / Matrix::colSums(dtm > 0))

R/tokenfilter.R

+6-8
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ step_tokenfilter <-
102102
id = rand_id("tokenfilter")) {
103103
if (percentage && (max_times > 1 | max_times < 0 |
104104
min_times > 1 | min_times < 0)) {
105-
rlang::abort(
106-
"`max_times` and `min_times` should be in the interval [0, 1]."
105+
cli::cli_abort(
106+
"{.arg max_times} and {.arg min_times} should be in the interval [0, 1]."
107107
)
108108
}
109-
109+
110110
add_step(
111111
recipe,
112112
step_tokenfilter_new(
@@ -258,11 +258,9 @@ tokenfilter_fun <- function(data, max_times, min_times, max_tokens,
258258
names(sort(tf[ids], decreasing = TRUE))
259259
} else {
260260
if (max_tokens > sum(ids)) {
261-
rlang::warn(
262-
glue(
263-
"max_tokens was set to '{max_tokens}', ",
264-
"but only {sum(ids)} was available and selected."
265-
)
261+
cli::cli_warn(
262+
"max_tokens was set to {.val {max_tokens}}, but only {sum(ids)} was
263+
available and selected."
266264
)
267265
max_tokens <- sum(ids)
268266
}

R/tokenize.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ tokenizer_switch <- function(name, object, data, call = caller_env()) {
454454
return(res)
455455
}
456456

457-
rlang::abort("`engine` argument is not valid.", call = call)
457+
cli::cli_abort("The {.arg engine} argument is not valid.", call = call)
458458
}
459459

460460
#' @rdname required_pkgs.step

R/tokenize_bpe.R

+6-7
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ prep.step_tokenize_bpe <- function(x, training, info = NULL, ...) {
121121

122122
bpe_options <- x$options
123123
if (!is.null(bpe_options$vocab_size)) {
124-
rlang::abort(
125-
"Please supply the vocabulary size using the `vocabulary_size` argument."
124+
cli::cli_abort(
125+
"Please supply the vocabulary size using the {.arg vocabulary_size}
126+
argument."
126127
)
127128
}
128129
bpe_options$vocab_size <- x$vocabulary_size
@@ -158,11 +159,9 @@ check_bpe_vocab_size <- function(text,
158159
text_count <- length(text_count)
159160

160161
if (vocabulary_size < text_count) {
161-
rlang::abort(
162-
glue(
163-
"`vocabulary_size` of {vocabulary_size} is too small for column ",
164-
"`{column}` which has a unique character count of {text_count}"
165-
),
162+
cli::cli_abort(
163+
"{.arg vocabulary_size} of {vocabulary_size} is too small for column
164+
{.arg {column}} which has a unique character count of {text_count}",
166165
call = call
167166
)
168167
}

R/tokenize_sentencepiece.R

+7-8
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ prep.step_tokenize_sentencepiece <- function(x, training, info = NULL, ...) {
120120

121121
sentencepiece_options <- x$options
122122
if (!is.null(sentencepiece_options$vocab_size)) {
123-
rlang::abort(
124-
"Please supply the vocabulary size using the `vocabulary_size` argument."
123+
cli::cli_abort(
124+
"Please supply the vocabulary size using the {.arg vocabulary_size}
125+
argument."
125126
)
126127
}
127128
sentencepiece_options$vocab_size <- x$vocabulary_size
@@ -160,12 +161,10 @@ check_sentencepiece_vocab_size <- function(text,
160161
text_count <- length(text_count)
161162

162163
if (vocabulary_size < text_count) {
163-
rlang::abort(
164-
glue(
165-
"`vocabulary_size` of {vocabulary_size} is too small for column ",
166-
"`{column}` which has a unique character count of {text_count}."
167-
),
168-
call = call
164+
cli::cli_abort(
165+
"The {.arg vocabulary_size} of {vocabulary_size} is too small for column {.arg {column}}
166+
which has a unique character count of {text_count}.",
167+
call = call
169168
)
170169
}
171170
}

R/tokenlist.R

+11-11
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ new_tokenlist <- function(tokens = list(), lemma = NULL, pos = NULL,
4444
unique_tokens = character()) {
4545
vec_assert(tokens, list())
4646
if (!(is.null(lemma) | is.list(lemma))) {
47-
rlang::abort("`lemma` must be NULL or a list.")
47+
cli::cli_abort("{.arg lemma} must be NULL or a list.")
4848
}
4949
if (!(is.null(pos) | is.list(pos))) {
50-
rlang::abort("`pos` must be NULL or a list.")
50+
cli::cli_abort("{.arg pos} must be NULL or a list.")
5151
}
5252
vec_assert(unique_tokens, character())
5353

@@ -141,7 +141,7 @@ obj_print_footer.textrecipes_tokenlist <- function(x, ...) {
141141
# or removes (for keep = FALSE) the words
142142
tokenlist_filter <- function(x, dict, keep = FALSE) {
143143
if (!is_tokenlist(x)) {
144-
rlang::abort("Input must be a tokenlist.")
144+
cli::cli_abort("Input must be a tokenlist.")
145145
}
146146

147147
if (!keep) {
@@ -180,7 +180,7 @@ tokenlist_filter <- function(x, dict, keep = FALSE) {
180180

181181
tokenlist_filter_function <- function(x, fn) {
182182
if (!is_tokenlist(x)) {
183-
rlang::abort("Input must be a tokenlist.")
183+
cli::cli_abort("Input must be a {.cls tokenlist}.")
184184
}
185185

186186
tokens <- get_tokens(x)
@@ -210,7 +210,7 @@ tokenlist_filter_function <- function(x, fn) {
210210

211211
tokenlist_apply <- function(x, fun, arguments = NULL) {
212212
if (!is_tokenlist(x)) {
213-
rlang::abort("Input must be a tokenlist.")
213+
cli::cli_abort("Input must be {.cls tokenlist} object.")
214214
}
215215

216216
tokens <- get_tokens(x)
@@ -226,7 +226,7 @@ tokenlist_apply <- function(x, fun, arguments = NULL) {
226226
# Takes a [tokenlist] and calculate the token count matrix
227227
tokenlist_to_dtm <- function(x, dict) {
228228
if (!is_tokenlist(x)) {
229-
rlang::abort("Input must be a tokenlist.")
229+
cli::cli_abort("Input must be a tokenlist.")
230230
}
231231

232232
tokens <- get_tokens(x)
@@ -246,23 +246,23 @@ tokenlist_to_dtm <- function(x, dict) {
246246

247247
tokenlist_lemma <- function(x) {
248248
if (!is_tokenlist(x)) {
249-
rlang::abort("Input must be a tokenlist.")
249+
cli::cli_abort("Input must be a tokenlist.")
250250
}
251251

252252
if (is.null(maybe_get_lemma(x))) {
253-
rlang::abort("`lemma` attribute not avaliable.")
253+
cli::cli_abort("The {.code lemma} attribute is not available.")
254254
}
255255

256256
tokenlist(maybe_get_lemma(x), pos = maybe_get_pos(x))
257257
}
258258

259259
tokenlist_pos_filter <- function(x, pos_tags) {
260260
if (!is_tokenlist(x)) {
261-
rlang::abort("Input must be a tokenlist.")
261+
cli::cli_abort("Input must be a tokenlist.")
262262
}
263263

264264
if (is.null(maybe_get_pos(x))) {
265-
rlang::abort("pos attribute not avaliable.")
265+
cli::cli_abort("{.arg pos} attribute not available.")
266266
}
267267

268268
tokens <- get_tokens(x)
@@ -292,7 +292,7 @@ tokenlist_pos_filter <- function(x, pos_tags) {
292292

293293
tokenlist_ngram <- function(x, n, n_min, delim) {
294294
if (!is_tokenlist(x)) {
295-
rlang::abort("Input must be a tokenlist.")
295+
cli::cli_abort("Input must be a tokenlist.")
296296
}
297297

298298
tokenlist(cpp11_ngram(get_tokens(x), n, n_min, delim))

R/word_embeddings.R

+4-7
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,13 @@ step_word_embeddings <- function(recipe,
110110
ncol(embeddings) == 1 ||
111111
!all(map_lgl(embeddings[, 2:ncol(embeddings)], is.numeric))
112112
) {
113-
embeddings_message <- glue(
114-
"embeddings should be a tibble with 1 character or factor column and ",
115-
"additional numeric columns."
116-
)
117-
rlang::abort(
118-
embeddings_message,
113+
cli::cli_abort(
114+
"embeddings should be a tibble with {.code 1} character or factor column
115+
and additional numeric columns.",
119116
class = "bad_embeddings"
120117
)
121118
}
122-
119+
123120
aggregation <- match.arg(aggregation)
124121

125122
add_step(

src/ngram.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ cpp11_ngram(cpp11::list_of<cpp11::strings> x,
101101
}
102102

103103
return(out);
104-
}
104+
}

tests/testthat/_snaps/lemma.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
Condition
66
Error in `step_lemma()`:
77
Caused by error in `bake()`:
8-
! `text` doesn't have a lemma attribute. Make sure the tokenization step includes lemmatization.
8+
! `text` doesn't have a lemma attribute.
9+
i Make sure the tokenization step includes lemmatization.
910

1011
# bake method errors when needed non-standard role columns are missing
1112

tests/testthat/_snaps/pos_filter.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
Condition
66
Error in `step_pos_filter()`:
77
Caused by error in `bake()`:
8-
! `text` doesn't have a pos attribute. Make sure the tokenization step includes part of speech tagging.
8+
! `text` doesn't have a pos attribute.
9+
i Make sure the tokenization step includes part of speech tagging.
910

1011
# bake method errors when needed non-standard role columns are missing
1112

0 commit comments

Comments
 (0)