Skip to content

Commit d1b519c

Browse files
committed
Name parser: recursively extracting the people. Also extracting the parser disputes. Added test cases.
1 parent caa00f2 commit d1b519c

File tree

2 files changed

+88
-38
lines changed

2 files changed

+88
-38
lines changed

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

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,47 +56,13 @@ public function parse(NaturalInputPerson $person) {
5656
try {
5757
$matches = array(); //ParsedPersonMatch
5858
foreach ($response->matches as $match) {
59-
$gender = null;
60-
if (isSet($match->parsedPerson->gender)) {
61-
$gender = new PersonGenderResult(
62-
new ComputedPersonGender($match->parsedPerson->gender->gender),
63-
(isset( $match->parsedPerson->gender->maleProportion) ? $match->parsedPerson->gender->maleProportion : null),
64-
$match->parsedPerson->gender->confidence
65-
);
66-
}
59+
$pp = $match->parsedPerson;
6760

68-
$terms = array();
69-
if (isSet($match->parsedPerson->outputPersonName->terms)) {
70-
foreach ($match->parsedPerson->outputPersonName->terms as $term) {
71-
array_push($terms, new Term($term->string, new OutputTermType($term->termType)));
72-
}
73-
}
74-
$outputPersonName = new OutputPersonName($terms);
75-
76-
$people = array();
77-
if (isSet($match->parsedPerson->people)) {
78-
foreach ($match->parsedPerson->people as $onePerson) {
79-
$terms = array();
80-
foreach ($onePerson->terms as $term) {
81-
array_push($terms, new Term($term->string, new OutputTermType($term->termType)));
82-
}
83-
array_push($names, new OutputPersonName($terms));
84-
}
85-
}
86-
87-
$parsedPerson = new ParsedPerson(
88-
new PersonType($match->parsedPerson->personType),
89-
new PersonRole($match->parsedPerson->personRole),
90-
$gender,
91-
(isSet($match->parsedPerson->addressingGivenName)) ? $match->parsedPerson->addressingGivenName : null,
92-
(isSet($match->parsedPerson->addressingSurname)) ? $match->parsedPerson->addressingSurname : null,
93-
$outputPersonName,
94-
$people
95-
);
61+
$parsedPerson = $this->extractPerson($pp);
9662

9763
$parserDisputes = array();
98-
if (isSet($match->parsedPerson->parserDisputes)) {
99-
foreach ($match->parsedPerson->parserDisputes as $dispute) {
64+
if (isSet($match->parserDisputes)) {
65+
foreach ($match->parserDisputes as $dispute) {
10066
array_push($parserDisputes, new ParserDispute(new DisputeType($dispute->disputeType), $dispute->message));
10167
}
10268
}
@@ -115,4 +81,51 @@ public function parse(NaturalInputPerson $person) {
11581
}
11682
}
11783

84+
private function extractGender($parsedPerson) {
85+
if (!isSet($parsedPerson->gender)) {
86+
return null;
87+
}
88+
return new PersonGenderResult(
89+
new ComputedPersonGender($parsedPerson->gender->gender),
90+
(isset( $parsedPerson->gender->maleProportion) ? $parsedPerson->gender->maleProportion : null),
91+
$parsedPerson->gender->confidence
92+
);
93+
}
94+
95+
private function extractTerms($parsedPerson) {
96+
$terms = array();
97+
if (isSet($parsedPerson->outputPersonName) && isSet($parsedPerson->outputPersonName->terms)) {
98+
foreach ($parsedPerson->outputPersonName->terms as $term) {
99+
array_push($terms, new Term($term->string, new OutputTermType($term->termType)));
100+
}
101+
}
102+
return new OutputPersonName($terms);
103+
}
104+
105+
private function extractPeople($parsedPerson) {
106+
$people = array();
107+
if (isSet($parsedPerson->people)) {
108+
foreach ($parsedPerson->people as $onePerson) {
109+
$extractedPerson = $this->extractPerson($onePerson);
110+
array_push($people, $extractedPerson);
111+
}
112+
}
113+
return $people;
114+
}
115+
116+
private function extractPerson($pp) {
117+
$gender = $this->extractGender($pp);
118+
$outputPersonName = $this->extractTerms($pp);
119+
$people = $this->extractPeople($pp);
120+
return new ParsedPerson(
121+
new PersonType($pp->personType),
122+
new PersonRole($pp->personRole),
123+
$gender,
124+
(isSet($pp->addressingGivenName)) ? $pp->addressingGivenName : null,
125+
(isSet($pp->addressingSurname)) ? $pp->addressingSurname : null,
126+
$outputPersonName,
127+
$people
128+
);
129+
}
130+
118131
}

tests/functional/PersonNameParserServiceTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,43 @@ public function testParse() {
4444
$this->assertEquals('Doe', $bestMatch->getParsedPerson()->getAddressingSurname());
4545
$this->assertEquals('John', $bestMatch->getParsedPerson()->getOutputPersonName()->getFirst('GIVENNAME')->getString());
4646
$this->assertEquals('Doe', $bestMatch->getParsedPerson()->getOutputPersonName()->getFirst('SURNAME')->getString());
47+
48+
$this->assertEquals(1, sizeof($bestMatch->getParserDisputes()));
49+
$this->assertEquals('GENDER', (string)$bestMatch->getParserDisputes()[0]->getDisputeType());
50+
}
51+
52+
public function test_parseTwoPeople() {
53+
//setup code:
54+
$context = Context::builder()
55+
->priority(Priority::REALTIME())
56+
->build();
57+
$myApiKey = 'test'; //grab one from nameapi.org
58+
$serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('rc50-api.nameapi.org'), '5.0');
59+
$personNameParser = $serviceFactory->parserServices()->personNameParser();
60+
61+
//the call:
62+
$inputPerson = NaturalInputPerson::builder()
63+
->name(InputPersonName::westernBuilder()
64+
->fullname( "Peter und Daniela Meyer" )
65+
->build())
66+
->build();
67+
$parseResult = $personNameParser->parse($inputPerson);
68+
69+
//the assertions:
70+
$bestMatch = $parseResult->getBestMatch();
71+
$this->assertEquals('MULTIPLE', (string)$bestMatch->getParsedPerson()->getPersonType());
72+
$people = $bestMatch->getParsedPerson()->getPeople();
73+
$this->assertEquals(2, sizeof($people));
74+
75+
$firstPerson = $people[0];
76+
$this->assertEquals('Peter', $firstPerson->getAddressingGivenName());
77+
$this->assertEquals('Meyer', $firstPerson->getAddressingSurname());
78+
$this->assertEquals('MALE', (string)$firstPerson->getGender()->getGender());
79+
80+
$secondPerson = $people[1];
81+
$this->assertEquals('Daniela', $secondPerson->getAddressingGivenName());
82+
$this->assertEquals('Meyer', $secondPerson->getAddressingSurname());
83+
$this->assertEquals('FEMALE', (string)$secondPerson->getGender()->getGender());
4784
}
4885

4986
}

0 commit comments

Comments
 (0)