Skip to content

tuqqu/gender-detector

Repository files navigation

Gender Detector

Get the most likely gender associated with a first name using PHP.

This library utilizes data sourced from the gender.c project created by Jörg Michael (details).

Installation

Install it with Composer:

composer require tuqqu/gender-detector

Usage

The usage is straightforward:

$detector = new GenderDetector\GenderDetector();

$detector->getGender('Thomas');
// Gender::Male

$detector->getGender('Avery');
// Gender::Unisex

I18N is fully supported:

$detector->getGender('Geirþrúður');
// Gender::Female

You can also specify a country:

$detector->getGender('Robin');
// Gender::MostlyMale

$detector->getGender('Robin', 'US');
// Gender::MostlyFemale

$detector->getGender('Robin', 'FR');
// Gender::Male

$detector->getGender('Robin', 'IE');
// Gender::Unisex

Country codes are case-insensitive ISO 3166-1 alpha-2 codes. Alternatively, you can utilize enum values; the complete list of values and codes can be found here.

Full list of all the possible gender values are:

enum Gender
{
    case Male;
    case MostlyMale;
    case Female;
    case MostlyFemale;
    case Unisex;
}

For an unknown name it will return null.

If you have an alternative data file, you can pass it as the first argument to the GenderDetector constructor. Additionally, you can add new dictionary files using the addDictionaryFile(string $path) method:

$detector = new GenderDetector('custom_file_path/dict.txt');
$detector->addDictionaryFile('custom_file_path/another_dict.txt');

Note that each GenderDetector instantiation triggers file parsing, so you might want to avoid reading the same file twice.

Licenses

The GenderDetector is licensed under the MIT License.

The data file data/nam_dict.txt is licensed under the GNU Free Documentation License.