Title: | Dynamically Pluralize Phrases |
---|---|
Description: | Converts English phrases to singular or plural form based on the length of an associated vector. Contains helper functions to create natural language lists from vectors and to include the length of a vector in natural language. |
Authors: | Alexander Rossell Hayes [aut, cre, cph] |
Maintainer: | Alexander Rossell Hayes <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.0 |
Built: | 2024-09-17 03:26:37 UTC |
Source: | https://github.com/rossellhayes/plu |
capitalize()
returns a character vector x
with the first
alphabetic character replaced with a capital form (if one exists).
capitalize(x) plu_capitalize(x) is_capital(x, strict = FALSE) is_capitalized(x, strict = FALSE)
capitalize(x) plu_capitalize(x) is_capital(x, strict = FALSE) is_capitalized(x, strict = FALSE)
x |
A character vector. |
strict |
If strict is |
is_capital()
returns TRUE
if all characters are capital, FALSE
if
all characters are lowercase, and NA
if characters are mixed case or any
characters are caseless (e.g. numbers, punctuation marks, characters from a
unicase language like Arabic, Chinese or Hindi).
is_capitalized()
returns TRUE
if the first alphabetic character in a
string is capital, FALSE
if the first alphabetic character is lowercase,
and NA
if there are no alphabetic characters.
capitalize()
returns a character vector of the same length as x
.
is_capital()
and is_capitalized()
return a logical vector of the same
length as x
.
capitalize(c("word", "a whole phrase")) capitalize("preserving MIXED Case") capitalize("... word") is_capital(c("a", "A", "!")) is_capital(c("aa", "AA", "!!")) is_capital("Aa") is_capitalized(c("a word", "A word", "a Word")) is_capitalized("... A word") is_capitalized("...")
capitalize(c("word", "a whole phrase")) capitalize("preserving MIXED Case") capitalize("... word") is_capital(c("a", "A", "!")) is_capital(c("aa", "AA", "!!")) is_capital("Aa") is_capitalized(c("a word", "A word", "a Word")) is_capitalized("... A word") is_capitalized("...")
Find a function
get_fun(fn, default = identity)
get_fun(fn, default = identity)
fn |
A function name, either a character string or an unquoted function name, with or without colons. |
default |
If |
A function
get_fun(plu_ral) get_fun(plu::ral) get_fun("plu_ral") get_fun("plu::ral") get_fun(NULL) get_fun(NULL, default = plu_ral)
get_fun(plu_ral) get_fun(plu::ral) get_fun("plu_ral") get_fun("plu::ral") get_fun(NULL) get_fun(NULL, default = plu_ral)
Informatively display a maximum number of elements
plu_more(x, max = 5, type = TRUE, fn = NULL, ..., det = "more") more(x, max = 5, type = TRUE, fn = NULL, ..., det = "more")
plu_more(x, max = 5, type = TRUE, fn = NULL, ..., det = "more") more(x, max = 5, type = TRUE, fn = NULL, ..., det = "more")
x |
A vector or list. |
max |
The maximum number of items to list.
Additional arguments are replaced with "n more".
Defaults to |
type |
|
fn |
A function to apply to the number of additional elements.
Default to |
... |
Additional arguments to |
det |
A determiner to place before the number of additional elements. Defaults to "more". |
If x
is a vector, a character vector with a length of max
+ 1
or less.
If x
is a list, a list with max + 1
or fewer elements.
plu::more(letters) # Setting `max` plu::more(letters, max = 10) plu::more(letters, max = 27) # If `max` is Inf or NULL, all elements will be preserved plu::more(letters, max = Inf) # If `max` is less than one, no elements will be preserved plu::more(letters, max = 0) # Setting element type plu::more(letters, type = "letter") # If `type` is FALSE or NULL, no type will be included plu::more(letters, type = FALSE) # Automatically generating type plu::more(1:100) plu::more(as.list(1:100)) plu::more(c(as.list(1:2), as.list(letters))) plu::more(fracture::fracture((1:9) / (9:1))) # Setting a determiner other than "more" plu::more(letters, det = "other") # Applying a function to the number plu::more(letters, fn = nombre::cardinal) # Automatic pluralization of type fish <- c("sea bass", "crucian carp", "dace", "coelecanth") plu::more(fish, max = 3, type = "fish") plu::more(fish, max = 2, type = "fish") teeth <- c("incisor", "canine", "molar", "wisdom tooth") plu::more(teeth, max = 3, type = "tooth") plu::more(teeth, max = 2, type = "tooth") cacti <- c("saguaro", "prickly pear", "barrel", "star") plu::more(cacti, max = 3, type = "cactus") plu::more(cacti, max = 2, type = "cactus") # Using plu_more() within a function verbose_sqrt <- function(x) { if (any(x < 0)) { problems <- x[x < 0] prob_msg <- crayon::silver(encodeString(problems, quote = "`")) warning( "Square root is undefined for ", and::and(plu::more(prob_msg, fn = crayon::silver, type = "input.")), call. = FALSE ) } sqrt(x) } ints <- round(runif(20, -10, 10)) verbose_sqrt(ints)
plu::more(letters) # Setting `max` plu::more(letters, max = 10) plu::more(letters, max = 27) # If `max` is Inf or NULL, all elements will be preserved plu::more(letters, max = Inf) # If `max` is less than one, no elements will be preserved plu::more(letters, max = 0) # Setting element type plu::more(letters, type = "letter") # If `type` is FALSE or NULL, no type will be included plu::more(letters, type = FALSE) # Automatically generating type plu::more(1:100) plu::more(as.list(1:100)) plu::more(c(as.list(1:2), as.list(letters))) plu::more(fracture::fracture((1:9) / (9:1))) # Setting a determiner other than "more" plu::more(letters, det = "other") # Applying a function to the number plu::more(letters, fn = nombre::cardinal) # Automatic pluralization of type fish <- c("sea bass", "crucian carp", "dace", "coelecanth") plu::more(fish, max = 3, type = "fish") plu::more(fish, max = 2, type = "fish") teeth <- c("incisor", "canine", "molar", "wisdom tooth") plu::more(teeth, max = 3, type = "tooth") plu::more(teeth, max = 2, type = "tooth") cacti <- c("saguaro", "prickly pear", "barrel", "star") plu::more(cacti, max = 3, type = "cactus") plu::more(cacti, max = 2, type = "cactus") # Using plu_more() within a function verbose_sqrt <- function(x) { if (any(x < 0)) { problems <- x[x < 0] prob_msg <- crayon::silver(encodeString(problems, quote = "`")) warning( "Square root is undefined for ", and::and(plu::more(prob_msg, fn = crayon::silver, type = "input.")), call. = FALSE ) } sqrt(x) } ints <- round(runif(20, -10, 10)) verbose_sqrt(ints)
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(), ... )
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(), ... )
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 |
A numeric vector which will determine the plurality of |
pl |
A logical vector indicating whether to use the plural form (if
|
irregulars |
What level of irregularity to use in pluralization.
|
replace_n |
A logical indicating whether to use special handling for |
open , close
|
The opening and closing delimiters for special strings.
See section "Special strings". Defaults to |
n_fn |
|
... |
The character vector x
altered to match the number of n
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.
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).
plu_ralize()
to convert an English word to its
plural form.
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)) # Vectorized `n` foods <- c("apple", "pear", "plum", "strawberry", "orange") quantities <- c(1, 2, 3, 4, 5) plu::ral(foods, n = quantities) paste( "The caterpillar ate", and::and(paste(nombre::cardinal(quantities), plu::ral(foods, n = quantities))) ) # 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)) 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", foods)) # The string "n" will be replaced by the number used for pluralization paste("The caterpillar ate", plu::ral("n apple", mon)) paste("The caterpillar ate", plu::ral("n delicacy", foods)) # Special brace strings plu::ral("{one|two}", n = 1) plu::ral("{one|two}", n = 2) plu::ral("{one|two|more}", n = 1) plu::ral("{one|two|more}", n = 2) plu::ral("{one|two|more}", n = 3) plu::ral("{one|two|more}", n = 50) plu::ral("{one|two|three|more}", n = 1) plu::ral("{one|two|three|more}", n = 2) plu::ral("{one|two|three|more}", n = 3) plu::ral("{one|two|three|more}", n = 50) plu::ral("{one|two|three|more}", n = 0) plu::ral("{one|two|three|more}", n = 1.5)
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)) # Vectorized `n` foods <- c("apple", "pear", "plum", "strawberry", "orange") quantities <- c(1, 2, 3, 4, 5) plu::ral(foods, n = quantities) paste( "The caterpillar ate", and::and(paste(nombre::cardinal(quantities), plu::ral(foods, n = quantities))) ) # 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)) 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", foods)) # The string "n" will be replaced by the number used for pluralization paste("The caterpillar ate", plu::ral("n apple", mon)) paste("The caterpillar ate", plu::ral("n delicacy", foods)) # Special brace strings plu::ral("{one|two}", n = 1) plu::ral("{one|two}", n = 2) plu::ral("{one|two|more}", n = 1) plu::ral("{one|two|more}", n = 2) plu::ral("{one|two|more}", n = 3) plu::ral("{one|two|more}", n = 50) plu::ral("{one|two|three|more}", n = 1) plu::ral("{one|two|three|more}", n = 2) plu::ral("{one|two|three|more}", n = 3) plu::ral("{one|two|three|more}", n = 50) plu::ral("{one|two|three|more}", n = 0) plu::ral("{one|two|three|more}", n = 1.5)
Pluralize a word
plu_ralize( x, irregulars = getOption("plu.irregulars", c("moderate", "conservative", "liberal", "none")) ) ralize( x, irregulars = getOption("plu.irregulars", c("moderate", "conservative", "liberal", "none")) )
plu_ralize( x, irregulars = getOption("plu.irregulars", c("moderate", "conservative", "liberal", "none")) ) ralize( x, irregulars = getOption("plu.irregulars", c("moderate", "conservative", "liberal", "none")) )
x |
A character vector of English words to be pluralized |
irregulars |
What level of irregularity to use in pluralization.
|
The character vector x
pluralized
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.
Irregular plurals list adapted from the Automatically Generated Inflection Database (AGID).
See plu-package for more details.
plu_ral()
to pluralize an English phrase based on a condition
plu::ralize("word") plu::ralize(c("group", "word")) plu::ralize(c("formula", "person", "child"), irregulars = "conservative") plu::ralize(c("formula", "person", "child"), irregulars = "moderate") plu::ralize(c("formula", "person", "child"), irregulars = "liberal") plu::ralize(c("formula", "person", "child"), irregulars = "none")
plu::ralize("word") plu::ralize(c("group", "word")) plu::ralize(c("formula", "person", "child"), irregulars = "conservative") plu::ralize(c("formula", "person", "child"), irregulars = "moderate") plu::ralize(c("formula", "person", "child"), irregulars = "liberal") plu::ralize(c("formula", "person", "child"), irregulars = "none")
This function has been deprecated in favor of
and::and()
, knitr::combine_words()
or glue::glue_collapse()
.
plu_stick(...) stick(...)
plu_stick(...) stick(...)
... |