Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate firstname and lastname from fullname string in C#

Tags:

string

c#

regex

I'm doing a website migration that involves extracting firstname and lastname from fullname. Given these were created by the end user, all kinds of permutations exist (although English and generally not too strange). Mostly I can take the first word as firstname and the last word as the lastname but have some exceptions from the occasional prefix and suffix. In going through the data and trying to get my head around all the likely exceptions I realized that this is a common problem that has been at least partially solved many times before.

Before reinventing the wheel, does anyone have any regular expressions that have worked for them or useful code? Performance is not a consideration as this is a one-time utility.

Typical values to be handled:

Jason Briggs, J.D. Smith, John Y Citizen, J Scott Myers, Bill Jackobson III, Mr. John Mills


Update: while a common problem, the typical solution seems to involve handling the majority of cases and manually cleaning the rest.

(Given the frequency this issue must be experienced I was originally expecting to find a utility library out there but was not able to find one myself with Google)

like image 514
Stuart Avatar asked Aug 02 '09 15:08

Stuart


4 Answers

My recommendation would be the following:

  1. Split the names on the spaces.

  2. Check the length of the returned array. If 2, easy split. If more, next.

  3. Compare the 1st value for prefixes (i.e. Mr. Mrs. Ms. Dr.)...if so, remove it else move to next.

  4. Compare the 1st value for length. If it's just 1 character, combine first 2 items in the array.

It's still not fool proof; however, it should address at least 80 per cent of your cases.

Hope this helps.

like image 184
JamesEggers Avatar answered Sep 26 '22 17:09

JamesEggers


It's probably impossible to do (reliably).

Even if you can do that for some names, you will get a Spanish person at some point, who will write down both family names. Or some people (forgot which nationality it is) that will put in "lastname firstname". Or one of many other situations...

The best you can probably do is split 2 words as first and last name, then go through the rest manually (yourself, or hire some professionals)...

like image 43
viraptor Avatar answered Sep 26 '22 17:09

viraptor


The fastest thing to do is a hybrid algorithm-human approach. You don't want to spend the time putting together a system that works 99.99% of the time because the last 5-10% of optimization will kill you. Also, you don't want to just dump all of the work on a person because most of the cases (I'm guessing) are fairly straightforward.

So, rapidly build something like what JamesEggers suggested, but catch all of the cases that appear unusual or do not fit your predefined conversions. Then, simply go through those cases manually (It shouldn't be too many).

You could go through those cases by yourself or outsource them to other users by setting up HITs in Mechanical Turk:

http://aws.amazon.com/mturk/

(Assuming 500 cases at $0.05 (high reward) your total cost should be $25 at most)

like image 28
Robert Venables Avatar answered Sep 23 '22 17:09

Robert Venables


If this is a one shot deal then I would strongly consider paying someone else who is a specialist to do it for you.

They will be experienced in working with poorly structured data sets.

I have no affiliation with them but Melissa Data provide a service that seems tailored to this sort of thing.

like image 43
ShuggyCoUk Avatar answered Sep 24 '22 17:09

ShuggyCoUk