Skip to content

Commit 309081c

Browse files
committed
Updated person name parser for updated result object.
1 parent 66287b0 commit 309081c

File tree

4 files changed

+20
-90
lines changed

4 files changed

+20
-90
lines changed

src/org/nameapi/client/services/parser/ParsingStatus.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,19 @@
22

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

5-
use org\nameapi\client\services\parser\ParsingStatus;
6-
7-
require_once(__DIR__.'/../ParsingStatus.php');
85
require_once(__DIR__.'/ParsedPersonMatch.php');
96

107

118
class PersonNameParserResult {
129

13-
/**
14-
* @var ParsingStatus
15-
*/
16-
private $parsingStatus = null;
17-
18-
/**
19-
* @var string $errorMessage
20-
*/
21-
private $errorMessage = null;
22-
2310
/**
2411
* @var ParsedPersonMatch[] $matches
2512
*/
2613
private $matches = null;
2714

28-
public function __construct(ParsingStatus $parsingStatus, $errorMessage, $matches) {
29-
$this->parsingStatus = $parsingStatus;
30-
$this->errorMessage = $errorMessage;
31-
$this->matches = $matches;
32-
}
33-
34-
/**
35-
* Tells whether parsing was successful or not.
36-
* @return ParsingStatus
37-
*/
38-
public function getParsingStatus() {
39-
return $this->parsingStatus;
40-
}
41-
42-
/**
43-
* Returns a string error message if getParsingStatus() is FAILURE, and null otherwise.
44-
* @return string
45-
*/
46-
public function getErrorMessage() {
47-
return $this->errorMessage;
15+
public function __construct($matches) {
16+
if (sizeof($matches)==0) throw new \Exception("At least one match is required!");
17+
$this->matches = $matches;
4818
}
4919

5020
/**
@@ -56,11 +26,10 @@ public function getMatches() {
5626
}
5727

5828
/**
59-
* Returns the best match, or null if none.
29+
* Returns the best match.
6030
* @return ParsedPersonMatch
6131
*/
6232
public function getBestMatch() {
63-
if (!$this->matches) return null;
6433
return $this->matches[0];
6534
}
6635

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use org\nameapi\ontology\input\entities\person\NaturalInputPerson;
77
use org\nameapi\ontology\input\entities\person\PersonType;
88

9-
use org\nameapi\client\services\parser\ParsingStatus;
109
use org\nameapi\client\services\parser\OutputTermType;
1110
use org\nameapi\client\services\parser\Term;
1211
use org\nameapi\client\services\parser\OutputPersonName;
@@ -43,26 +42,21 @@ public function __construct(Context $context) {
4342
public function parse(NaturalInputPerson $person) {
4443
$parameters = new wsdl\ParseArguments($this->context, $person);
4544
$result = $this->soapPersonNameParser->parse($parameters);
46-
$parsingStatus = new ParsingStatus($result->parsingStatus);
47-
$errorMessage = ((string)$parsingStatus==='FAILURE') ? $result->errorMessage : null;
48-
$matches = null;
49-
if ($parsingStatus->isSuccess()) {
50-
$matches = array(); //ParsedPersonMatch
51-
foreach ($result->parsedPersonMatches as $match) {
52-
$names = array();
53-
foreach ($match->parsedPerson->names as $name) {
54-
$terms = array();
55-
foreach ($name->terms as $term) {
56-
array_push($terms, new Term($term->string, new OutputTermType($term->termType)));
57-
}
58-
array_push($names, new OutputPersonName($terms));
45+
$matches = array(); //ParsedPersonMatch
46+
foreach ($result->parsedPersonMatches as $match) {
47+
$names = array();
48+
foreach ($match->parsedPerson->names as $name) {
49+
$terms = array();
50+
foreach ($name->terms as $term) {
51+
array_push($terms, new Term($term->string, new OutputTermType($term->termType)));
5952
}
60-
$parsedPerson = new ParsedPerson(new PersonType($match->parsedPerson->personType), $names);
61-
$parsedPersonMatch = new ParsedPersonMatch($parsedPerson, $match->likeliness, $match->confidence);
62-
array_push($matches, $parsedPersonMatch);
53+
array_push($names, new OutputPersonName($terms));
6354
}
55+
$parsedPerson = new ParsedPerson(new PersonType($match->parsedPerson->personType), $names);
56+
$parsedPersonMatch = new ParsedPersonMatch($parsedPerson, $match->likeliness, $match->confidence);
57+
array_push($matches, $parsedPersonMatch);
6458
}
65-
return new PersonNameParserResult($parsingStatus, $errorMessage, $matches);
59+
return new PersonNameParserResult($matches);
6660
}
6761

6862
}

tests/functional/org/nameapi/client/services/parser/personnameparser/PersonNameParserServiceTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public function testParse() {
1818
->build())
1919
->build();
2020
$parseResult = $personNameParser->parse($inputPerson);
21-
$this->assertEquals('SUCCESS', (string)$parseResult->getParsingStatus());
21+
$bestMatch = $parseResult->getBestMatch();
22+
$name = $bestMatch->getParsedPerson()->getFirstName();
23+
$this->assertEquals('John', $name->getFirst('GIVENNAME')->getString());
24+
$this->assertEquals('Doe', $name->getFirst('SURNAME')->getString());
2225
}
2326

2427
}

0 commit comments

Comments
 (0)