Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: abachman/human_name_parser
base: 06dc786e8d
...
head fork: abachman/human_name_parser
compare: 34ab7bb682
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
2  .gitignore
@@ -2,3 +2,5 @@
.bundle
Gemfile.lock
pkg/*
+
+fixtures/clean
View
BIN  fixtures/dist.female.first.gz
Binary file not shown
View
15 lib/human_name_parser/name.rb
@@ -1,7 +1,7 @@
module HumanNameParser
class Name
PREFIXES = ['mr', 'ms', 'miss', 'mrs', 'sir', 'prof', 'professor', 'md', 'dr']
- SUFFIXES = ['esq','esquire','jr','sr','2','ii','iii','iv', 'v', 'phd', 'md', 'do', 'dc', 'dds']
+ SUFFIXES = ['esq','esquire','jr','sr','2', 'i', 'ii','iii','iv', 'v', 'phd', 'md', 'do', 'dc', 'dds']
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']
attr_accessor :first, :middle, :last, :prefix, :suffix
@@ -128,9 +128,18 @@ def split_last_comma_first_middle
match = @input_string.match(",")
normalized = ""
if match
- normalized = [match.post_match.strip, match.pre_match.strip].join(" ")
+ normalized_first, normalized_last = [match.post_match.strip, match.pre_match.strip]
+
+ # in case suffix follows right side of comma
+ split_right_side = normalized_first.split(' ')
+ while is_suffix?(split_right_side.last)
+ normalized_last += ' ' + split_right_side.pop
+
+ # prune suffix from first name group
+ normalized_first = split_right_side.join(' ')
+ end
end
- normalized.split(" ")
+ [normalized_first, normalized_last].map {|n| n.split(" ")}.flatten
end
def split_first_middle_last
View
204 spec/name_spec.rb
@@ -1,181 +1,47 @@
require 'human_name_parser/name'
-describe HumanNameParser::Name do
- # any unicode issues?
- context "when full name is Björn Charles van der O'Malley" do
- let(:full_name) { "Björn Charles van der O'Malley" }
- it "should parse the name" do
- @name = HumanNameParser::Name.new full_name
- @name.first.should == 'Björn'
- @name.last.should == "van der O'Malley"
- @name.middle.should == 'Charles'
- end
- end
-
- context "when full name is Marley Mante PhD" do
- let(:full_name) { "Marley Mante PhD" }
- it "should parse the name" do
- @name = HumanNameParser::Name.new full_name
- @name.first.should == 'Marley'
- @name.last.should == 'Mante'
- @name.middle.should == ''
- @name.suffix.should == 'PhD'
- end
- end
-
- context "when full name is Marley Mante" do
- let(:full_name) { "Marley Mante" }
- it "should parse the name" do
- @name = HumanNameParser::Name.new full_name
- @name.first.should == 'Marley'
- @name.last.should == 'Mante'
- @name.middle.should == ''
- @name.suffix.should == ''
- end
- end
-
- context "when full name is Marley" do
- let(:full_name) { "Marley" }
- it "should parse the name" do
- @name = HumanNameParser::Name.new full_name
- @name.first.should == 'Marley'
- @name.last.should == ''
- @name.middle.should == ''
- @name.suffix.should == ''
- end
- end
-
- context "when full name is Marley Mante, esq." do
- let(:full_name) { "Marley Mante, esq." }
- it "should parse the name" do
- @name = HumanNameParser::Name.new full_name
- @name.first.should == 'Marley'
- @name.last.should == 'Mante'
- @name.middle.should == ''
- @name.suffix.should == 'esq.'
- end
- end
-
- context 'when full name is Mary Lou Smith' do
- let(:full_name) { "Mary Lou Smith" }
- before do
- @name = HumanNameParser::Name.new full_name
- end
-
- it "gets first name" do
- @name.first.should == 'Mary'
- end
-
- it "gets last name" do
- @name.last.should == 'Smith'
- end
-
- it "gets middle" do
- @name.middle.should == 'Lou'
- end
-
- it "gets prefix" do
- @name.prefix.should == ''
- end
-
- it "gets suffix" do
- @name.suffix.should == ''
- end
-
- it "gets initials" do
- @name.initials.should == 'MLS'
- end
- end
-
- context 'when full name is Mr. Alphonse di Morel Jr. Esq.' do
- let(:full_name) { "Mr. Alphonse di Morel Jr. Esq." }
- before { @name = HumanNameParser::Name.new full_name }
-
- it "gets first" do
- @name.first.should == 'Alphonse'
- end
+NAMES = [
+ ['', ['', '', '', '', '', '']],
+ ['Downey, Robert Jr.', ['Robert', '', 'Downey', '', 'Jr.', 'RD']],
+ ['Mr. Alphonse di Morel Jr. Esq.', ['Alphonse', '', 'di Morel', 'Mr.', 'Jr. Esq.', 'AD']],
+ ['Mary Lou Smith', ['Mary', 'Lou', 'Smith', '', '', 'MLS']],
+ ['Marley', ['Marley', '', '', '', '', 'M']],
+ ['Marley Mante', ['Marley', '', 'Mante', '', '', 'MM']],
+ ['Marley Mante PhD', ['Marley', '', 'Mante', '', 'PhD', 'MM']],
+ ['Marley Mante, esq.', ['Marley', '', 'Mante', '', 'esq.', 'MM']],
+ ["Björn Charles van der O'Malley", ['Björn', 'Charles', "van der O'Malley", '', '', 'BCV']],
+ ['Alex Rothlenson-ben-Elsburghmohampton', ['Alex', '', 'Rothlenson-ben-Elsburghmohampton', '', '', 'AR']],
+ ['1234 Anywhere St., North Pole, SD 22323', ['', '', '', '', '', '']]
+]
- it "gets last" do
- @name.last.should == 'di Morel'
- end
-
- it "gets prefix" do
- @name.prefix.should == 'Mr.'
- end
-
- it "gets suffix" do
- @name.suffix.should == 'Jr. Esq.'
- end
-
- it "gets initials" do
- @name.initials.should == 'AD'
- end
- end
-
- context 'when full name is ROBOTO' do
- let(:full_name) { "ROBOTO" }
- before { @name = HumanNameParser::Name.new full_name }
-
- it "gets first" do
- @name.first.should == 'ROBOTO'
- end
-
- it 'gets initials' do
- @name.initials.should == 'R'
- end
-
- it "doesn't get last" do
- @name.last.should == ''
- end
- end
-
- context 'when full name is Downey Jr., Robert' do
- let(:full_name) { 'Downey Jr., Robert' }
- before { @name = HumanNameParser::Name.new full_name }
-
- it "gets first" do
- @name.first.should == 'Robert'
- end
-
- it 'gets initials' do
- @name.initials.should == 'RD'
- end
-
- it "gets last" do
- @name.last.should == 'Downey'
- end
-
- it "gets middle" do
- @name.middle.should == ''
- end
-
- it "gets suffix" do
- @name.suffix.should == 'Jr.'
- end
- end
+describe HumanNameParser::Name do
+ NAMES.each do |name_array|
+ context "when full name is #{ name_array.first.inspect }" do
+ before { @name = HumanNameParser::Name.new name_array.first }
- context 'when full name is garbage' do
- let(:full_name) { '1234 Anywhere St., North Pole, SD 22323' }
- before { @name = HumanNameParser::Name.new full_name }
+ it "gets first name" do
+ @name.first.should == name_array[1][0]
+ end
- it "gets first" do
- @name.first.should == ''
- end
+ it "gets middle name" do
+ @name.middle.should == name_array[1][1]
+ end
- it 'gets initials' do
- @name.initials.should == ''
- end
+ it "gets last name" do
+ @name.last.should == name_array[1][2]
+ end
- it "gets last" do
- @name.last.should == ''
- end
+ it "gets prefix" do
+ @name.prefix.should == name_array[1][3]
+ end
- it "gets middle" do
- @name.middle.should == ''
- end
+ it "gets suffix" do
+ @name.suffix.should == name_array[1][4]
+ end
- it "gets suffix" do
- @name.suffix.should == ''
+ it "gets initials" do
+ @name.initials.should == name_array[1][5]
+ end
end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.