plural

English pluralize/singularize engine with irregulars, count formatting and ordinals.

v1.0.0Text & stringsbundledimport "plural";
Overview Installation Documentation Examples

Overview

An English pluralization and singularization engine for HolyPHP with built-in irregular words, uncountable nouns, case preservation, custom rule registration, count formatting, and ordinal numbers.

bundled — works offline inflect singular ordinal

Quick start

import "plural";

println(plural_pluralize("box"));       // "boxes"
println(plural_pluralize("person"));    // "people"
println(plural_pluralize("City"));      // "Cities"
println(plural_singularize("knives"));  // "knife"
println(plural_count(3, "box"));        // "3 boxes"
println(plural_count(1, "boxes"));      // "1 box"
println(plural_ordinal(21));            // "21st"

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install plural

This adds the library to hphp.yaml, installs it into .hphp/packages/plural/ and pins the exact version in hphp.lock:

require:
  plural: "^1.0"

Use it

import "plural";

plural is bundled with the compiler, so import "plural"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install plural          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install plural
Packageplural
Version1.0.0 (follows the compiler)
Entry fileplural.hphp
Sourcepackages/lib/plural ↗

Documentation

API Reference

Top-Level Functions

All top-level functions operate on a shared default PluralEngine instance.

plural_pluralize(string $word): string

Returns the English plural form of $word, preserving lowercase, Title Case, and UPPERCASE formatting. Uncountable nouns and already-plural irregulars are returned unchanged.

plural_singularize(string $word): string

Returns the English singular form of $word, preserving original casing. Uncountable nouns and already-singular irregulars are returned unchanged.

plural_is_plural(string $word): bool

Returns true if $word is in plural form (or is uncountable), false otherwise.

plural_is_singular(string $word): bool

Returns true if $word is in singular form (or is uncountable), false otherwise.

plural_count(int $count, string $word, bool $inclusive = true): string

Inflects $word according to $count (singular for 1 and -1, plural otherwise).

plural_ordinal(int $n): string

Formats an integer as an English ordinal string (e.g. 1 -> "1st", 2 -> "2nd", 3 -> "3rd", 11 -> "11th", 21 -> "21st", -3 -> "-3rd").

plural_ordinal_suffix(int $n): string

Returns only the ordinal suffix ("st", "nd", "rd", or "th") for $n.

plural_add_irregular(string $singular, string $plural): void

Registers a custom irregular singular/plural pair on the default engine.

plural_add_uncountable(string $word): void

Registers a custom uncountable noun on the default engine.

plural_add_plural_rule(string $suffix, string $replacement): void

Registers a custom suffix replacement rule for pluralization (most recently added rule takes precedence).

plural_add_singular_rule(string $suffix, string $replacement): void

Registers a custom suffix replacement rule for singularization (most recently added rule takes precedence).

plural_reset(): void

Resets the default engine back to its built-in rules, clearing all custom additions.

PluralEngine Class

For isolated rule sets that do not affect the global default engine, instantiate PluralEngine directly:

$engine = new PluralEngine();
$engine->addIrregular("widget", "widgeti");
println($engine->pluralize("widget")); // "widgeti"

Methods:

Error Handling

All functions validate their inputs and throw an Exception prefixed with plural: on invalid input:

Examples

01Quick start

Bundled with the compiler — import "plural"; works offline in any file, or add it to a project with hphp install plural.

import "plural";

println(plural_pluralize("box"));       // "boxes"
println(plural_pluralize("person"));    // "people"
println(plural_pluralize("City"));      // "Cities"
println(plural_singularize("knives"));  // "knife"
println(plural_count(3, "box"));        // "3 boxes"
println(plural_count(1, "boxes"));      // "1 box"
println(plural_ordinal(21));            // "21st"
Try it
hphp install plural
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_plural.hphp.