Pluralize a phrase based on the length of a vector

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

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

Arguments

x

An character vector 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_fn

A function to apply to the output of the special sequence "n". See examples. Defaults to identity, which returns n unchanged.

...

Additional arguments passed to the function n_fn.

n

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

pl

A logical value 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 "}".

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.

    • This output can be modified with n_fn.

  • 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"
later <- c( rep("plum", 3), rep("strawberry", 4), rep("orange", 5), "chocolate cake", "ice-cream cone", "pickle", "Swiss cheese", "salami", "lollipop", "cherry pie", "sausage", "cupcake", "watermelon" ) 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", later))
#> [1] "The caterpillar ate all of the delicacies"
paste("The caterpillar ate", plu::ral("n apple", mon))
#> [1] "The caterpillar ate 1 apple"
paste("The caterpillar ate", plu::ral("n delicacy", later))
#> [1] "The caterpillar ate 22 delicacies"
paste("The caterpillar ate", plu::ral("n apple", mon, nombre::cardinal))
#> [1] "The caterpillar ate one apple"
paste("The caterpillar ate", plu::ral("n delicacy", later, nombre::cardinal))
#> [1] "The caterpillar ate twenty-two 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"