Skip to content

Commit

Permalink
Enum support, PHP bumped to 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tuqqu committed Aug 27, 2023
1 parent d24ed55 commit 076205f
Show file tree
Hide file tree
Showing 23 changed files with 618 additions and 714 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
name: Static analysis
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
ini-values: memory_limit=-1
tools: composer:v2

Expand All @@ -32,12 +32,12 @@ jobs:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
ini-values: memory_limit=-1
tools: composer:v2

Expand Down
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
composer.lock
vendor
.idea/
/.php_cs.cache
/vendor
/.php-cs-fixer.cache
/.phpunit.result.cache
/.phpunit.cache
25 changes: 25 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

return (new PhpCsFixer\Config())
->setRules([
'@PER' => true,
'strict_param' => true,
'single_import_per_statement' => false,
'no_unused_imports' => true,
'array_syntax' => ['syntax' => 'short'],
'single_line_empty_body' => true,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
])
->setRiskyAllowed(true)
->setFinder(PhpCsFixer\Finder::create()
->in(__DIR__ . '/bin')
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
)
;
29 changes: 0 additions & 29 deletions .php_cs.dist

This file was deleted.

102 changes: 42 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,83 @@
## Gender Detector

GenderDetector is a PHP package that detects the gender of a person based on first name.
It uses the data file from the project 'gender.c' by Jörg Michael ([details](https://autohotkey.com/board/topic/20260-gender-verification-by-forename-cmd-line-tool-db/])).
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](https://autohotkey.com/board/topic/20260-gender-verification-by-forename-cmd-line-tool-db/])).

### Installation

Install it with Composer
Install it with Composer:

```bash
composer require tuqqu/gender-detector
```

### Usage

Its usage is simple, for any given name it will give you one of the following genders (strings):
```
male
mostly_male
unisex
mostly_female
female
```
For an unknown name it will return `null`.
All the gender values are available as constants of the `GenderDetector\Gender` class for the convenience.
The usage is straightforward:

```php
<?php
$detector = new GenderDetector\GenderDetector();

$genderDetector = new GenderDetector\GenderDetector();
$detector->getGender('Thomas');
// Gender::Male

print $genderDetector->detect('Thomas');
// male

print $genderDetector->detect('Avery');
// unisex
$detector->getGender('Avery');
// Gender::Unisex
```

I18N is fully supported
Internationalization (I18N) is fully supported:

```php
<?php

print $genderDetector->detect('Želmíra');
// female
$detector->getGender('Želmíra');
// Gender::Female

print $genderDetector->detect('Geirþrúður');
// female
$detector->getGender('Geirþrúður');
// Gender::Female
```

You may specify a country or region.
You can also specify a country or region:

```php
<?php
use GenderDetector\Country;

print $genderDetector->detect('Robin');
// mostly_male
$detector->getGender('Robin');
// Gender::MostlyMale

print $genderDetector->detect('Robin', GenderDetector\Country::USA);
// mostly_female
$detector->getGender('Robin', Country::Usa);
// Gender::MostlyFemale

print $genderDetector->detect('Robin', GenderDetector\Country::FRANCE);
// male
$detector->getGender('Robin', Country::France);
// Gender::Male

print $genderDetector->detect('Robin', GenderDetector\Country::IRELAND);
// unisex
$detector->getGender('Robin', Country::Ireland);
// Gender::Unisex
```

All the countries are available as constants of the `GenderDetector\Country` class.

For more details see [country list](/doc/country_list.md).
For more details about countries see [country list](/doc/country_list.md).


You may want to override the unknown name value.
If it is the case, you need to set a new value with `setUnknownGender(string $unknown)` method.
Full list of all the possible values are:

```php
<?php

$genderDetector = new GenderDetector\GenderDetector();

print $genderDetector->detect('Doe');
// (null)
enum Gender
{
case Male;
case MostlyMale;
case Female;
case MostlyFemale;
case Unisex;
}
```

$genderDetector->setUnknownGender(GenderDetector\Gender::UNISEX);
For an unknown name it will return `null`.

print $genderDetector->detect('Doe');
// unisex
```

If you happen to have an alternative data file, you might pass it to the `GenderDetector` constructor's first argument.
Additionally you may add new dictionary files with `addDictionaryFile(string $path)` method.
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:

```php
<?php

$genderDetector = new GenderDetector\GenderDetector('custom_file_path/dict.txt');
$genderDetector->addDictionaryFile('custom_file_path/another_dict.txt');
$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.
Expand All @@ -104,4 +86,4 @@ Note that each `GenderDetector` instantiation triggers file parsing, so you migh

The `GenderDetector` is licensed under the MIT License.

The data file `data/nam_dict.txt` is licensed under the GNU Free Documentation License.
The data file `data/nam_dict.txt` is licensed under the GNU Free Documentation License.
121 changes: 0 additions & 121 deletions bin/gender-detector

This file was deleted.

Loading

0 comments on commit 076205f

Please sign in to comment.