python-nameparser icon indicating copy to clipboard operation
python-nameparser copied to clipboard

nickname getting output at end of name with no punctuation

Open jonathanmorgan opened this issue 10 years ago • 4 comments

When I parse the name Dr. Jason "Jay" Ross:

>>> from nameparser import HumanName
>>> test_name = 'Dr. Jason "Jay" Ross'
>>> test_hn = HumanName( test_name )

It is broken out like this:

>>> test_hn
<HumanName : [
        title: 'Dr.' 
        first: 'Jason' 
        middle: '' 
        last: 'Ross' 
        suffix: ''
        nickname: 'Jay'
]>

When I convert it to a string:

>>> str( test_hn )
'Dr. Jason Ross Jay'

...the nickname is positioned at the end of the string, so it looks like it is the last name and Ross is the middle name. In my code, I use the HumanName class as the canonical way to convert from name parts to name string, and that has worked well up until this. It is wrapped in a method call, and so for now I've just started removing any nickname before I convert to string. Is there another way to specify how you want the nickname to be treated in string output?

Thanks!

Jon Morgan

jonathanmorgan avatar Feb 11 '16 03:02 jonathanmorgan

There is some support for python string formatting which can dictate how the string is rendered: http://nameparser.readthedocs.org/en/latest/usage.html#string-format

But I think it probably won't help you because you'd need to conditionally add spaces when name parts are present vs missing and, afaik, you can't do that with python string formatting. Let me know if you can think of a nice solution, handling nicknames in the string format is sorta where I got stuck.

derek73 avatar Feb 11 '16 05:02 derek73

I'll think on it. You could allow for sets of format specifications that are applied based on which properties are present in the name part dictionary.

Make a little function that makes a six-digit string hash that captures which fields are present in the HumanName, with a digit for each name part, value of 0 if nothing in a given part, 1 if present. So for my example, using order "title", "first", "middle", "last", "suffix", "nickname", the value would be 110101.

Format specification would be a dictionary that maps specific values for this hash to format strings (maybe wild cards eventually, but I'm not sure the easy way to implement that in a dictionary).

Then, for a given conversion to string, if format specification present:

  • get the hash
  • look for a match in the dictionary
    • if one present, use it.
    • if not, just use the default.

I'll see if I can find time to try something like this in my own code, see how it works.

You could make it fancier later, but I think this would at least be flexible. Might not be the optimal implementation, but you get the idea, and dictionary lookups are pretty fast.

jonathanmorgan avatar Feb 11 '16 20:02 jonathanmorgan

So, I think I realized that you can actually do this already with string formatting, because it strips out spaces and trailing commas when you use a custom string format. Just include a key name for everything except nickname in the string_format attribute.

>>> name = HumanName("Dr. Juan Q. Xavier de la Vega III (Doc Vega)")
>>> name.string_format = "{title} {first} {middle} {last}, {suffix}"
>>> str(name)
'Dr. Juan Q. Xavier de la Vega, III'
>>> name.middle=""
>>> str(name)
'Dr. Juan de la Vega, III'
>>> name.title=""
>>> str(name)
'Juan de la Vega, III'
>>> name.suffix=""
>>> str(name)
'Juan de la Vega'

I guess one improvement might be to make it so that configuration is shared among all instances like the constants are.

derek73 avatar Mar 14 '16 21:03 derek73

The custom string format method now removes any empty parenthesis that might be included in a custom string_format parameter, and I added a way to configure it for all instances.

http://nameparser.readthedocs.org/en/latest/usage.html#change-the-output-string-with-string-formatting

>>> from nameparser.config import CONSTANTS
>>> CONSTANTS.string_format = "{title} {first} {last}"
>>> name = HumanName("Dr. Juan Ruiz de la Vega III (Doc Vega)")
>>> str(name)
'Dr. Juan de la Vega'

derek73 avatar Mar 15 '16 03:03 derek73