Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

account for the presence of numeric values in a vector of characters #9

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions R/numberize.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,24 @@ number_from <- function(digits) {
#'
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.numberize <- function(text, lang = c("en", "fr", "es")) {
digits <- digits_from(text, lang)
if (anyNA(digits)) {
# return NA if the input is NA
if (is.na(text)) {
return(NA)
}
number_from(digits)

# convert to numeric. Numeric values will pass and non numeric values will be
# coerced to NA and converted into numbers.
tmp_text <- suppressWarnings(as.numeric(text))
if (!is.na(tmp_text)) {
return(tmp_text)
} else {
# when the text does not correspond to a number, digits_from() returns NA
digits <- digits_from(text, lang)
if (anyNA(digits)) {
return(NA)
}
number_from(digits)
}
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ numberize("veintiuno", lang = "es")

# convert a vector of written values
numberize(
text = c("dix", "soixante-cinq", "deux mille vingt-quatre", NA),
text = c(17, "dix", "soixante-cinq", "deux mille vingt-quatre", NA),
lang = "fr"
)
```
Expand Down
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,28 @@ These examples illustrate the current functionality.
``` r
# numberize a French string
numberize("zéro", lang = "fr")
#> zéro
#> 0
#> [1] 0

# numberize a Spanish string
numberize("Siete mil quinientos cuarenta y cinco", lang = "es")
#> Siete mil quinientos cuarenta y cinco
#> 7545
#> [1] 7545

# numberize the English string "nine hundred and ninety-nine trillion, nine hundred and ninety-nine billion, nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine" # nolint: line_length_linter.
formatC(numberize("nine hundred and ninety-nine trillion, nine hundred and ninety-nine billion, nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine"), big.mark = ",", format = "fg") # nolint: line_length_linter.
#> nine hundred and ninety-nine trillion, nine hundred and ninety-nine billion, nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine
#> "999,999,999,999,999"
#> [1] "999,999,999,999,999"

# some edge cases
numberize("veintiún", lang = "es")
#> veintiún
#> 21
#> [1] 21
numberize("veintiuno", lang = "es")
#> veintiuno
#> 21
#> [1] 21

# convert a vector of written values
numberize(
text = c("dix", "soixante-cinq", "deux mille vingt-quatre", NA),
text = c(17, "dix", "soixante-cinq", "deux mille vingt-quatre", NA),
lang = "fr"
)
#> dix soixante-cinq deux mille vingt-quatre
#> 10 65 2024
#> <NA>
#> NA
#> [1] 17 10 65 2024 NA
```

## Related packages and Limitations
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-numberize.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ test_that("non digit word returns NA", {
res <- numberize("epiverse", lang = "en")
expect_true(is.na(res))
})

test_that("vector with number and words and NA is properly handled", {
res <- numberize(
c(17, "dix", "soixante-cinq", "deux mille vingt-quatre", NA),
lang = "fr"
)
expect_identical(res, c(17, 10, 65, 2024, NA))
})
Loading