Skip to content

Commit

Permalink
Merge branch 'main' into bahadzie/issue7
Browse files Browse the repository at this point in the history
  • Loading branch information
bahadzie authored Apr 8, 2024
2 parents 4a034e3 + 6645e34 commit a196dc6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
19 changes: 16 additions & 3 deletions R/numberize.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,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
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +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")
#> [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"
)
#> [1] 10 65 2024 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 @@ -105,3 +105,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))
})

0 comments on commit a196dc6

Please sign in to comment.