Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rossellhayes/nombre

Number names in R 2️⃣💬
https://github.com/rossellhayes/nombre

natural-language r rstats

Last synced: 11 days ago
JSON representation

Number names in R 2️⃣💬

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)

library(nombre)
library(ipa)
# remotes::install_github("GuangchuangYu/badger")
library(badger)
```

# nombre

`r badge_cran_release(color = "brightgreen")`
`r badge_lifecycle("stable")`
`r badge_license(color = "blueviolet")`
`r badge_github_actions(action = "R-CMD-check")`
`r badge_codecov()`
`r badge_dependencies()`

> *nombre* (French) `r sampa("/nO~bR/")`: number
> *nombre* (Spanish) `r sampa("/\"nom.b4e/")`: name
> `nombre`: package to convert numbers to their names in R

**nombre** converts numeric vectors to character vectors of English words.
You can use it to express numbers as cardinals (one, two, three) or ordinals (first, second, third), as well as numerators and denominators. **nombre** supports not just whole numbers, but also negatives, fractions, and ratios.

## Installation

You can install the released version of **nombre** from [CRAN](https://CRAN.R-project.org) with:

``` {r eval = FALSE}
install.packages("nombre")
```

or the development version from GitHub with:

``` {r eval = FALSE}
# install.packages("remotes")
remotes::install_github("rossellhayes/nombre")
```

## Usage

```{r include = FALSE}
library(nombre)
```

**nombre** converts numerics into words:

```{r}
nom_card(2)
nom_card(2L)
x <- rep(TRUE, 525600)
nom_card(length(x))
```

It also works for numeric vectors:

```{r}
nom_card(8^(1:10))
```

**nombre** can also generate ordinals, adverbials, collectives, numerators and denominators:

```{r}
nom_ord(1:5)
nom_adv(1:5)
nom_coll(1:5)
nom_numer(1:5)
nom_denom(1:5)
nom_denom(1:5, numerator = 1:5)
```

> `r emo::ji("shushing")` (numerators are almost always the same as cardinals)

You can also add ordinal suffixes to numerics or arbitrary number-like strings:

```{r}
nom_ord(1:5, cardinal = FALSE)
nom_ord(c("n", "dozen", "umpteen", "eleventy", "one zillion"))
```

It can also handle less common numerics, like negatives, fractions, and ratios:

```{r}
nom_card(-2)
nom_card(9.75)
nom_ratio(0.25)
nom_ratio(3)
```

### Math with nombres

**nombre** implements an S3 class that seamlessly decides when to treat nombres like characters and when to treat them like numerics.

```{r}
x <- nom_card(25)
x
x + 2
sqrt(x)
x < 30
x == "twenty-five"
```

### Reverse it

`uncardinal()` attempts to convert character vectors of cardinal number names to numerics.

```{r}
uncardinal(c("twenty-five", "negative three", "infinity"))
```

## Advantages `r emo::ji("rocket")`

**nombre** is implemented using vectorized base R and runs faster than alternatives like [**english**](https://CRAN.R-project.org/package=english):

```{r}
bench::mark(as.character(nom_card(1:1000)), as.character(english::english(1:1000)))
```

---

Hex sticker image adapted from artwork by
@[allison_horst](https://github.com/allisonhorst/stats-illustrations).

Hex sticker fonts are
[Source Sans by Adobe](https://github.com/adobe-fonts/source-sans) and
[Permanent Marker](https://www.fontsquirrel.com/fonts/permanent-marker)
by [Font Diner](https://fontdiner.com/).

Please note that **nombre** is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.