human_name_parser 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+
6
+ fixtures/clean
Binary file
@@ -1,7 +1,7 @@
1
1
  module HumanNameParser
2
2
  class Name
3
3
  PREFIXES = ['mr', 'ms', 'miss', 'mrs', 'sir', 'prof', 'professor', 'md', 'dr']
4
- SUFFIXES = ['esq','esquire','jr','sr','2','ii','iii','iv', 'v', 'phd', 'md', 'do', 'dc', 'dds']
4
+ SUFFIXES = ['esq','esquire','jr','sr','2', 'i', 'ii','iii','iv', 'v', 'phd', 'md', 'do', 'dc', 'dds']
5
5
  LAST_PREFIXES = ['al', 'bar','ben','bin','da','dal','de la', 'de', 'del', 'der', 'di', 'el', 'ibn', 'la', 'le', 'mc', 'san', 'st', 'ste', 'van', 'van der', 'van den', 'vel','von']
6
6
 
7
7
  attr_accessor :first, :middle, :last, :prefix, :suffix
@@ -128,9 +128,18 @@ module HumanNameParser
128
128
  match = @input_string.match(",")
129
129
  normalized = ""
130
130
  if match
131
- normalized = [match.post_match.strip, match.pre_match.strip].join(" ")
131
+ normalized_first, normalized_last = [match.post_match.strip, match.pre_match.strip]
132
+
133
+ # in case suffix follows right side of comma
134
+ split_right_side = normalized_first.split(' ')
135
+ while is_suffix?(split_right_side.last)
136
+ normalized_last += ' ' + split_right_side.pop
137
+
138
+ # prune suffix from first name group
139
+ normalized_first = split_right_side.join(' ')
140
+ end
132
141
  end
133
- normalized.split(" ")
142
+ [normalized_first, normalized_last].map {|n| n.split(" ")}.flatten
134
143
  end
135
144
 
136
145
  def split_first_middle_last
@@ -1,3 +1,3 @@
1
1
  module HumanNameParser
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/spec/name_spec.rb CHANGED
@@ -1,181 +1,47 @@
1
1
  require 'human_name_parser/name'
2
2
 
3
- describe HumanNameParser::Name do
4
- # any unicode issues?
5
- context "when full name is Björn Charles van der O'Malley" do
6
- let(:full_name) { "Björn Charles van der O'Malley" }
7
- it "should parse the name" do
8
- @name = HumanNameParser::Name.new full_name
9
- @name.first.should == 'Björn'
10
- @name.last.should == "van der O'Malley"
11
- @name.middle.should == 'Charles'
12
- end
13
- end
14
-
15
- context "when full name is Marley Mante PhD" do
16
- let(:full_name) { "Marley Mante PhD" }
17
- it "should parse the name" do
18
- @name = HumanNameParser::Name.new full_name
19
- @name.first.should == 'Marley'
20
- @name.last.should == 'Mante'
21
- @name.middle.should == ''
22
- @name.suffix.should == 'PhD'
23
- end
24
- end
25
-
26
- context "when full name is Marley Mante" do
27
- let(:full_name) { "Marley Mante" }
28
- it "should parse the name" do
29
- @name = HumanNameParser::Name.new full_name
30
- @name.first.should == 'Marley'
31
- @name.last.should == 'Mante'
32
- @name.middle.should == ''
33
- @name.suffix.should == ''
34
- end
35
- end
36
-
37
- context "when full name is Marley" do
38
- let(:full_name) { "Marley" }
39
- it "should parse the name" do
40
- @name = HumanNameParser::Name.new full_name
41
- @name.first.should == 'Marley'
42
- @name.last.should == ''
43
- @name.middle.should == ''
44
- @name.suffix.should == ''
45
- end
46
- end
47
-
48
- context "when full name is Marley Mante, esq." do
49
- let(:full_name) { "Marley Mante, esq." }
50
- it "should parse the name" do
51
- @name = HumanNameParser::Name.new full_name
52
- @name.first.should == 'Marley'
53
- @name.last.should == 'Mante'
54
- @name.middle.should == ''
55
- @name.suffix.should == 'esq.'
56
- end
57
- end
58
-
59
- context 'when full name is Mary Lou Smith' do
60
- let(:full_name) { "Mary Lou Smith" }
61
- before do
62
- @name = HumanNameParser::Name.new full_name
63
- end
64
-
65
- it "gets first name" do
66
- @name.first.should == 'Mary'
67
- end
68
-
69
- it "gets last name" do
70
- @name.last.should == 'Smith'
71
- end
72
-
73
- it "gets middle" do
74
- @name.middle.should == 'Lou'
75
- end
76
-
77
- it "gets prefix" do
78
- @name.prefix.should == ''
79
- end
80
-
81
- it "gets suffix" do
82
- @name.suffix.should == ''
83
- end
84
-
85
- it "gets initials" do
86
- @name.initials.should == 'MLS'
87
- end
88
- end
89
-
90
- context 'when full name is Mr. Alphonse di Morel Jr. Esq.' do
91
- let(:full_name) { "Mr. Alphonse di Morel Jr. Esq." }
92
- before { @name = HumanNameParser::Name.new full_name }
93
-
94
- it "gets first" do
95
- @name.first.should == 'Alphonse'
96
- end
3
+ NAMES = [
4
+ ['', ['', '', '', '', '', '']],
5
+ ['Downey, Robert Jr.', ['Robert', '', 'Downey', '', 'Jr.', 'RD']],
6
+ ['Mr. Alphonse di Morel Jr. Esq.', ['Alphonse', '', 'di Morel', 'Mr.', 'Jr. Esq.', 'AD']],
7
+ ['Mary Lou Smith', ['Mary', 'Lou', 'Smith', '', '', 'MLS']],
8
+ ['Marley', ['Marley', '', '', '', '', 'M']],
9
+ ['Marley Mante', ['Marley', '', 'Mante', '', '', 'MM']],
10
+ ['Marley Mante PhD', ['Marley', '', 'Mante', '', 'PhD', 'MM']],
11
+ ['Marley Mante, esq.', ['Marley', '', 'Mante', '', 'esq.', 'MM']],
12
+ ["Björn Charles van der O'Malley", ['Björn', 'Charles', "van der O'Malley", '', '', 'BCV']],
13
+ ['Alex Rothlenson-ben-Elsburghmohampton', ['Alex', '', 'Rothlenson-ben-Elsburghmohampton', '', '', 'AR']],
14
+ ['1234 Anywhere St., North Pole, SD 22323', ['', '', '', '', '', '']]
15
+ ]
97
16
 
