Parse Full Names with Ruby
May 31st, 2009
We do a lot of data migrations for Donor Tools – a lot of folks are coming to us from other systems, and they need their data ported over. Depending on the kind of system they were using, this can be anywhere from a quick script to a major data transformation headache.
Recently I needed a quick way to parse a full name string into name parts. Given a name like “Dr. Joe Donor, M.D.”, I wanted to end up with a name object with a prefix of “Dr.”, a suffix of “M.D.”, a first name of “Joe”, and a last name of “Donor”. Complicating matters, it also needed to be able to handle odd permutations like “Dr. and Mrs. Joe and Jane Donor”, etc.
The main problem that I had was with the “and”. If there was an “and”, it should put the preceding and following words together to form a single word. This turned out to be nearly impossible with regular expressions, but pretty easy with a combination of Ruby and regex. Here’s how it looks.
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 |
class Name < ActiveRecord::Base def self.parse(name) return false unless name.is_a?(String) # First, split the name into an array parts = name.split # If any part is "and", then put together the two parts around it # For example, "Mr. and Mrs." or "Mickey and Minnie" parts.each_with_index do |part, i| if ["and", "&"].include?(part) and i > 0 p3 = parts.delete_at(i+1) p2 = parts.at(i) p1 = parts.delete_at(i-1) parts[i-1] = [p1, p2, p3].join(" ") end end # Build a hash of the remaining parts { :suffix => (s = parts.pop unless parts.last !~ /(\w+\.|[IVXLM]+|[A-Z]+)$/), :last_name => (l = parts.pop), :prefix => (p = parts.shift unless parts[0] !~ /^\w+\./), :first_name => (f = parts.shift), :middle_name => (m = parts.join(" ")) } end end |
Here’s the output:
1 2 3 4 5 6 7 8 9 10 11 |
Name.parse "Mr. Joe Donor" => {:middle_name=>"", :prefix=>"Mr.", :last_name=>"Donor", :suffix=>nil, :first_name=>"Joe"} Name.parse "Dr. and Mrs. Joe Donor, M.D." => {:middle_name=>"", :prefix=>"Dr. and Mrs.", :last_name=>"Donor,", :suffix=>"M.D.", :first_name=>"Joe"} Name.parse "Joe and Jane Donor" => {:middle_name=>"", :prefix=>nil, :last_name=>"Donor", :suffix=>nil, :first_name=>"Joe and Jane"} Name.parse "Joe and Jane Major-Donor" => {:middle_name=>"", :prefix=>nil, :last_name=>"Major-Donor", :suffix=>nil, :first_name=>"Joe and Jane"} |
Gist First Impressions
May 6th, 2009
After publicly swooning over Gist, I finally got my Gist invite yesterday! What a treat! I wanted to share a few quick first impressions.
First impression is that this is really, really cool. I put in a couple key bits of information, and it started sucking down my friends and followers (doesn’t that make me sound cool). After a while it had imported all my contacts, and put together a sort of aggregate “wall” for each.
Since many of my contacts were from Twitter, many have just their tweets in their contact wall. For example, I follow Guy Kawasaki, and since he doesn’t know I exist all I have on his wall is his prolific list of recent tweets. This is minorly useful, but I think it will start to get a lot more informative after I hook in more services and provide more detail about my contacts. However, for a few of my contacts I have a lot more information, such as rss feeds and such, which makes their contact record bristle with information.
One of the things I’m hoping for is Apple Mail integration. Currently they have an Outlook plugin, which looks promising but doesn’t work for me as a Mac user. Also, I had a bit of trouble connecting to my Google Apps account. Might have been a PBKAC (Problem Between Keyboard And Chair) error though.
All in all I’m very impressed. They’ve got an awesome service and I can’t wait to see how it develops.
Donor Tools is Growing
May 5th, 2009
This is an exciting time for Donor Tools! We’re growing, and I’m so pleased to welcome Chris Dumas to the Donor Tools team!
Chris is joining Donor Tools to head up business development. Chris has a tremendous amount of positive energy, and he knows his stuff. This is an exciting time at Donor Tools. We’re growing, momentum is building, and we’re excited about the new things that we have to offer in the coming months.
Read Chris’ remarks over on the Donor Tools blog →
Welcome Chris! It’s great to have you on board!
Gist
May 5th, 2009
I am itching to try Gist. The idea, as I understand it, is that Gist gives you a web’s-eye view of the people who you email with. So you could see blog articles, tweets (maybe?), news, etc. about the person who just sent you a message. Pretty idea.
I haven’t gotten my invite yet, but I’m waiting with baited breath (hint hint). I’ll be sure to let you all know how it goes if I get an early invite to the beta.
Subscribe
Network
Archives
- July 2009 (1)
- May 2009 (4)
- March 2009 (1)
- February 2009 (1)
- December 2008 (2)
- November 2008 (1)
- October 2008 (7)
- September 2008 (4)
- August 2008 (4)
- June 2008 (2)
- April 2008 (1)
- March 2008 (1)
- February 2008 (5)
- January 2008 (1)
- December 2007 (1)
- November 2007 (4)
- October 2007 (1)
- September 2007 (1)
- August 2007 (3)
- July 2007 (2)
- June 2007 (5)
- May 2007 (6)
- April 2007 (3)
- March 2007 (4)
- February 2007 (5)
- January 2007 (5)
- December 2006 (6)
- November 2006 (1)
- October 2006 (8)
- September 2006 (12)
- August 2006 (7)
- July 2006 (6)
- June 2006 (6)
- May 2006 (2)
- April 2006 (17)
- March 2006 (15)
- February 2006 (7)
- January 2006 (12)
- December 2005 (25)
- November 2005 (11)
- October 2005 (3)
- September 2005 (2)
- August 2005 (4)
- July 2005 (5)
- June 2005 (1)