Pluralize a phrase based on the length of a vector

plu_ral(
  x,
  vector = NULL,
  n = NULL,
  pl = NULL,
  irregulars = c("moderate", "conservative", "liberal", "none"),
  replace_n = TRUE,
  open = "{",
  close = "}",
  n_fn = lifecycle::deprecated(),
  ...
)

ral(
  x,
  vector = NULL,
  n = NULL,
  pl = NULL,
  irregulars = c("moderate", "conservative", "liberal", "none"),
  replace_n = TRUE,
  open = "{",
  close = "}",
  n_fn = lifecycle::deprecated(),
  ...
)

Arguments

x

A character vector (or vector that can be coerced to character) of English words or phrase to be pluralized. See details for special sequences which are handled differently.

vector

A vector whose length determines n. Defaults to NULL.

n

A numeric vector which will determine the plurality of x. Defaults to length(vector). If specified, overrides vector.

pl

A logical vector indicating whether to use the plural form (if TRUE) or the singular form (if FALSE) of x. Defaults to FALSE when n is 1 or -1 and TRUE for all other values. If specified, overrides n.

irregulars

What level of irregularity to use in pluralization. "moderate" uses the most common pluralization. "conservative" uses the most common irregular plural if one exists, even if a regular plural is more common. "liberal" uses a regular plural if it exists, even if an irregular plural is more common. "none" attempts to apply regular noun pluralization rules to all words. See section "Irregular plurals" for more details. Defaults to "moderate". The default can be changed by setting options(plu.irregulars). See examples in ralize() for more details.

replace_n

A logical indicating whether to use special handling for "n". See details. Defaults to TRUE.

open, close

The opening and closing delimiters for special strings. See section "Special strings". Defaults to "{" and "}".

n_fn

[Deprecated]

...

[Deprecated]

Value

The character vector x altered to match the number of n

Irregular plurals

Many words in English have both regular and irregular plural forms. For example, the word "person" can be pluralized as "persons" or "people", and the word "formula" can be pluralized as "formulas" or "formulae". plu offers several options for how to handle words with multiple plurals.

  • The moderate list attempts to apply the most common pluralization, whether it is regular or irregular. This chooses the irregular plural "people" but the regular plural "formulas".

  • The conservative list attempts to apply an irregular plural to every word that has one. This chooses "people" and "formulae", but still uses regular plurals for words that have no irregular plural form.

  • The liberal list attempts to apply a regular plural to every word that has one. This chooses "persons" and "formulas", but still uses irregular plurals for words that have no common regular plural, like "women". Many words in English have invariant plurals that look exactly the same as their singular forms, like "fish" or "deer". The liberal list attempts to use regular plurals for these words, producing "fishes" and "deers".

  • The none list applies regular pluralization rules to all words, even those with no common regular plural. This produces, for example, "womans" as a plural for "woman" even though this is not a common English word.

Special strings

Certain strings in x receive special treatment.

  • By default, "a" and "an" are deleted in the plural ("a word" to "words").

  • The string "n" will be replaced with the length of vector or the number in n.

  • Strings between open and close separated by a pipe will be treated as a custom plural ("{a|some} word" to "a word", "some words").

    • More than two strings separated by pipes will be treated as singular, dual, trial, ... and plural forms. For example, "{the|both|all} word" to "the word" (1), "both words" (2), "all words" (3+).

    • See examples for more.

  • Any other string between open and close without a pipe will be treated as invariant. For example, "attorney {general}" to "attorneys general" (notice "general" is not pluralized).

See also

plu_ralize() to convert an English word to its plural form.

Examples

plu::ral("apple", pl = FALSE)
#> [1] "apple"
plu::ral("apple", pl = TRUE)
#> [1] "apples"

plu::ral("apple", n = 1)
#> [1] "apple"
plu::ral("apple", n = 2)
#> [1] "apples"
plu::ral("apple", n = 0)
#> [1] "apples"
plu::ral("apple", n = -1)
#> [1] "apple"
plu::ral("apple", n = 0.5)
#> [1] "apples"

mon <- c("apple")
tue <- c("pear", "pear")

plu::ral("apple", mon)
#> [1] "apple"
plu::ral("pear", tue)
#> [1] "pears"

paste("Monday, the caterpillar ate", plu::ral("an apple", mon))
#> [1] "Monday, the caterpillar ate an apple"
paste("Tuesday, the caterpillar ate", plu::ral("a pear", tue))
#> [1] "Tuesday, the caterpillar ate pears"

paste("Monday, the caterpillar visited", plu::ral("an {apple} tree", mon))
#> [1] "Monday, the caterpillar visited an apple tree"
paste("Tuesday, the caterpillar visited", plu::ral("a {pear} tree", tue))
#> [1] "Tuesday, the caterpillar visited pear trees"

paste("Monday, the caterpillar ate", plu::ral("a {single|multiple} apple", mon))
#> [1] "Monday, the caterpillar ate a single apple"
paste("Tuesday, the caterpillar ate", plu::ral("a {single|multiple} pear", tue))
#> [1] "Tuesday, the caterpillar ate multiple pears"

# Vectorized `n`
foods <- c("apple", "pear", "plum", "strawberry", "orange")
quantities <- c(1, 2, 3, 4, 5)
plu::ral(foods, n = quantities)
#> [1] "apple"        "pears"        "plums"        "strawberries" "oranges"     
paste(
  "The caterpillar ate",
  and::and(paste(nombre::cardinal(quantities), plu::ral(foods, n = quantities)))
)
#> [1] "The caterpillar ate one apple, two pears, three plums, four strawberries, and five oranges"

# Some words have a dual form, a specific form for quantities of two
paste("The caterpillar ate", plu::ral("{the|both|all of the} apple", mon))
#> [1] "The caterpillar ate the apple"
paste("The caterpillar ate", plu::ral("{the|both|all of the} pear", tue))
#> [1] "The caterpillar ate both pears"
paste("The caterpillar ate", plu::ral("{the|both|all of the} delicacy", foods))
#> [1] "The caterpillar ate all of the delicacies"

# The string "n" will be replaced by the number used for pluralization
paste("The caterpillar ate", plu::ral("n apple", mon))
#> [1] "The caterpillar ate 1 apple"
paste("The caterpillar ate", plu::ral("n delicacy", foods))
#> [1] "The caterpillar ate 5 delicacies"

# Special brace strings
plu::ral("{one|two}", n = 1)
#> [1] "one"
plu::ral("{one|two}", n = 2)
#> [1] "two"

plu::ral("{one|two|more}", n = 1)
#> [1] "one"
plu::ral("{one|two|more}", n = 2)
#> [1] "two"
plu::ral("{one|two|more}", n = 3)
#> [1] "more"
plu::ral("{one|two|more}", n = 50)
#> [1] "more"

plu::ral("{one|two|three|more}", n = 1)
#> [1] "one"
plu::ral("{one|two|three|more}", n = 2)
#> [1] "two"
plu::ral("{one|two|three|more}", n = 3)
#> [1] "three"
plu::ral("{one|two|three|more}", n = 50)
#> [1] "more"
plu::ral("{one|two|three|more}", n = 0)
#> [1] "more"
plu::ral("{one|two|three|more}", n = 1.5)
#> [1] "more"