Skip to content

Commit 7e2d75c

Browse files
committed
Updated name parser for v5.0 api.
1 parent 583fd74 commit 7e2d75c

File tree

9 files changed

+258
-37
lines changed

9 files changed

+258
-37
lines changed

src/org/nameapi/client/fault/FaultInfo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace org\nameapi\client\fault;
44

5+
require_once(__DIR__.'/Blame.php');
6+
require_once(__DIR__.'/FaultInfo.php');
7+
require_once(__DIR__.'/FaultInfoUnmarshaller.php');
8+
require_once(__DIR__.'/Retry.php');
9+
require_once(__DIR__.'/RetryType.php');
10+
require_once(__DIR__.'/ServiceException.php');
11+
512
/**
613
* An object containing fault information that is used within a {@link ServiceException}.
714
*

src/org/nameapi/client/services/BaseService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace org\nameapi\client\services;
44

5+
require_once(__DIR__.'/../fault/FaultInfo.php');
6+
57
use org\nameapi\client\fault\Blame;
68
use org\nameapi\client\fault\FaultInfo;
79
use org\nameapi\client\fault\Retry;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\parser\personnameparser;
4+
5+
6+
/**
7+
* Class DisputeType
8+
*
9+
* Possible values are:
10+
* GENDER
11+
* SPELLING
12+
*
13+
*/
14+
final class DisputeType {
15+
16+
/**
17+
* @var string $value
18+
*/
19+
private $value = null;
20+
21+
public function __construct($value) {
22+
if ($value!=='GENDER' && $value!=='SPELLING') {
23+
throw new \Exception('Invalid value for DisputeType: '.$value.'!');
24+
}
25+
$this->value = $value;
26+
}
27+
28+
29+
30+
public function __toString() {
31+
return $this->value;
32+
}
33+
34+
}
35+

src/org/nameapi/client/services/parser/personnameparser/ParsedPerson.php

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
namespace org\nameapi\client\services\parser\personnameparser;
44

55
require_once(__DIR__.'/../../../../ontology/input/entities/person/PersonType.php');
6+
require_once(__DIR__.'/../../../../ontology/input/entities/person/PersonRole.php');
7+
require_once(__DIR__.'/../../genderizer/persongenderizer/PersonGenderResult.php');
68
require_once(__DIR__.'/../OutputPersonName.php');
79

810
use org\nameapi\ontology\input\entities\person\PersonType;
11+
use org\nameapi\ontology\input\entities\person\PersonRole;
912
use org\nameapi\client\services\parser\OutputPersonName;
13+
use org\nameapi\client\services\genderizer\persongenderizer\PersonGenderResult;
1014

1115

