@@ -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}
0 commit comments