def sort_name_to_display_name(sort_name): """ Take the "Last Name, First Name"-formatted sort_name, and convert it to a "First Name Last Name" format appropriate for displaying to patrons in a catalog listing. While the code attempts to do the best it can, name recognition gets complicated really fast when there's more than one plain-format first name and one plain-format last name. This code is meant to serve as first line of approximation. If we later on can find better human librarian-checked sort and display names in the Metadata Wrangler, we use those. :param sort_name Doe, Jane :return display_name Jane Doe """ if not sort_name: return None name = HumanName(sort_name) # name has title, first, middle, last, suffix, nickname if name.nickname: name.nickname = '(' + name.nickname + ')' display_name = u' '.join([name.title, name.first, name.nickname, name.middle, name.last, name.suffix]) display_name = name_tidy(display_name) return display_name
def display_name_to_sort_name(display_name): """ Take the "First Name Last Name"-formatted display_name, and convert it to a "Last Name, First Name" format appropriate for searching and sorting by. Checks first if the display_name fits what we know of corporate entity business names. If yes, uses the whole name without re-converting it. Uses the HumanName library to try to parse the name into parts, and rearrange the parts into desired order and format. """ if not display_name: return None # TODO: to humanname: PhD, Ph.D. Sister, Queen are titles and suffixes # check if corporate, and if yes, return whole if is_corporate_name(display_name): return display_name # clean up the common PhD and MD suffixes, so HumanName recognizes them better display_name = name_tidy(display_name) # name has title, first, middle, last, suffix, nickname name = HumanName(display_name) if name.nickname: name.nickname = '(' + name.nickname + ')' # Note: When the first and middle names are initials that have come in with a space between them, # let them keep that space, to be consistent with initials with no periods, which would be more # easily algorithm-recognized if they were placed separately. So: # 'Classy, A. B.' and 'Classy Abe B.' and 'Classy A. Barney' and 'Classy, Abe Barney' and 'Classy, A B'. if not name.last: # Examples: 'Pope Francis', 'Prince'. sort_name = u' '.join( [name.first, name.middle, name.suffix, name.nickname]) if name.title: sort_name = u''.join([name.title, ", ", sort_name]) else: sort_name = u' '.join( [name.first, name.middle, name.suffix, name.nickname, name.title]) sort_name = u''.join([name.last, ", ", sort_name]) sort_name = name_tidy(sort_name) return sort_name
def display_name_to_sort_name(display_name): """ Take the "First Name Last Name"-formatted display_name, and convert it to a "Last Name, First Name" format appropriate for searching and sorting by. Checks first if the display_name fits what we know of corporate entity business names. If yes, uses the whole name without re-converting it. Uses the HumanName library to try to parse the name into parts, and rearrange the parts into desired order and format. """ if not display_name: return None # TODO: to humanname: PhD, Ph.D. Sister, Queen are titles and suffixes # check if corporate, and if yes, return whole if is_corporate_name(display_name): return display_name # clean up the common PhD and MD suffixes, so HumanName recognizes them better display_name = name_tidy(display_name) # name has title, first, middle, last, suffix, nickname name = HumanName(display_name) if name.nickname: name.nickname = '(' + name.nickname + ')' # Note: When the first and middle names are initials that have come in with a space between them, # let them keep that space, to be consistent with initials with no periods, which would be more # easily algorithm-recognized if they were placed separately. So: # 'Classy, A. B.' and 'Classy Abe B.' and 'Classy A. Barney' and 'Classy, Abe Barney' and 'Classy, A B'. if not name.last: # Examples: 'Pope Francis', 'Prince'. sort_name = u' '.join([name.first, name.middle, name.suffix, name.nickname]) if name.title: sort_name = u''.join([name.title, ", ", sort_name]) else: sort_name = u' '.join([name.first, name.middle, name.suffix, name.nickname, name.title]) sort_name = u''.join([name.last, ", ", sort_name]) sort_name = name_tidy(sort_name) return sort_name
def HumanNameFmXML(self, ell): hn = HumanName() for el in ell: if el.tag == 'First': hn.first = el.text elif el.tag == 'Middle': hn.middle = el.text elif el.tag == 'Last': hn.last = el.text elif el.tag == 'Title': hn.title = el.text elif el.tag == 'Suffix': hn.suffix = el.text elif el.tag == 'NickName': hn.nickname = el.text else: pass return hn
def person_name_from_xml(self, ell): '''Create a person mane from an XML element.''' hname = HumanName() for elm in ell: if elm.tag == 'First': hname.first = elm.text elif elm.tag == 'Middle': hname.middle = elm.text elif elm.tag == 'Last': hname.last = elm.text elif elm.tag == 'Title': hname.title = elm.text elif elm.tag == 'Suffix': hname.suffix = elm.text elif elm.tag == 'NickName': hname.nickname = elm.text else: pass return hname
def to_HumanName( self ): ''' This method creates a nameparser HumanName() object instance for the Person name property values in this instance. Returns the HumanName instance. preconditions: None. postconditions: None. ''' # return reference instance_OUT = None # declare variables me = "to_HumanName" my_name_prefix = "" my_first_name = "" my_middle_name = "" my_last_name = "" my_name_suffix = "" my_nickname = "" my_full_name_string = "" my_lookup_name = "" got_name_parts = False # retrieve values from this instance my_name_prefix = self.get( self.PROP_NAME_NAME_PREFIX, None ) my_first_name = self.get( self.PROP_NAME_FIRST_NAME, None ) my_middle_name = self.get( self.PROP_NAME_MIDDLE_NAME, None ) my_last_name = self.get( self.PROP_NAME_LAST_NAME, None ) my_name_suffix = self.get( self.PROP_NAME_NAME_SUFFIX, None ) my_nickname = self.get( self.PROP_NAME_NICKNAME, None ) my_full_name_string = self.get( self.PROP_NAME_FULL_NAME_STRING, None ) my_lookup_name = self.get_lookup_name() # got name parts? got_name_parts = self.got_name_parts() if ( got_name_parts == True ): # build human name from name parts. instance_OUT = HumanName() # Use nested values to populate HumanName. if ( my_name_prefix ): instance_OUT.title = my_name_prefix #-- END check to see if name_prefix. --# if ( my_first_name ): instance_OUT.first = my_first_name #-- END check to see if first_name. --# if ( my_middle_name ): instance_OUT.middle = my_middle_name #-- END check to see if middle_name. --# if ( my_last_name ): instance_OUT.last = my_last_name #-- END check to see if last_name. --# if ( my_name_suffix ): instance_OUT.suffix = my_name_suffix #-- END check to see if name_suffix. --# if ( my_nickname ): instance_OUT.nickname = my_nickname #-- END check to see if nickname. --# # got full name string? elif ( ( my_full_name_string is not None ) and ( my_full_name_string != "" ) ): # yes. Pass it to HumanName instance_OUT = HumanName( my_full_name_string ) # how about lookup name? elif ( ( my_lookup_name is not None ) and ( my_lookup_name != "" ) ): # yes. Pass it to HumanName instance_OUT = HumanName( my_lookup_name ) else: # no names present at all. Return None. instance_OUT = None #-- END check to see what name information we have --# return instance_OUT #-- END method to_HumanName() --# #-- END class PersonDetails --#
def test_formating_of_nicknames_in_middle(self): hn = HumanName("Rev John A. Kenneth Doe III (Kenny)") hn.string_format = "{title} {first} ({nickname}) {middle} {last} {suffix}" assert u(hn) == "Rev John (Kenny) A. Kenneth Doe III" hn.nickname = "" assert u(hn) == "Rev John A. Kenneth Doe III"
def test_formating_of_nicknames_with_double_quotes(self): hn = HumanName("Rev John A. Kenneth Doe III (Kenny)") hn.string_format = '{title} {first} {middle} {last} {suffix} "{nickname}"' assert u(hn) == 'Rev John A. Kenneth Doe III "Kenny"' hn.nickname = "" assert u(hn) == "Rev John A. Kenneth Doe III"
def test_formating_of_nicknames_with_single_quotes(self): hn = HumanName("Rev John A. Kenneth Doe III (Kenny)") hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'" assert u(hn) == "Rev John A. Kenneth Doe III 'Kenny'" hn.nickname = "" assert u(hn) == "Rev John A. Kenneth Doe III"
def test_formating_of_nicknames_with_parenthesis(self): hn = HumanName("Rev John A. Kenneth Doe III (Kenny)") hn.string_format = "{title} {first} {middle} {last} {suffix} ({nickname})" assert u(hn) == "Rev John A. Kenneth Doe III (Kenny)" hn.nickname = "" assert u(hn) == "Rev John A. Kenneth Doe III"