Class: People::NameParser

Inherits:
Object
  • Object
show all
Defined in:
lib/people.rb

Overview

Class to parse names into their components like first, middle, last, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ NameParser

Creates a name parsing object



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
# File 'lib/people.rb', line 9

def initialize( opts={} )

  @name_chars = "A-Za-z0-9\\-\\'"
  @nc = @name_chars

  @opts = {
    :strip_mr   => true,
    :strip_mrs  => false,
    :case_mode  => 'proper',
    :couples    => false
  }.merge! opts

  ## constants

  @titles = [ 'Mr\.? and Mrs\.? ',
              'Mrs\.? ',
              'M/s\.? ',
              'Ms\.? ',
              'Miss\.? ',
              'Mme\.? ',
              'Mr\.? ',
              'Messrs ',
              'Mister ',
              'Mast(\.|er)? ',
              'Ms?gr\.? ',
              'Sir ',
              'Lord ',
              'Lady ',
              'Madam(e)? ',
              'Dame ',

              # Medical
              'Dr\.? ',
              'Doctor ',
              'Sister ',
              'Matron ',

              # Legal
              'Judge ',
              'Justice ',

              # Police
              'Det\.? ',
              'Insp\.? ',

              # Military
              'Brig(adier)? ',
              'Capt(\.|ain)? ',
              'Commander ',
              'Commodore ',
              'Cdr\.? ',
              'Colonel ',
              'Gen(\.|eral)? ',
              'Field Marshall ',
              'Fl\.? Off\.? ',
              'Flight Officer ',
              'Flt Lt ',
              'Flight Lieutenant ',
              'Pte\. ',
              'Private ',
              'Sgt\.? ',
              'Sargent ',
              'Air Commander ',
              'Air Commodore ',
              'Air Marshall ',
              'Lieutenant Colonel ',
              'Lt\.? Col\.? ',
              'Lt\.? Gen\.? ',
              'Lt\.? Cdr\.? ',
              'Lieutenant ',
              '(Lt|Leut|Lieut)\.? ',
              'Major General ',
              'Maj\.? Gen\.?',
              'Major ',
              'Maj\.? ',

              # Religious
              'Rabbi ',
              'Brother ',
              'Father ',
              'Chaplain ',
              'Pastor ',
              'Bishop ',
              'Mother Superior ',
              'Mother ',
              'Most Rever[e|a]nd ',
              'Very Rever[e|a]nd ',
              'Mt\.? Revd\.? ',
              'V\.? Revd?\.? ',
              'Rever[e|a]nd ',
              'Revd?\.? ',

              # Other
              'Prof(\.|essor)? ',
              'Ald(\.|erman)? '
            ];


  @suffixes = [
               'Jn?r\.?,? Esq\.?',
               'Sn?r\.?,? Esq\.?',
               'I{1,3},? Esq\.?',

               'Jn?r\.?,? M\.?D\.?',
               'Sn?r\.?,? M\.?D\.?',
               'I{1,3},? M\.?D\.?',

               'Sn?r\.?',         # Senior
               'Jn?r\.?',         # Junior

               'Esq(\.|uire)?',
               'Esquire.',
               'Attorney at Law.',
               'Attorney-at-Law.',

               'Ph\.?d\.?',
               'C\.?P\.?A\.?',

               'XI{1,3}',            # 11th, 12th, 13th
               'X',                  # 10th
               'IV',                 # 4th
               'VI{1,3}',            # 6th, 7th, 8th
               'V',                  # 5th
               'IX',                 # 9th
               'I{1,3}\.?',             # 1st, 2nd, 3rd
               'M\.?D\.?',           # M.D.
               'D.?M\.?D\.?'         # M.D.
              ];

  @last_name_p = "((;.+)|(((Mc|Mac|Des|Dell[ae]|Del|De La|De Los|Da|Di|Du|La|Le|Lo|St\.|Den|Von|Van|Von Der|Van De[nr]) )?([#{@nc}]+)))";
  @mult_name_p = "((;.+)|(((Mc|Mac|Des|Dell[ae]|Del|De La|De Los|Da|Di|Du|La|Le|Lo|St\.|Den|Von|Van|Von Der|Van De[nr]) )?([#{@nc} ]+)))";

  @seen = 0
  @parsed = 0;

end

Instance Attribute Details

#parsedObject (readonly)

Returns the value of attribute parsed.



6
7
8
# File 'lib/people.rb', line 6

def parsed
  @parsed
end

#seenObject (readonly)

Returns the value of attribute seen.



6
7
8
# File 'lib/people.rb', line 6

def seen
  @seen
end

Instance Method Details

#clean(s) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
# File 'lib/people.rb', line 291

