plu (version 0.1.1)

plu_ral: Pluralize a phrase based on the length of a vector

Description

Pluralize a phrase based on the length of a vector

Usage

plu_ral(
  x,
  vector = integer(2),
  n_fn = NULL,
  ...,
  n = length(vector),
  pl = abs(n) != 1,
  irregulars = c("moderate", "conservative", "liberal", "none"),
  replace_n = TRUE
)

ral( x, vector = integer(2), n_fn = NULL, ..., n = length(vector), pl = abs(n) != 1, irregulars = c("moderate", "conservative", "liberal", "none"), replace_n = TRUE )

Arguments

x

An English word or phrase to be pluralized. See details for special sequences which are handled differently.

vector

A vector whose length determines n. Defaults to length 2.

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(n). 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. 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.

Value

The character vector x altered to match the number of n

Details

Certain strings in x are treated specially.

  • 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 braces separated by a pipe will be treated as a custom plural ("{a|some} word" to "a word", "some words").

    • Three strings separated by pipes will be treated as a singular, dual, and plural form ("{the|both|all} word" to "the word" (1), "both words" (2), "all words" (3+)).

  • Any other string between braces will be treated as invariant ("attorney {general}" to "attorneys general").

See Also

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

Examples

Run this code
# NOT RUN {
plu::ral("apple", pl = FALSE)
plu::ral("apple", pl = TRUE)

plu::ral("apple", n = 1)
plu::ral("apple", n = 2)
plu::ral("apple", n = 0)
plu::ral("apple", n = -1)
plu::ral("apple", n = 0.5)

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

plu::ral("apple", mon)
plu::ral("pear", tue)

paste("Monday, the caterpillar ate", plu::ral("an apple", mon))
paste("Tuesday, the caterpillar ate", plu::ral("a pear", tue))

paste("Monday, the caterpillar visited", plu::ral("an {apple} tree", mon))
paste("Tuesday, the caterpillar visited", plu::ral("a {pear} tree", tue))

paste("Monday, the caterpillar ate", plu::ral("a {single|multiple} apple", mon))
paste("Tuesday, the caterpillar ate", plu::ral("a {single|multiple} pear", tue))

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))
paste("The caterpillar ate", plu::ral("{the|both|all of the} pear", tue))
paste("The caterpillar ate", plu::ral("{the|both|all of the} delicacy", later))

paste("The caterpillar ate", plu::ral("n apple", mon))
paste("The caterpillar ate", plu::ral("n delicacy", later))

paste("The caterpillar ate", plu::ral("n apple", mon, nombre::cardinal))
paste("The caterpillar ate", plu::ral("n delicacy", later, nombre::cardinal))
# }

Run the code above in your browser using DataCamp Workspace