Skip to content

Commit

Permalink
Merge 115b061 into 8da40c7
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed May 21, 2020
2 parents 8da40c7 + 115b061 commit 50487b6
Show file tree
Hide file tree
Showing 16 changed files with 357 additions and 44 deletions.
23 changes: 15 additions & 8 deletions README.md
Expand Up @@ -123,12 +123,13 @@ via `getFullName()`:
echo $name->getFullName(); // J. Peter M. Schluter
```

### Setting Languages
### Setting Definitions (Languages)
```php
$parser = new TheIconic\NameParser\Parser([
new TheIconic\NameParser\Language\English(), //default
new TheIconic\NameParser\Language\German(),
])
new TheIconic\NameParser\Definition\English\Basics(), //default
new TheIconic\NameParser\Definition\German\Basics(),
new TheIconic\NameParser\Definition\Configurable([...], [...], [...]),
]);
```

### Setting nickname delimiters
Expand Down Expand Up @@ -201,9 +202,9 @@ occur with different salutations, last name prefixes, suffixes etc.
or in some cases even with the parsing order.

To solve problems with salutations, last name prefixes and suffixes
you can create a separate language definition file and inject it when
instantiating the parser, see 'Setting Languages' above and compare
the existing language files as examples.
you can create a separate definition file and inject it when
instantiating the parser, see 'Setting Definitions' above and compare
the existing definition files as examples.

To deal with parsing order you may want to reformat the input string,
e.g. by simply splitting it into words and reversing their order.
Expand Down Expand Up @@ -235,7 +236,7 @@ nick names from the input string and then use these to implement gender
detection using another package (e.g. [this one](https://github.com/tuqqu/gender-detector)) or service.

### Having fun with normalisation
Writing different language files can not only be useful for parsing,
Writing different definition files can not only be useful for parsing,
but you can remap the normalised versions of salutations, prefixes and suffixes
to transform them into something totally different.

Expand All @@ -248,6 +249,12 @@ gist.
Of course this can also be used in more useful ways, e.g. to spell out
abbreviated titles, like `Prof.` as `Professor` etc. .

### Dynamic definition classes
As the `Configurable` definition class shows, your custom definitions
do not have to be defined in fixed constants. You can implement definition
files that load them from a configuration file or from a database or from
an API. And of course you can combine multiple of those.

## License

THE ICONIC Name Parser library for PHP is released under the MIT License.
72 changes: 72 additions & 0 deletions src/Definition/Configurable.php
@@ -0,0 +1,72 @@
<?php

namespace TheIconic\NameParser\Definition;

use TheIconic\NameParser\DefinitionInterface;

class Configurable implements DefinitionInterface
{
private $salutations = [];
private $suffixes = [];
private $lastnamePrefixes = [];

public function __construct(
array $salutations,
array $suffixes,
array $lastnamePrefixes
) {
$this->salutations = $this->sanitize($salutations);
$this->suffixes = $this->sanitize($suffixes);
$this->lastnamePrefixes = $this->sanitize($lastnamePrefixes);
}

public function addSalutations(array $salutations): void
{
$this->salutations = array_merge(
$this->salutations,
$this->sanitize($salutations)
);
}

public function addSuffixes(array $suffixes): void
{
$this->suffixes = array_merge(
$this->suffixes,
$this->sanitize($suffixes)
);
}

public function addLastnamePrefixes(array $lastnamePrefixes): void
{
$this->lastnamePrefixes = array_merge(
$this->lastnamePrefixes,
$this->sanitize($lastnamePrefixes)
);
}

public function getSalutations(): array
{
return $this->salutations;
}

public function getSuffixes(): array
{
return $this->suffixes;
}

public function getLastnamePrefixes(): array
{
return $this->lastnamePrefixes;
}

private function sanitize(array $mappings): array
{
$sanitized = [];

foreach ($mappings as $alias => $normalized) {
$sanitized[strtolower($alias)] = $normalized;
}

return $sanitized;
}
}
14 changes: 11 additions & 3 deletions src/Language/English.php → src/Definition/English/Basics.php
@@ -1,10 +1,10 @@
<?php

namespace TheIconic\NameParser\Language;
namespace TheIconic\NameParser\Definition\English;

use TheIconic\NameParser\LanguageInterface;
use TheIconic\NameParser\DefinitionInterface;

class English implements LanguageInterface
class Basics implements DefinitionInterface
{
const SUFFIXES = [
'1st' => '1st',
Expand Down Expand Up @@ -35,8 +35,11 @@ class English implements LanguageInterface
];

const SALUTATIONS = [
'dame' => 'Dame',
'dr' => 'Dr.',
'fr' => 'Fr.',
'lady' => 'Lady',
'lord' => 'Lord',
'madam' => 'Madam',
'master' => 'Mr.',
'miss' => 'Miss',
Expand All @@ -45,9 +48,14 @@ class English implements LanguageInterface
'mrs' => 'Mrs.',
'ms' => 'Ms.',
'mx' => 'Mx.',
'pastor' => 'Pr.',
'pr' => 'Pr.',
'rev' => 'Rev.',
'reverend' => 'Rev.',
'rt hon' => 'Rt. Hon.',
'sir' => 'Sir',
'prof' => 'Prof.',
'professor' => 'Prof.',
'his honour' => 'His Honour',
'her honour' => 'Her Honour'
];
Expand Down
155 changes: 155 additions & 0 deletions src/Definition/English/MilitaryRanks.php
@@ -0,0 +1,155 @@
<?php

namespace TheIconic\NameParser\Definition\English;

use TheIconic\NameParser\DefinitionInterface;

class MilitaryRanks implements DefinitionInterface
{
const SALUTATIONS = [
'1sg' => '1stSgt.',
'1stsgt' => '1stSgt.',
'a1c' => 'A1C',
'ab' => 'AB',
'adm' => 'Adm.',
'amn' => 'Amn.',
'ccm' => 'CCM',
'cdt' => 'Cdt.',
'cmc' => 'CMC',
'cmd' => 'Cmd.',
'cmsaf' => 'CMSAF',
'cmsgt' => 'CMSgt',
'cpl' => 'Cpl.',
'cpo' => 'CPO',
'cpt' => 'Cpt.',
'cptn' => 'Cpt.',
'csm' => 'CSM',
'ens' => 'Ens.',
'esn' => 'Ens.',
'fadm' => 'FAdm.',
'flt' => '1stLt.',
'fltmc' => 'FLTMC',
'formc' => 'FORMC',
'gen' => 'Gen.',
'gysgt' => 'GySgt.',
'lcpl' => 'LCpl.',
'ltcmd' => 'LtCmd.',
'ltgen' => 'LtGen.',
'maj' => 'Maj.',
'majgen' => 'MajGen.',
'mcpo' => 'MCPO',
'mcpo-cg' => 'MCPO-CG',
'mcpon' => 'MCPON',
'mgysgt' => 'MGySgt.',
'msg' => 'MSgt.',
'msgt' => 'MSgt.',
'ocdt' => 'OCdt.',
'pfc' => 'PFC',
'po1' => 'PO1',
'po2' => 'PO2',
'po3' => 'PO3',
'pv1' => 'Pvt.',
'pv2' => 'Pvt.',
'pvt' => 'Pvt.',
'radm' => 'RAdm.',
'sa' => 'SA',
'scpo' => 'SCPO',
'sfc' => 'SFC',
'sgm' => 'SgtMaj.',
'sgt' => 'Sgt.',
'sgtmaj' => 'SgtMaj.',
'sgtmajmc' => 'SgtMajMC',
'slt' => '2ndLt.',
'sma' => 'SMA',
'smsgt' => 'SMSgt.',
'sn' => 'Sn.',
'spc' => 'Spc.',
'sra' => 'SrA',
'ssg' => 'SSgt.',
'ssgt' => 'SSgt.',
'tsgt' => 'TSgt.',
'vadm' => 'VAdm.',
];

const SUFFIXES = [
'1sg' => '1SG',
'1stsgt' => '1SG',
'a1c' => 'A1C',
'ab' => 'AB',
'adm' => 'ADM',
'amn' => 'AMN',
'ccm' => 'CCM',
'cdt' => 'CDT',
'cmc' => 'CMC',
'cmd' => 'CMD',
'cmsaf' => 'CMSAF',
'cmsgt' => 'CMSGT',
'cpl' => 'CPL',
'cpo' => 'CPO',
'cpt' => 'CPT',
'cptn' => 'CPT',
'csm' => 'CSM',
'ens' => 'ENS',
'esn' => 'ENS',
'fadm' => 'FADM',
'flt' => '1LT',
'fltmc' => 'FLTMC',
'formc' => 'FORMC',
'gen' => 'GEN',
'gysgt' => 'GYSGT',
'lcpl' => 'LCPL',
'ltcmd' => 'LTCMD',
'ltgen' => 'LTGEN',
'maj' => 'MAJ',
'majgen' => 'MAJGEN',
'mcpo' => 'MCPO',
'mcpo-cg' => 'MCPO-CG',
'mcpon' => 'MCPON',
'mgysgt' => 'MGYSGT',
'msg' => 'MSGT',
'msgt' => 'MSGT',
'ocdt' => 'OCDT',
'pfc' => 'PFC',
'po1' => 'PO1',
'po2' => 'PO2',
'po3' => 'PO3',
'pv1' => '1PV',
'pv2' => '2PV',
'pvt' => 'PVT',
'radm' => 'RADM',
'sa' => 'SA',
'scpo' => 'SCPO',
'sfc' => 'SFC',
'sgm' => 'SGTMAJ',
'sgt' => 'SGT',
'sgtmaj' => 'SGTMAJ',
'sgtmajmc' => 'SGTMAJMC',
'slt' => '2LT',
'sma' => 'SMA',
'smsgt' => 'SMSGT',
'sn' => 'SN',
'spc' => 'SPC',
'sra' => 'SRA',
'ssg' => 'SSGT',
'ssgt' => 'SSGT',
'tsgt' => 'TSGT',
'vadm' => 'VADM',
];

const LASTNAME_PREFIXES = [];

public function getSuffixes(): array
{
return self::SUFFIXES;
}

public function getSalutations(): array
{
return self::SALUTATIONS;
}

public function getLastnamePrefixes(): array
{
return self::LASTNAME_PREFIXES;
}
}
6 changes: 3 additions & 3 deletions src/Language/German.php → src/Definition/German/Basics.php
@@ -1,10 +1,10 @@
<?php

namespace TheIconic\NameParser\Language;
namespace TheIconic\NameParser\Definition\German;

use TheIconic\NameParser\LanguageInterface;
use TheIconic\NameParser\DefinitionInterface;

class German implements LanguageInterface
class Basics implements DefinitionInterface
{
const SUFFIXES = [
'1.' => '1.',
Expand Down
2 changes: 1 addition & 1 deletion src/LanguageInterface.php → src/DefinitionInterface.php
Expand Up @@ -2,7 +2,7 @@

namespace TheIconic\NameParser;

interface LanguageInterface
interface DefinitionInterface
{
public function getSuffixes(): array;

Expand Down
1 change: 0 additions & 1 deletion src/Mapper/AbstractMapper.php
Expand Up @@ -3,7 +3,6 @@
namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Nickname;

abstract class AbstractMapper
{
Expand Down
1 change: 0 additions & 1 deletion src/Mapper/LastnameMapper.php
Expand Up @@ -2,7 +2,6 @@

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\LanguageInterface;
use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Lastname;
use TheIconic\NameParser\Part\LastnamePrefix;
Expand Down

0 comments on commit 50487b6

Please sign in to comment.