Type: Package
Title: Dynamically Pluralize Phrases
Version: 0.3.0
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.
License: MIT + file LICENSE
URL: https://pkg.rossellhayes.com/plu/, https://github.com/rossellhayes/plu
BugReports: https://github.com/rossellhayes/plu/issues
Depends: R (≥ 2.10)
Imports: lifecycle
Suggests: and, covr, crayon, fracture, glue, knitr, nombre, testthat (≥ 3.0.0), withr
RdMacros: lifecycle
Config/testthat/edition: 3
Encoding: UTF-8
Language: en-US
RoxygenNote: 7.2.3
NeedsCompilation: no
Packaged: 2023-09-23 18:27:17 UTC; alex
Author: Alexander Rossell Hayes ORCID iD [aut, cre, cph]
Maintainer: Alexander Rossell Hayes <alexander@rossellhayes.com>
Repository: CRAN
Date/Publication: 2023-09-23 19:40:02 UTC

plu: Dynamically Pluralize Phrases

Description

logo

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.

Copyright

plu uses data from the Automatically Generated Inflection Database (AGID), which is released under the following license:

The final product is under the following copyright, as well as any copyrights mentioned below.

Copyright 2000-2014 by Kevin Atkinson

Permission to use, copy, modify, distribute and sell this database, the associated scripts, the output created form the scripts and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Kevin Atkinson makes no representations about the suitability of this array for any purpose. It is provided "as is" without express or implied warranty.

The part-of-speech database is taken from Alan Beale 2of12id and the WordNet database which is under the following copyright:

This software and database is being provided to you, the LICENSEE, by Princeton University under the following license. By obtaining, using and/or copying this software and database, you agree that you have read, understood, and will comply with these terms and conditions.:

Permission to use, copy, modify and distribute this software and database and its documentation for any purpose and without fee or royalty is hereby granted, provided that you agree to comply with the following copyright notice and statements, including the disclaimer, and that the same appear on ALL copies of the software, database and documentation, including modifications that you make for internal use or for distribution.

WordNet 1.6 Copyright 1997 by Princeton University. All rights reserved.

THIS SOFTWARE AND DATABASE IS PROVIDED "AS IS" AND PRINCETON UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PRINCETON UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANT- ABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE, DATABASE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.

The name of Princeton University or Princeton may not be used in advertising or publicity pertaining to distribution of the software and/or database. Title to copyright in this software, database and any associated documentation shall at all times remain with Princeton University and LICENSEE agrees to preserve same.

Alan Beale 2of12id.txt is indirectly derived from the Moby part-of-speech database and the WordNet database. The Moby part-of-speech is in the public domain:

The Moby lexicon project is complete and has been place into the public domain. Use, sell, rework, excerpt and use in any way on any platform.

Placing this material on internal or public servers is also encouraged. The compiler is not aware of any export restrictions so freely distribute world-wide.

You can verify the public domain status by contacting

Grady Ward 3449 Martha Ct. Arcata, CA 95521-4884

grady@netcom.com grady@northcoast.com