1216
class ParsedPerson {
@@ -15,16 +19,43 @@ class ParsedPerson {
1519
* @var PersonType $personType
1620
*/
1721
private $personType = null;
22+
/**
23+
* @var PersonRole $personRole
24+
*/
25+
private $personRole = null;
26+
/**
27+
* @var PersonGenderResult $gender
28+
*/
29+
private $gender = null;
30+
31+
private $addressingGivenName = null;
32+
private $addressingSurname = null;
1833

1934
/**
20-
* @var OutputPersonName[] $names
35+
* @var OutputPersonName $outputPersonName
2136
*/
22-
private $names = null;
37+
private $outputPersonName = null;
2338

24-
public function __construct(PersonType $personType, array $names) {
25-
if (!$names) throw new \Exception("Names may not be empty!");
26-
$this->personType = $personType;
27-
$this->names = $names;
39+
/**
40+
* @var ParsedPerson[] $people
41+
*/
42+
private $people = null;
43+
44+
public function __construct(PersonType $personType,
45+
PersonRole $personRole,
46+
PersonGenderResult $gender,
47+
$addressingGivenName,
48+
$addressingSurname,
49+
OutputPersonName $outputPersonName,
50+
array $people) {
51+
//if (!$names) throw new \Exception("Names may not be empty!");
52+
$this->personType = $personType;
53+
$this->personRole = $personRole;
54+
$this->gender = $gender;
55+
$this->addressingGivenName = $addressingGivenName;
56+
$this->addressingSurname = $addressingSurname;
57+
$this->outputPersonName = $outputPersonName;
58+
$this->people = $people;
2859
}
2960

3061
/**
@@ -35,39 +66,55 @@ public function getPersonType() {
3566
}
3667

3768
/**
38-
* Usually just one, but multiple when the person type is NATURAL_MULTIPLE.
39-
* @return OutputPersonName[]
69+
* @return PersonRole
4070
*/
41-
public function getNames() {
42-
return $this->names;
71+
public function getPersonRole() {
72+
return $this->personRole;
4373
}
4474

4575
/**
46-
* @return OutputPersonName
76+
* @return PersonGenderResult
4777
*/
48-
public function getFirstName() {
49-
return $this->names[0];
78+
public function getGender() {
79+
return $this->gender;
80+
}
81+
82+
/**
83+
* @return null
84+
*/
85+
public function getAddressingGivenName() {
86+
return $this->addressingGivenName;
87+
}
88+
89+
/**
90+
* @return null
91+
*/
92+
public function getAddressingSurname() {
93+
return $this->addressingSurname;
5094
}
5195

5296
/**
53-
* @throws \Exception if there is not exactly one.
5497
* @return OutputPersonName
5598
*/
56-
public function getSingleName() {
57-
if (sizeof($this->names) != 1) {
58-
throw new \Exception('Expected exactly 1 name, got '.sizeof($this->names).'!');
59-
}
60-
return $this->names[0];
99+
public function getOutputPersonName() {
100+
return $this->outputPersonName;
101+
}
102+
103+
/**
104+
* Returns the people contained withhin this person.
105+
*
106+
* <p>If the getPersonType() is 'MULTIPLE' then expect content here. But also 'FAMILY' and 'LEGAL' can
107+
* have entries here.</p>
108+
*
109+
* @return ParsedPerson[]
110+
*/
111+
public function getPeople() {
112+
return $this->people;
61113
}
62114

63115

64116
public function __toString() {
65-
$nameStr = '';
66-
foreach ($this->names as $name) {
67-
if ($nameStr != '') $nameStr .= ',';
68-
$nameStr .= $name;
69-
}
70-
$str = 'ParsedPerson{personType='.$this->personType.', names='.$nameStr.'}';
117+
$str = 'ParsedPerson{personType='.$this->personType.'}';
71118
return $str;
72119
}
73120
}

src/org/nameapi/client/services/parser/personnameparser/ParsedPersonMatch.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace org\nameapi\client\services\parser\personnameparser;
44

55
require_once(__DIR__.'/ParsedPerson.php');
6+
require_once(__DIR__.'/ParserDispute.php');
67

78
class ParsedPersonMatch {
89

@@ -11,6 +12,11 @@ class ParsedPersonMatch {
1112
*/
1213
private $parsedPerson = null;
1314

15+
/**
16+
* @var ParserDispute[] $parserDisputes
17+
*/
18+
private $parserDisputes = null;
19+
1420
/**
1521
* @var float $likeliness
1622
*/
@@ -25,12 +31,14 @@ class ParsedPersonMatch {
2531
* @param ParsedPerson $parsedPerson
2632
* @param float $likeliness
2733
* @param float $confidence
34+
* @param $parserDisputes
2835
* @access public
2936
*/
30-
public function __construct($parsedPerson, $likeliness, $confidence) {
37+
public function __construct(ParsedPerson $parsedPerson, $likeliness, $confidence, $parserDisputes) {
3138
$this->parsedPerson = $parsedPerson;
3239
$this->likeliness = $likeliness;
3340
$this->confidence = $confidence;
41+
$this->parserDisputes = $parserDisputes;
3442
}
3543

3644
/**
@@ -54,4 +62,12 @@ public function getConfidence() {
5462
return $this->confidence;
5563
}
5664

65+
/**
66+
* Usually empty, that's good.
67+
* @return ParserDispute[]
68+
*/
69+
public function getParserDisputes() {
70+
return $this->parserDisputes;
71+
}
72+
5773
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\parser\personnameparser;
4+
5+
require_once(__DIR__.'/DisputeType.php');
6+
7+
class ParserDispute {
8+
9+
/**
10+
* @var DisputeType $disputeType
11+
*/
12+
private $disputeType = null;
13+
14+
/**
15+
* @var string $message
16+
*/
17+
private $message = null;
18+
19+
/**
20+
* @param DisputeType $disputeType
21+
* @param string $message
22+
* @access public
23+
*/
24+
public function __construct(DisputeType $disputeType, $message) {
25+
$this->disputeType = $disputeType;
26+
$this->message = $message;
27+
}
28+
29+
/**
30+
* @return DisputeType
31+
*/
32+
public function getDisputeType() {
33+
return $this->disputeType;
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getMessage() {
40+
return $this->message;
41+
}
42+
43+
}

src/org/nameapi/client/services/parser/personnameparser/PersonNameParserService.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace org\nameapi\client\services\parser\personnameparser;
44

5+
require_once(__DIR__.'/PersonNameParserResult.php');
6+
require_once(__DIR__.'/../../genderizer/persongenderizer/PersonGenderResult.php');
7+
58
use org\nameapi\client\fault\ServiceException;
69
use org\nameapi\client\services\BaseService;
710
use org\nameapi\client\services\parser\OutputPersonName;
@@ -10,8 +13,9 @@
1013
use org\nameapi\ontology\input\context\Context;
1114
use org\nameapi\ontology\input\entities\person\NaturalInputPerson;
1215
use org\nameapi\ontology\input\entities\person\PersonType;
13-
14-
require_once(__DIR__.'/PersonNameParserResult.php');
16+
use org\nameapi\ontology\input\entities\person\PersonRole;
17+
use org\nameapi\client\services\genderizer\persongenderizer\PersonGenderResult;
18+
use org\nameapi\ontology\input\entities\person\gender\ComputedPersonGender;
1519

1620

1721
/**
@@ -52,16 +56,54 @@ public function parse(NaturalInputPerson $person) {
5256
try {
5357
$matches = array(); //ParsedPersonMatch
5458
foreach ($response->matches as $match) {
55-
$names = array();
56-
foreach ($match->parsedPerson->names as $name) {
57-
$terms = array();
58-
foreach ($name->terms as $term) {
59+
$gender = new PersonGenderResult(
60+
new ComputedPersonGender($match->parsedPerson->gender->gender),
61+
(isset( $match->parsedPerson->gender->maleProportion) ? $match->parsedPerson->gender->maleProportion : null),
62+
$match->parsedPerson->gender->confidence
63+
);
64+
65+
$terms = array();
66+
if (isSet($match->parsedPerson->outputPersonName->terms)) {
67+
foreach ($match->parsedPerson->outputPersonName->terms as $term) {
5968
array_push($terms, new Term($term->string, new OutputTermType($term->termType)));
6069
}
61-
array_push($names, new OutputPersonName($terms));
6270
}
63-
$parsedPerson = new ParsedPerson(new PersonType($match->parsedPerson->personType), $names);
64-
$parsedPersonMatch = new ParsedPersonMatch($parsedPerson, $match->likeliness, $match->confidence);
71+
$outputPersonName = new OutputPersonName($terms);
72+
73+
$people = array();
74+
if (isSet($match->parsedPerson->people)) {
75+
foreach ($match->parsedPerson->people as $onePerson) {
76+
$terms = array();
77+
foreach ($onePerson->terms as $term) {
78+
array_push($terms, new Term($term->string, new OutputTermType($term->termType)));
79+
}
80+
array_push($names, new OutputPersonName($terms));
81+
}
82+
}
83+
84+
$parsedPerson = new ParsedPerson(
85+
new PersonType($match->parsedPerson->personType),
86+
new PersonRole($match->parsedPerson->personRole),
87+
$gender,
88+
$match->parsedPerson->addressingGivenName,
89+
$match->parsedPerson->addressingSurname,
90+
$outputPersonName,
91+
$people
92+
);
93+
94+
$parserDisputes = array();
95+
if (isSet($match->parsedPerson->parserDisputes)) {
96+
foreach ($match->parsedPerson->parserDisputes as $dispute) {
97+
array_push($parserDisputes, new ParserDispute(new DisputeType($dispute->disputeType), $dispute->message));
98+
}
99+
}
100+
101+
$parsedPersonMatch = new ParsedPersonMatch(
102+
$parsedPerson,
103+
$match->likeliness,
104+
$match->confidence,
105+
$parserDisputes
106+
);
65107
array_push($matches, $parsedPersonMatch);
66108
}
67109
return new PersonNameParserResult($matches);

0 commit comments

Comments
 (0)