numberize

License: MIT R-CMD-check Codecov test coverage lifecycle-stable

numberize is an R package to convert numbers written as English, French or Spanish words from "zero" to "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" to positive integer values.

numberize is developed at the Centre for the Mathematical Modelling of Infectious Diseases at the London School of Hygiene and Tropical Medicine as part of the Epiverse-TRACE program.

Installation

The package can be installed from CRAN using

install.packages("numberize")

Development version

You can install the development version of numberize from GitHub with:

pak::pak("epiverse-trace/numberize")

Example

These examples illustrate the current functionality.

library("numberize")

# numberize a French string
numberize("zéro", lang = "fr")
#> [1] 0

# numberize a Spanish string
numberize("Siete mil quinientos cuarenta y cinco", lang = "es")
#> [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.
#> [1] "999,999,999,999,999"

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

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

numberize is released as a standalone package in the hope that it will be useful to the R community at large. numberize was created in response to data cleaning requirements in {cleanepi}.

Lifecycle

This package is currently stable, as defined by the RECON software lifecycle.

Contributions

Contributions are welcome via pull requests.

Code of Conduct

Please note that the numberize project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.