def clean( s )

  # remove illegal characters
  s.gsub!( /[^A-Za-z0-9\-\'\.&\/ \,]/, "" )
  # remove repeating spaces
  s.gsub!( /  +/, " " )
  s.gsub!( /\s+/, " " )
  s.strip!
  s

end

#get_name_parts(name, no_last_name = false) ⇒ Object



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
# File 'lib/people.rb', line 334

def get_name_parts( name, no_last_name = false )

  first  = ""
  middle = ""
  last   = ""

  if no_last_name
    last_name_p = ''
    mult_name_p = ''
  else
    last_name_p = @last_name_p
    mult_name_p = @mult_name_p
  end

  parsed = false

  # M ERICSON
  if name.match( /^([A-Za-z])\.? (#{last_name_p})$/i )
    first  = $1;
    middle = '';
    last   = $2;
    parsed = true
    parse_type = 1;

    # M E ERICSON
  elsif name.match( /^([A-Za-z])\.? ([A-Za-z])\.? (#{last_name_p})$/i )
    first  = $1;
    middle = $2;
    last   = $3;
    parsed = true
    parse_type = 2;

    # M.E. ERICSON
  elsif name.match( /^([A-Za-z])\.([A-Za-z])\. (#{last_name_p})$/i )
    first  = $1;
    middle = $2;
    last   = $3;
    parsed = true
    parse_type = 3;

    # M E E ERICSON
  elsif name.match( /^([A-Za-z])\.? ([A-Za-z])\.? ([A-Za-z])\.? (#{last_name_p})$/i )
    first  = $1;
    middle = $2 + ' ' + $3;
    last   = $4;
    parsed = true
    parse_type = 4;

    # M EDWARD ERICSON
  elsif name.match( /^([A-Za-z])\.? ([#{@nc}]+) (#{last_name_p})$/i )
    first  = $1;
    middle = $2;
    last   = $3;
    parsed = true
    parse_type = 5;

    # MATTHEW E ERICSON
  elsif name.match( /^([#{@nc}]+) ([A-Za-z])\.? (#{last_name_p})$/i )
    first  = $1;
    middle = $2;
    last   = $3;
    parsed = true
    parse_type = 6;

    # MATTHEW E E ERICSON
  elsif name.match( /^([#{@nc}]+) ([A-Za-z])\.? ([A-Za-z])\.? (#{last_name_p})$/i )
    first  = $1;
    middle = $2 + ' ' + $3;
    last   = $4;
    parsed = true
    parse_type = 7;

    # MATTHEW E.E. ERICSON
  elsif name.match( /^([#{@nc}]+) ([A-Za-z]\.[A-Za-z]\.) (#{last_name_p})$/i )
    first  = $1;
    middle = $2;
    last   = $3;
    parsed = true
    parse_type = 8;

    # MATTHEW ERICSON
  elsif name.match( /^([#{@nc}]+) (#{last_name_p})$/i )
    first  = $1;
    middle = '';
    last   = $2;
    parsed = true
    parse_type = 9;

    # MATTHEW EDWARD ERICSON
  elsif name.match( /^([#{@nc}]+) ([#{@nc}]+) (#{last_name_p})$/i )
    first  = $1;
    middle = $2;
    last   = $3;
    parsed = true
    parse_type = 10;

    # MATTHEW E. SHEIE ERICSON
  elsif name.match( /^([#{@nc}]+) ([A-Za-z])\.? ($multNamePat)$/i )
    first  = $1;
    middle = $2;
    last   = $3;
    parsed = true
    parse_type = 11;
  end

  last.gsub!( /;/, "" )

  return [ parsed, parse_type, first, middle, last ];

end

#get_suffix(name) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/people.rb', line 319

def get_suffix( name )

  @suffixes.each do |sfx|
    sfx_p = Regexp.new( "(.+) (#{sfx})$", true )
    if name.match( sfx_p )
      name.replace $1.strip
      suffix = $2
      return $2
    end

  end

  return ""
end

#get_title(name) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/people.rb', line 303

def get_title( name )

  @titles.each do |title|
    title_p = Regexp.new( "^(#{title})(.+)", true )
    if m = name.match( title_p )

      title = m[1]
      name.replace( m[-1].strip )
      return title
    end

  end

  return ""
end

#parse(name) ⇒ Object



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
# File 'lib/people.rb', line 146

def parse( name )

  @seen += 1

  clean  = ''
  out = Hash.new( "" )

  out[:orig]  = name.dup

  name = name.dup

  name = clean( name )

  # strip trailing suffices
  @suffixes.each do |sfx|
    sfx_p = Regexp.new( "(.+), (#{sfx})$", true )
    ##puts sfx_p
    name.gsub!( sfx_p, "\\1 \\2" )
  end

  name.gsub!( /Mr\.? \& Mrs\.?/i, "Mr. and Mrs." )

  # Flip last and first if contain comma
  name.gsub!( /;/, "" )
  name.gsub!( /(.+),(.+)/, "\\2 ;\\1" )


  name.gsub!( /,/, "" )
  name.strip!

  if @opts[:couples]
    name.gsub!( / +and +/i, " \& " )
  end



  if @opts[:couples] && name.match( /\&/ )

    names = name.split( / *& */ )
    a = names[0]
    b = names[1]

    out[:title2] = get_title( b );
    out[:suffix2] = get_suffix( b );

    b.strip!

    parts = get_name_parts( b )

    out[:parsed2] = parts[0]
    out[:parse_type2] = parts[1]
    out[:first2] = parts[2]
    out[:middle2] = parts[3]
    out[:last] = parts[4]

    out[:title] = get_title( a );
    out[:suffix] = get_suffix( a );

    a.strip!
    a += " "

    parts = get_name_parts( a, true )

    out[:parsed] = parts[0]
    out[:parse_type] = parts[1]
    out[:first] = parts[2]
    out[:middle] = parts[3]

    if out[:parsed] && out[:parsed2]
      out[:multiple] = true
    else
      out = Hash.new( "" )
    end


  else

    out[:title] = get_title( name );
    out[:suffix] = get_suffix( name );

    parts = get_name_parts( name )

    out[:parsed] = parts[0]
    out[:parse_type] = parts[1]
    out[:first] = parts[2]
    out[:middle] = parts[3]
    out[:last] = parts[4]

  end


  if @opts[:case_mode] == 'proper'
    [ :title, :first, :middle, :last, :suffix, :clean, :first2, :middle2, :title2, :suffix2 ].each do |part|
      next if part == :suffix && out[part].match( /^[iv]+$/i );
      out[part] = proper( out[part] )
    end

  elsif @opts[:case_mode] == 'upper'
    [ :title, :first, :middle, :last, :suffix, :clean, :first2, :middle2, :title2, :suffix2 ].each do |part|
      out[part].upcase!
    end

  else

  end

  if out[:parsed]
    @parsed += 1
  end

  out[:clean] = name





  return {
    :title       => "",
    :first       => "",
    :middle      => "",
    :last        => "",
    :suffix      => "",

    :title2      => "",
    :first2      => "",
    :middle2     => "",
    :suffix2     => "",

    :clean       => "",

    :parsed      => false,
    :parse_type  => "",

    :parsed      => false,
    :parse_type  => "",

    :parsed2     => false,
    :parse_type2 => "",

    :multiple    => false
  }.merge( out )

end

#proper(name) ⇒ Object



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
# File 'lib/people.rb', line 447

def proper ( name )

  fixed = name.downcase

  # Now uppercase first letter of every word. By checking on word boundaries,
  # we will account for apostrophes (D'Angelo) and hyphenated names
  fixed.gsub!( /\b(\w+)/ ) { |m| m.match( /^[ixv]$+/i ) ? m.upcase :  m.capitalize }

  # Name case Macs and Mcs
  # Exclude names with 1-2 letters after prefix like Mack, Macky, Mace
  # Exclude names ending in a,c,i,o,z or j, typically Polish or Italian

  if fixed.match( /\bMac[a-z]{2,}[^a|c|i|o|z|j]\b/i  )

    fixed.gsub!( /\b(Mac)([a-z]+)/i ) do |m|
      $1 + $2.capitalize
    end

    # Now correct for "Mac" exceptions
    fixed.gsub!( /MacHin/i,  'Machin' )
    fixed.gsub!( /MacHlin/i, 'Machlin' )
    fixed.gsub!( /MacHar/i,  'Machar' )
    fixed.gsub!( /MacKle/i,  'Mackle' )
    fixed.gsub!( /MacKlin/i, 'Macklin' )
    fixed.gsub!( /MacKie/i,  'Mackie' )

    # Portuguese
    fixed.gsub!( /MacHado/i,  'Machado' );

    # Lithuanian
    fixed.gsub!( /MacEvicius/i, 'Macevicius' )
    fixed.gsub!( /MacIulis/i,   'Maciulis' )
    fixed.gsub!( /MacIas/i,     'Macias' )

  elsif fixed.match( /\bMc/i )
    fixed.gsub!( /\b(Mc)([a-z]+)/i ) do |m|
      $1 + $2.capitalize
    end

  end

  # Exceptions (only 'Mac' name ending in 'o' ?)
  fixed.gsub!( /Macmurdo/i, 'MacMurdo' )

  return fixed

end