forked from joshfraser/PHP-Name-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.php
512 lines (447 loc) · 20.1 KB
/
parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
/**
* Split a full name into its constituent parts
* - prefix/salutation (Mr. Mrs. Dr. etc)
* - given/first name
* - middle name/initial(s)
* - surname (last name)
* - suffix (II, PhD, Jr. etc)
*
* Author: Josh Fraser
*
* Contribution from Clive Verrall www.cliveverrall.com February 2016
*
* // other contributions:
* // - eric willis [list of honorifics](http://notes.ericwillis.com/2009/11/common-name-prefixes-titles-and-honorifics/)
* // - `TomThak` for raising issue #16 and providing [wikepedia resource](https://cs.wikipedia.org/wiki/Akademick%C3%BD_titul)
* // - `atla5` for closing the issue.
*/
class FullNameParser {
/**
* Create the dictionary of terms for use later
*
* - Common honorific prefixes (english)
* - Common compound surname identifiers
* - Common suffixes (lineage and professional)
* Note: longer professional titles should appear earlier in the array than shorter titles to reduce the risk of mis-identification e.g. BEng before BE
* Also note that case and periods are part of the matching for professional titles and therefore need to be correct, there are no case conversions
*/
protected $dict = array(
'prefix' => array(
'Mr.' => array('mr', 'mister', 'master'),
'Mrs.' => array('mrs', 'missus', 'missis'),
'Ms.' => array('ms', 'miss'),
'Dr.' => array('dr'),
'Rev.' => array("rev", "rev'd", "reverend"),
'Fr.' => array('fr', 'father'),
'Sr.' => array('sr', 'sister'),
'Prof.' => array('prof', 'professor'),
'Sir' => array('sir'),
'Hon.' => array('honorable'),
'Pres.' => array('president'),
'Gov' => array('governor','governer'),
'Ofc' => array('officer'),
'Msgr' => array('monsignor'),
'Sr.' => array('sister'),
'Br.' => array('brother'),
'Supt.' => array('superintendent'),
'Rep.' => array('representatitve'),
'Sen.' => array('senator'),
'Amb.' => array('ambassador'),
'Treas.' => array('treasurer'),
'Sec.' => array('secretary'),
'Pvt.' => array('private'),
'Cpl.' => array('corporal'),
'Sgt.' => array('sargent'),
'Adm.' => array('administrative','administrator','administrater'),
'Maj.' => array('major'),
'Capt.' => array('captain'),
'Cmdr.' => array('commander'),
'Lt.' => array('lieutenant'),
'Lt. Col.' => array('lieutenant colonel'),
'Col.' => array('colonel'),
'Gen.' => array('general'),
'Bc.' => array('bachelor', 'baccalaureus'),
'BcA.' => array('bachelor of arts', 'baccalaureus artis'),
'ICDr.' => array('doctor of canon law', 'juris cononici doctor'),
'Ing.' => array('engineer', 'ingenieur'),
'Ing. sheet.' => array('architect engineer', 'intrudes upon architectus'),
'JUDr.' => array('juris doctor utriusque', 'doctor rights'),
'MDDr.' => array('doctor of dental medicine', 'medicinae doctor dentium'),
'MgA.' => array('master of arts','magister artis'),
'Mgr.' => array('master'),
'MD.' => array('doctor of general medicine'),
'DVM.' => array('doctor of veterinary medine'),
'PaedDr.' => array('doctor of education'),
'PharmDr.' => array('doctor of pharmacy'),
'PhDr.' => array('doctor of philosophy'),
'PhMr.' => array('master of pharmacy'),
'RCDr.' => array('doctor of business studies'),
'RNDr.' => array('doctor of science'),
'DSc.' => array('doctor of science'),
'RSDr.' => array('doctor of socio-political sciences'),
'RTDr.' => array('doctor of technical sciences'),
'ThDr.' => array('doctor of theology'),
'Th.D.' => array('doctor of theology'),
'ThLic.' => array('licentiate of theology'),
'ThMgr.' => array('master of theology','master of divinity'),
'Acad.' => array('academian','academic'),
'ArtD.' => array('doctor of arts'),
'DiS.' => array('certified specialist'),
'As.' => array('assistant'),
'Odb. As.' => array('assistant professor'),
'Doc.' => array('associate professor'),
' ' => array('the')
),
'compound' => array('da','de','del','della','der','di','du','la','pietro','st.','st','ter','van','vanden','vere','von'),
'suffixes' => array(
'line' => array('I','II','III','IV','V','1st','2nd','3rd','4th','5th','Senior','Junior','Jr','Sr'),
'prof' => array('AO', 'B.A.', 'M.Sc', 'BCompt', 'PhD', 'Ph.D.','APR','RPh','PE','MD', 'M.D.', 'MA','DMD','CME', 'BSc', 'Bsc', 'BSc(hons)', 'Ph.D.', 'BEng', 'M.B.A.', 'MBA', 'FAICD', 'CM', 'OBC', 'M.B.', 'ChB', 'FRCP', 'FRSC',
'FREng', 'Esq', 'MEng', 'MSc', 'J.D.', 'JD', 'BGDipBus', 'Dip', 'Dipl.Phys','M.H.Sc.', 'MPA', 'B.Comm', 'B.Eng', 'B.Acc', 'FSA', 'PGDM', 'FCPA', 'RN', 'R.N.', 'MSN',
'PCA', 'PCCRM','PCFP','PCGD','PCHR','PCM','PCPS','PCPM','PCSCM','PCSM','PCMM','PCTC','ACA', 'FCA','ACMA', 'FCMA','AAIA', 'FAIA','CCC','MIPA','FIPA','CIA','CFE','CISA','CFAP',
'QC', 'Q.C.', 'M.Tech', 'CTA', 'C.I.M.A.', 'B.Ec',
'CFIA','ICCP','CPS','CAP-OM','CAPTA','TNAOAP','AFA','AVA','ASA','CAIA','CBA','CVA','ICVS','CIIA','CMU','PFM','PRM','CFP','CWM','CCP','EA','CCMT','CGAP','CDFM','CFO','CGFM','CGAT','CGFO','CMFO','CPFO','CPFA',
'BMD','BIET','P.Eng','PE', 'MBBS', 'MB', 'BCh', 'BAO', 'BMBS', 'MBBChir', 'MBChBa','MPhil','LL.D', 'LLD', 'D.Lit','DEA', 'DESS', 'DClinPsy', 'DSc', 'MRes', 'M.Res', 'Psy.D', 'Pharm.D',
'BA(Admin)', 'BAcc','BACom','BAdmin','BAE','BAEcon','BA(Ed)','BA(FS)','BAgr','BAH','BAI','BAI(Elect)','BAI(Mech)','BALaw','BAO','BAppSc','BArch','BArchSc','BARelSt','BASc','BASoc', 'D.D.S.',
'BASS','BATheol','BBA','BBLS', 'BBS','BBus','BChem','BCJ','BCL','BCLD(SocSc)','BClinSci','BCom','BCombSt','BCommEdCommDev','BComp','BComSc','BCoun','BD','BDes','BE','BEcon','BEcon&Fin', 'M.P.P.M.', 'MPPM',
'BEconSci', 'BEd','BEng','BES','BEng(Tech)','BFA','BFin','BFLS','BFST','BH','BHealthSc','BHSc','BHy','BJur','BL','BLE','BLegSc','BLib','BLing','BLitt','BLittCelt','BLS','BMedSc','BMet',
'BMid', 'BMin','BMS','BMSc','BMSc','BMS','BMus','BMusEd','BMusPerf','BN', 'BNS','BNurs','BOptom','BPA','BPharm', 'BPhil', 'TTC', 'DIP', 'Tchg', 'BEd', 'MEd','ACIB', 'FCIM', 'FCIS', 'FCS', 'Fcs',
'Bachelor', 'O.C.', 'JP', 'C.Eng', 'C.P.A.', 'B.B.S.', 'MBE', 'GBE', 'KBE', 'DBE', 'CBE', 'OBE', 'MRICS', 'D.P.S.K.', 'D.P.P.J.', 'DPSK', 'DPPJ', 'B.B.A.', 'GBS', 'MIGEM', 'M.I.G.E.M.', 'FCIS',
'BPhil(Ed)', 'BPhys','BPhysio','BPl','BRadiog','BSc', 'B.Sc', 'BScAgr','BSc(Dairy)','BSc(DomSc)','BScEc','BScEcon','BSc(Econ)','BSc(Eng)','BScFor','BSc(HealthSc)','BSc(Hort)', 'BBA', 'B.B.A.',
'BSc(MCRM)', 'BSc(Med)','BSc(Mid)','BSc(Min)','BSc(Psych)', 'BSc(Tech)','BSD', 'BSocSc','BSS','BStSu','BTchg','BTCP','BTech','BTechEd','BTh','BTheol','BTS','EdB','LittB','LLB','MA','MusB','ScBTech',
'CEng', 'FCA', 'CFA', 'Cfa', 'C.F.A.', 'LLB', 'LL.B', 'LLM', 'LL.M', 'CA(SA)', 'C.A.', 'CA','CPA', 'Solicitor', 'DMS', 'FIWO', 'CEnv', 'MICE', 'MIWEM', 'B.Com', 'BCom', 'BAcc', 'BA', 'BEc', 'MEc', 'HDip', 'B.Bus.', 'E.S.C.P.' )
),
'vowels' => array('a','e','i','o','u')
);
protected $not_nicknames = array( "(hons)");
/**
* This is the primary method which calls all other methods
*
* @param string $name the full name you wish to parse
* @return array returns associative array of name parts
*/
public function parse_name($full_name) {
# Remove leading/trailing whitespace
$full_name = trim($full_name);
// remove any words that don't add value
// $full_name = str_replace("(Hons)", '', $full_name );
// $full_name = str_replace("(hons)", '', $full_name );
# Setup default vars
extract(array('salutation' => '', 'fname' => '', 'initials' => '', 'lname' => '', 'suffix' => ''));
# Find all the professional suffixes possible
$professional_suffix = $this->get_pro_suffix($full_name);
// The position of the first professional suffix denotes then end of the name and the start of the suffixes
$first_suffix_index = strlen($full_name);
foreach ($professional_suffix as $key => $psx) {
$start = strpos($full_name, $psx);
if( $start === FALSE ) {
echo "ASSERT ERROR, the professional suffix:".$psx." cannot be found in the full name:".$full_name."<br>";
continue;
}
if( $start < $first_suffix_index) {
$first_suffix_index = $start;
}
}
// everything to the right of the first professional suffix is part of the suffix
$suffix = substr($full_name, $first_suffix_index);
// remove the suffixes from the full_name
$full_name = substr($full_name, 0, $first_suffix_index);
# Deal with nickname, push to array
$has_nick = $this->get_nickname($full_name);
if ($has_nick) {
# Remove wrapper chars from around nickname
$name['nickname'] = substr($has_nick, 1, (strlen($has_nick) - 2));
# Remove the nickname from the full name
$full_name = str_replace($has_nick, '', $full_name);
# Get rid of consecutive spaces left by the removal
$full_name = str_replace(' ', ' ', $full_name);
}
# Grab a list of words from the remainder of the full name
$unfiltered_name_parts = $this->break_words($full_name);
# Is first word a title or multiple titles consecutively?
if( count($unfiltered_name_parts)) {
// only start looking if there are any words left in the name to process
while ($s = $this->is_salutation($unfiltered_name_parts[0])) {
$salutation .= "$s ";
array_shift($unfiltered_name_parts);
}
$salutation = trim($salutation);
// Find if there is a line suffix, if so then move it out
# Is last word a suffix or multiple suffixes consecutively?
while ($s = $this->is_line_suffix($unfiltered_name_parts[count($unfiltered_name_parts)-1], $full_name)) {
if( $suffix != "") {
$suffix = $s.", ".$suffix;
} else {
$suffix .= $s;
}
array_pop($unfiltered_name_parts);
}
$suffix = trim($suffix);
} else {
$salutation = "";
$suffix = "";
}
// Re-pack the unfiltered name parts array and exclude empty words
$name_arr = array();
foreach ($unfiltered_name_parts as $key => $name_part) {
$name_part = trim($name_part);
if(strlen($name_part) == '1') {
// If any word left is of one character that is not alphabetic then it is not a real word, so remove it
if( ! ctype_alpha($name_part)) {
$name_part = "";
}
}
if( strlen(trim($name_part)) ) {
$name_arr[] = $name_part;
}
}
$unfiltered_name_parts = $name_arr;
# set the ending range after prefix/suffix trim
$end = count($unfiltered_name_parts);
# concat the first name
for ($i=0; $i<$end-1; $i++) {
$word = $unfiltered_name_parts[$i];
# move on to parsing the last name if we find an indicator of a compound last name (Von, Van, etc)
# we use $i != 0 to allow for rare cases where an indicator is actually the first name (like "Von Fabella")
if ($this->is_compound($word) && $i != 0) {
break;
}
# is it a middle initial or part of their first name?
# if we start off with an initial, we'll call it the first name
if ($this->is_initial($word)) {
# is the initial the first word?
if ($i == 0) {
# if so, do a look-ahead to see if they go by their middle name
# for ex: "R. Jason Smith" => "Jason Smith" & "R." is stored as an initial
# but "R. J. Smith" => "R. Smith" and "J." is stored as an initial
if ($this->is_initial($unfiltered_name_parts[$i+1])) {
$fname .= " ".strtoupper($word);
}
else {
$initials .= " ".strtoupper($word);
}
}
# otherwise, just go ahead and save the initial
else {
$initials .= " ".strtoupper($word);
}
}
else {
$fname .= " ".$this->fix_case($word);
}
}
if( count($unfiltered_name_parts)) {
# check that we have more than 1 word in our string
if ($end-0 > 1) {
# concat the last name
for ($i; $i < $end; $i++) {
$lname .= " ".$this->fix_case($unfiltered_name_parts[$i]);
}
}
else {
# otherwise, single word strings are assumed to be first names
$fname = $this->fix_case($unfiltered_name_parts[$i]);
}
} else {
$fname = "";
}
# return the various parts in an array
$name['salutation'] = $salutation;
$name['fname'] = trim($fname);
$name['initials'] = trim($initials);
$name['lname'] = trim($lname);
$name['suffix'] = $suffix;
return $name;
}
/**
* Breaks name into individual words
*
* @param string $name the full name you wish to parse
* @return array full list of words broken down by spaces
*/
public function break_words($name) {
$temp_word_arr = explode(' ', $name);
$final_word_arr = array();
foreach ($temp_word_arr as $key => $word) {
if( $word != "" && $word != ",") {
$final_word_arr[] = $word;
}
}
return $final_word_arr;
}
/**
* Checks for the existence of, and returns professional suffix
*
* @param string $name the name you wish to test
* @return mixed returns the suffix if exists, false otherwise
*/
public function get_pro_suffix($name) {
$found_suffix_arr = array();
foreach ($this->dict['suffixes']['prof'] as $suffix) {
if (preg_match("/,[\s]*$suffix\b/i", $name, $matches)) {
$found_suffix = trim($matches[0]);
$found_suffix = rtrim($found_suffix,',');
$found_suffix = ltrim($found_suffix,',');
$found_suffix_arr[] = trim($found_suffix);
} else if( strpos($name, $suffix) !== FALSE ) {
$found_suffix_arr[] = $suffix;
}
}
return $found_suffix_arr;
}
/**
* Function to check name for existence of nickname based on these stipulations
* - String wrapped in parentheses (string)
* - String wrapped in double quotes "string"
* x String wrapped in single quotes 'string'
*
* I removed the check for strings in single quotes 'string' due to possible
* conflicts with names that may include apostrophes. Arabic transliterations, for example
*
* @param string $name the name you wish to test against
* @return mixed returns nickname if exists, false otherwise
*/
protected function get_nickname($name) {
if (preg_match("/[\(|\"].*?[\)|\"]/", $name, $matches)) {
if( ! in_array( strtolower($matches[0]), $this->not_nicknames ) ) {
return $matches[0];
} else {
return false;
}
}
return false;
}
/**
* Checks word against array of common lineage suffixes
*
* @param string $word the single word you wish to test
* @param string $name full name for context in determining edge-cases
* @return mixed boolean if false, string if true (returns suffix)
*/
protected function is_line_suffix($word, $name) {
# Ignore periods and righ commas, normalize case
$word = str_replace('.', '', strtolower($word));
$word = rtrim($word,',');
# Search the array for our word
$line_match = array_search($word, array_map('strtolower', $this->dict['suffixes']['line']));
# Now test our edge cases based on lineage
if ($line_match !== false) {
# Store our match
$matched_case = $this->dict['suffixes']['line'][$line_match];
# Remove it from the array
$temp_array = $this->dict['suffixes']['line'];
unset($temp_array[$line_match]);
# Make sure we're dealing with the suffix and not a surname
if ($word == 'senior' || $word == 'junior') {
# If name is Joshua Senior, it's pretty likely that Senior is the surname
# However, if the name is Joshua Jones Senior, then it's likely a suffix
if (str_word_count($name) < 3) {
return false;
}
# If the word Junior or Senior is contained, but so is some other
# lineage suffix, then the word is likely a surname and not a suffix
foreach ($temp_array as $suffix) {
if (preg_match("/\b".$suffix."\b/i", $name)) {
return false;
}
}
}
return $matched_case;
}
return false;
}
/**
* Checks word against list of common honorific prefixes
*
* @param string $word the single word you wish to test
* @return boolean
*/
protected function is_salutation($word) {
$word = str_replace('.', '', strtolower($word));
foreach ($this->dict['prefix'] as $replace => $originals) {
if (in_array($word, $originals)) {
return $replace;
}
}
return false;
}
/**
* Checks our dictionary of compound indicators to see if last name is compound
*
* @param string $word the single word you wish to test
* @return boolean
*/
protected function is_compound($word) {
return array_search(strtolower($word), $this->dict['compound']);
}
/**
* Test string to see if it's a single letter/initial (period optional)
*
* @param string $word the single word you wish to test
* @return boolean
*/
protected function is_initial($word) {
return ((strlen($word) == 1) || (strlen($word) == 2 && $word{1} == "."));
}
/**
* Checks for camelCase words such as McDonald and MacElroy
*
* @param string $word the single word you wish to test
* @return boolean
*/
protected function is_camel_case($word) {
if (preg_match("/[A-Za-z]([A-Z]*[a-z][a-z]*[A-Z]|[a-z]*[A-Z][A-Z]*[a-z])[A-Za-z]*/", $word)) {
return true;
}
return false;
}
# ucfirst words split by dashes or periods
# ucfirst all upper/lower strings, but leave camelcase words alone
public function fix_case($word) {
# Fix case for words split by periods (J.P.)
if (strpos($word, '.') !== false) {
$word = $this->safe_ucfirst(".", $word);;
}
# Fix case for words split by hyphens (Kimura-Fay)
if (strpos($word, '-') !== false) {
$word = $this->safe_ucfirst("-", $word);
}
# Special case for single letters
if (strlen($word) == 1) {
$word = strtoupper($word);
}
# Special case for 2-letter words
if (strlen($word) == 2) {
# Both letters vowels (uppercase both)
if (in_array(strtolower($word{0}), $this->dict['vowels']) && in_array(strtolower($word{1}), $this->dict['vowels'])) {
$word = strtoupper($word);
}
# Both letters consonants (uppercase both)
if (!in_array(strtolower($word{0}), $this->dict['vowels']) && !in_array(strtolower($word{1}), $this->dict['vowels'])) {
$word = strtoupper($word);
}
# First letter is vowel, second letter consonant (uppercase first)
if (in_array(strtolower($word{0}), $this->dict['vowels']) && !in_array(strtolower($word{1}), $this->dict['vowels'])) {
$word = ucfirst(strtolower($word));
}
# First letter consonant, second letter vowel or "y" (uppercase first)
if (!in_array(strtolower($word{0}), $this->dict['vowels']) && (in_array(strtolower($word{1}), $this->dict['vowels']) || strtolower($word{1}) == 'y')) {
$word = ucfirst(strtolower($word));
}
}
# Fix case for words which aren't initials, but are all upercase or lowercase
if ( (strlen($word) >= 3) && (ctype_upper($word) || ctype_lower($word)) ) {
$word = ucfirst(strtolower($word));
}
return $word;
}
# helper public function for fix_case
public function safe_ucfirst($seperator, $word) {
# uppercase words split by the seperator (ex. dashes or periods)
$parts = explode($seperator, $word);
foreach ($parts as $word) {
$words[] = ($this->is_camel_case($word)) ? $word : ucfirst(strtolower($word));
}
return implode($seperator, $words);
}
}