The word list used is a combination of several word list:

  1. The ENABLE2K word lists which is in the public domain:

    The ENABLE master word list, WORD.LST, is herewith formally released into the Public Domain. Anyone is free to use it or distribute it in any manner they see fit. No fee or registration is required for its use nor are "contributions" solicited (if you feel you absolutely must contribute something for your own peace of mind, the authors of the ENABLE list ask that you make a donation on their behalf to your favorite charity). This word list is our gift to the Scrabble community, as an alternate to "official" word lists. Game designers may feel free to incorporate the WORD.LST into their games. Please mention the source and credit us as originators of the list. Note that if you, as a game designer, use the WORD.LST in your product, you may still copyright and protect your product, but you may not legally copyright or in any way restrict redistribution of the WORD.LST portion of your product. This may under law restrict your rights to restrict your users' rights, but that is only fair.

  2. All of the word lists except ABLE.LST in the ENABLE2K Supplemnt which consists of:

    2DICTS.LST ALSO.LST LETTERS.LST OSPDADD.LST UCACR.LST LCACR.LST NOPOS.LST PLURALS.LST UPPER.LST

    All of these word lists are also in the public domain.

  3. The list of signature words from the YAWL package which is in the public domain.

  4. The UK Advanced Cryptics Dictionary which in under the following copyright:

    Copyright (c) J Ross Beresford 1993-1999. All Rights Reserved.

    The following restriction is placed on the use of this publication: if The UK Advanced Cryptics Dictionary is used in a software package or redistributed in any form, the copyright notice must be prominently displayed and the text of this document must be included verbatim.

    There are no other restrictions: I would like to see the list distributed as widely as possible.

  5. Some extra words found in the Part-Of-Speech database that was not found in any of the above word lists.

  6. Words found in the Jargon File Word List package, available at http://aspell.sourceforge.net/wl/, which is in the Public Domain.

  7. Words in 2of12id.txt not in any of the word lists above. 2of12id is indirectly derived from all the above sources and most of the word lists from the Moby Words package:

    10196pla.ces 113809of.fic 21986na.mes 256772co.mpo 354984si.ngl 3897male.nam 4160offi.cia 4946fema.len 6213acro.nym 74550com.mon

    The Moby Word package, like the Part-Of-Speech database is in the public domain.

  8. And finally some extra words that I added myself. These words can be found in the file "extra-words"

Author(s)

Maintainer: Alexander Rossell Hayes alexander@rossellhayes.com (ORCID) [copyright holder]

See Also

Useful links:


Capitalization

Description

capitalize() returns a character vector x with the first alphabetic character replaced with a capital form (if one exists).

Usage

capitalize(x)

plu_capitalize(x)

is_capital(x, strict = FALSE)

is_capitalized(x, strict = FALSE)

Arguments

x

A character vector.

strict

If strict is TRUE, is_capital() and is_capitalized() return FALSE instead of NA when characters are neither capital nor lowercase. Defualts to FALSE.

Details

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.

Value

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.

Examples

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

Description

Find a function

Usage

get_fun(fn, default = identity)

Arguments

fn

A function name, either a character string or an unquoted function name, with or without colons.

default

If fn is NULL, the default function is returned. Defaults to identity().

Value

A function

Examples

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

Description

Informatively display a maximum number of elements

Usage

plu_more(x, max = 5, type = TRUE, fn = NULL, ..., det = "more")

more(x, max = 5, type = TRUE, fn = NULL, ..., det = "more")

Arguments

x

A vector or list.

max

The maximum number of items to list. Additional arguments are replaced with "n more". Defaults to 5. If max if Inf, NULL, FALSE, or NA, all elements are preserved.

type

A logical or character.

  • If a character, type is passed to ral() and pasted after the number of elements.

  • If TRUE, the default, the first class of x is used as the type.

    • If x is a list with different classes of element, "element" is used in place of a class name.

  • If FALSE or NA, nothing is pasted after the number of elements.

fn

A function to apply to the number of additional elements. Default to NULL, which applies no function.

...

Additional arguments to fn.

det

A determiner to place before the number of additional elements. Defaults to "more".

Value

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.

Examples

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

Description

Pluralize a phrase based on the length of a vector

Usage

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.

Special strings

Certain strings in x receive special treatment.

See Also

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

Examples

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

Description

Pluralize a word

Usage

plu_ralize(
  x,
  irregulars = getOption("plu.irregulars", c("moderate", "conservative", "liberal",
    "none"))
)

ralize(
  x,
  irregulars = getOption("plu.irregulars", c("moderate", "conservative", "liberal",
    "none"))
)

Arguments

x

A character vector of English words to be pluralized

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.

Value

The character vector x pluralized

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.

Source

Irregular plurals list adapted from the Automatically Generated Inflection Database (AGID).

See plu-package for more details.

See Also

plu_ral() to pluralize an English phrase based on a condition

Examples

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")

Deprecated functions

Description

[Deprecated] This function has been deprecated in favor of and::and(), knitr::combine_words() or glue::glue_collapse().

Usage

plu_stick(...)

stick(...)

Arguments

...

[Deprecated]

Value

A deprecation error.