arraypress/inflector

A PHP library for pluralizing and singularizing English words with comprehensive irregular and uncountable word handling.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/arraypress/inflector

dev-main 2025-11-30 17:45 UTC

This package is auto-updated.

Last update: 2025-11-30 18:07:40 UTC


README

A PHP library for pluralizing and singularizing English words with comprehensive irregular and uncountable word handling.

Installation

composer require arraypress/inflector

Usage

Basic Pluralization

pluralize( 'cat' );       // 'cats'
pluralize( 'category' );  // 'categories'
pluralize( 'child' );     // 'children'
pluralize( 'person' );    // 'people'
pluralize( 'analysis' );  // 'analyses'

Basic Singularization

singularize( 'cats' );       // 'cat'
singularize( 'categories' ); // 'category'
singularize( 'children' );   // 'child'
singularize( 'people' );     // 'person'
singularize( 'analyses' );   // 'analysis'

Conditional Inflection

// Returns singular when count is 1, plural otherwise
inflect( 'cat', 1 );   // 'cat'
inflect( 'cat', 2 );   // 'cats'
inflect( 'cat', 0 );   // 'cats'

// Or use the count parameter on pluralize
pluralize( 'item', 1 );  // 'item'
pluralize( 'item', 5 );  // 'items'

Detection

is_plural( 'cats' );        // true
is_plural( 'children' );    // true
is_plural( 'cat' );         // false

is_singular( 'cat' );       // true
is_singular( 'person' );    // true
is_singular( 'people' );    // false

is_uncountable( 'sheep' );       // true
is_uncountable( 'information' ); // true
is_uncountable( 'cat' );         // false

Case Preservation

The inflector preserves the case pattern of the original word:

pluralize( 'Cat' );      // 'Cats'
pluralize( 'CAT' );      // 'CATS'
pluralize( 'Person' );   // 'People'
pluralize( 'PERSON' );   // 'PEOPLE'

singularize( 'CHILDREN' );  // 'CHILD'
singularize( 'People' );    // 'Person'

Using the Class Directly

If you prefer, you can also use the Inflector class directly:

use ArrayPress\Inflector\Inflector;

Inflector::pluralize( 'category' );    // 'categories'
Inflector::singularize( 'children' );  // 'child'
Inflector::is_plural( 'cats' );        // true
Inflector::clear_cache();              // Clear internal caches

Supported Word Types

Irregular Words

The library handles 150+ irregular English words including:

  • People: person/people, man/men, woman/women, child/children
  • Body parts: tooth/teeth, foot/feet, goose/geese
  • Animals: mouse/mice, louse/lice, ox/oxen
  • Latin/Greek: criterion/criteria, phenomenon/phenomena, analysis/analyses, thesis/theses
  • Latin -us: cactus/cacti, fungus/fungi, nucleus/nuclei, radius/radii
  • Latin -a: alumna/alumnae, antenna/antennae, formula/formulae
  • Latin -ex/-ix: index/indices, matrix/matrices, vertex/vertices
  • French: bureau/bureaux, chateau/chateaux, plateau/plateaux
  • f/fe endings: leaf/leaves, life/lives, knife/knives, wolf/wolves
  • Compound words: passerby/passersby, son-in-law/sons-in-law

Uncountable Words

Words that remain the same in singular and plural:

  • Animals: sheep, fish, deer, moose, salmon, trout, bison
  • Abstract concepts: information, advice, knowledge, news, research
  • Materials: equipment, furniture, luggage, rice, water, money
  • Academic fields: mathematics, physics, economics, statistics
  • Other: traffic, software, feedback, homework

Regular Patterns

Standard English pluralization rules:

  • Add -s: cat → cats, dog → dogs
  • Add -es (s, ss, sh, ch, x, z): bus → buses, church → churches
  • Change -y to -ies: city → cities, category → categories
  • Change -f/-fe to -ves: leaf → leaves, knife → knives
  • Add -es (consonant + o): hero → heroes, potato → potatoes

Functions

Function Returns Description
pluralize( $word, $count ) string Convert to plural (or singular if count is 1)
singularize( $word ) string Convert to singular
inflect( $word, $count ) string Plural if count ≠ 1, singular if count = 1
is_plural( $word ) bool Check if word is plural
is_singular( $word ) bool Check if word is singular
is_uncountable( $word ) bool Check if word is uncountable

Performance

The library uses internal caching to optimize repeated operations. Results are cached on first use, making subsequent calls extremely fast.

// First call computes and caches
pluralize( 'category' );  // Computed

// Subsequent calls use cache
pluralize( 'category' );  // From cache

Requirements

  • PHP 8.0+

License

GPL-2.0-or-later