tags:

views:

125

answers:

6

Is there a standard or simple way to guess a name from an email address, similar to what gmail does?

For example, "[email protected]" should give "John Smith".

Doing this shouldn't be too hard (strip domain name, remove special characters, capitalize, etc), but I'm sure there should be existing code for this.

Code in Ruby would be preferred, but any other language would be fine.

A: 

Regular expressions are probably the way to go.

James Allen
not a lot of help in that answer
glenn jackman
+1  A: 

The regex below should solve your problem

/(\w+)[._-](\w+)@.+/
Piotr Czapla
I've seen addresses like [email protected], so your re will capture 'smith' and 'jr' in this example.
glenn jackman
A: 

And a very large dictionary of common first and last names, because not all email addresses will relate so nicely to actual names.

This is likely to work a lot better if all your mail is from one country or culture.

I do have to wonder why you want to do this, though.

Joe Mabel
For the website I'm working on, I want to use this when user hasn't specified a name yet.
Ralf
In that case, I would rather use just the email address minus the domain.com.
Raj More
A: 
def email_to_name(email)
  name = email[/[^@]+/]
  name.split(".").map {|n| n.capitalize }.join(" ")
end

p email_to_name("[email protected]")
# => "John Smith"

This is such a simple task that I doubt you'll find any "existing code" doing this.

August Lilleaas
I just thought there might be many special cases, and someone might have come up with heuristics to handle it. I'll start with this code and adapt it.
Ralf
+1  A: 

Gmail uses the envelope style email address one of the extensions to rfc-822. So it only guesses if the email address is in envolope form like this: Terry Terribad <[email protected]>.

For gmail it's then just a case of trying to figure out what goes in the front of the <> through parsing the email and generally guessing.

Otherwise there really is no way to guess that my name would be Chuck Vose from my email address as I don't use chuck.vose or anything like that.

Chuck Vose
yes, this is the right answer, google doesnt make any guesses it reads what was sent, all email providers (almost) append the name to the email address
Ayyash
When the name is not present, gmail uses everything before the @domain, so it would be "terry" if it was only "[email protected]".
Ralf
A: 

This will be funny when you guess the name and it is way off because the user doesn't use their name in their email address. Like the one guy said, you should have a dictionary of common names and you only do the guess if the words you extracted (using the regular expression) matches a name in the dictionary. However, it might not be a bad idea to have it populate someone's name that is completely wrong and absurd b/c then it could be motivation for them to change it to their real name. :-)

Brian T. Hannan