98
- it "gets last" do
99
- @name.last.should == 'di Morel'
100
- end
101
-
102
- it "gets prefix" do
103
- @name.prefix.should == 'Mr.'
104
- end
105
-
106
- it "gets suffix" do
107
- @name.suffix.should == 'Jr. Esq.'
108
- end
109
-
110
- it "gets initials" do
111
- @name.initials.should == 'AD'
112
- end
113
- end
114
-
115
- context 'when full name is ROBOTO' do
116
- let(:full_name) { "ROBOTO" }
117
- before { @name = HumanNameParser::Name.new full_name }
118
-
119
- it "gets first" do
120
- @name.first.should == 'ROBOTO'
121
- end
122
-
123
- it 'gets initials' do
124
- @name.initials.should == 'R'
125
- end
126
-
127
- it "doesn't get last" do
128
- @name.last.should == ''
129
- end
130
- end
131
-
132
- context 'when full name is Downey Jr., Robert' do
133
- let(:full_name) { 'Downey Jr., Robert' }
134
- before { @name = HumanNameParser::Name.new full_name }
135
-
136
- it "gets first" do
137
- @name.first.should == 'Robert'
138
- end
139
-
140
- it 'gets initials' do
141
- @name.initials.should == 'RD'
142
- end
143
-
144
- it "gets last" do
145
- @name.last.should == 'Downey'
146
- end
147
-
148
- it "gets middle" do
149
- @name.middle.should == ''
150
- end
151
-
152
- it "gets suffix" do
153
- @name.suffix.should == 'Jr.'
154
- end
155
- end
17
+ describe HumanNameParser::Name do
18
+ NAMES.each do |name_array|
19
+ context "when full name is #{ name_array.first.inspect }" do
20
+ before { @name = HumanNameParser::Name.new name_array.first }
156
21
 
157
- context 'when full name is garbage' do
158
- let(:full_name) { '1234 Anywhere St., North Pole, SD 22323' }
159
- before { @name = HumanNameParser::Name.new full_name }
22
+ it "gets first name" do
23
+ @name.first.should == name_array[1][0]
24
+ end
160
25
 
161
- it "gets first" do
162
- @name.first.should == ''
163
- end
26
+ it "gets middle name" do
27
+ @name.middle.should == name_array[1][1]
28
+ end
164
29
 
165
- it 'gets initials' do
166
- @name.initials.should == ''
167
- end
30
+ it "gets last name" do
31
+ @name.last.should == name_array[1][2]
32
+ end
168
33
 
169
- it "gets last" do
170
- @name.last.should == ''
171
- end
34
+ it "gets prefix" do
35
+ @name.prefix.should == name_array[1][3]
36
+ end
172
37
 
173
- it "gets middle" do
174
- @name.middle.should == ''
175
- end
38
+ it "gets suffix" do
39
+ @name.suffix.should == name_array[1][4]
40
+ end
176
41
 
177
- it "gets suffix" do
178
- @name.suffix.should == ''
42
+ it "gets initials" do
43
+ @name.initials.should == name_array[1][5]
44
+ end
179
45
  end
180
46
  end
181
47
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human_name_parser
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Bachman
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-11 00:00:00 -05:00
18
+ date: 2011-11-14 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -61,6 +61,7 @@ files:
61
61
  - LICENSE
62
62
  - README.md
63
63
  - Rakefile
64
+ - fixtures/dist.female.first.gz
64
65
  - fixtures/test_names.txt
65
66
  - human_name_parser.gemspec
66
67
  - lib/human_name_parser.rb