Beispiel #1
0
 def test_assignment_to_attribute(self):
     hn = HumanName("John A. Kenneth Doe, Jr.")
     hn.last = "de la Vega"
     self.m(hn.last,"de la Vega", hn)
     hn.title = "test"
     self.m(hn.title,"test", hn)
     hn.first = "test"
     self.m(hn.first,"test", hn)
     hn.middle = "test"
     self.m(hn.middle,"test", hn)
     hn.suffix = "test"
     self.m(hn.suffix,"test", hn)
 def test_formating_removing_pieces_from_name_buckets(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.string_format = "{title} {first} {middle} {last} {suffix}"
     assert u(hn) == "Rev John A. Kenneth Doe III"
     hn.middle = ""
     assert u(hn) == "Rev John Doe III"
     hn.suffix = ""
     assert u(hn) == "Rev John Doe"
     hn.title = ""
     assert u(hn) == "John Doe"
 def test_assign_list_to_attribute(self):
     hn = HumanName("John A. Kenneth Doe, Jr.")
     hn.title = ["test1", "test2"]
     assert hn.title == "test1 test2"
     hn.first = ["test3", "test4"]
     assert hn.first == "test3 test4"
     hn.middle = ["test5", "test6", "test7"]
     assert hn.middle == "test5 test6 test7"
     hn.last = ["test8", "test9", "test10"]
     assert hn.last == "test8 test9 test10"
     hn.suffix = ["test"]
     assert hn.suffix == "test"
Beispiel #4
0
 def from_parts(cls,
                first=None,
                last=None,
                middle=None,
                suffix=None,
                title=None):
     name = HumanName()
     name.first = first
     name.middle = middle
     name.last = last
     name.suffix = suffix
     name.title = title
     return ParsedName(name)
Beispiel #5
0
def normalize_names(names):
    res = []
    if names:
        for name in re.split('\s+and\s+|\s*&\s*|,\s+|\s*\+\s*', names):
            name = {
                'Simon': 'Simon Greenhill',
                'D. Mead': 'David Mead',
                'Alex François': 'Alexandre François',
                'Dr Alex François': 'Alexandre François',
                'R. Blust': 'Robert Blust',
            }.get(name, name)
            name = HumanName(name.title())
            res.append('{0} {1}'.format(name.first or name.title,
                                        name.last).strip())
    return ' and '.join(res)
 def test_assignment_to_attribute(self):
     hn = HumanName("John A. Kenneth Doe, Jr.")
     hn.last = "de la Vega"
     assert hn.last == "de la Vega"
     hn.title = "test"
     assert hn.title == "test"
     hn.first = "test"
     assert hn.first == "test"
     hn.middle = "test"
     assert hn.middle == "test"
     hn.suffix = "test"
     assert hn.suffix == "test"
     with pytest.raises(TypeError):
         hn.suffix = [["test"]]
     with pytest.raises(TypeError):
         hn.suffix = {"test": "test"}
Beispiel #7
0
    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
Beispiel #8
0
    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 parse_persname(persname, auth="", source=""):
    name, birth_date, death_date = extract_birth_death_dates(persname)
    birth_date, death_date = validate_dates(birth_date, death_date)
    dates_string = make_date_string(birth_date, death_date)
    name = HumanName(name)

    titles = [
        "sir", "mr", "mrs", "baron", "dame", "madame", "viscount", "conte"
    ]
    numbers = ["II", "III"]
    title = name.title
    suffix = name.suffix
    number = u""

    # check if the suffix should actually be a title
    if not title and any(suffix.lower().strip(". ") == title
                         for title in titles):
        title = suffix.capitalize()
        if "mr" in title.lower() and not title.endswith("."):
            title += "."
        suffix = u""

    # extract numbers from the suffix
    if suffix in numbers:
        number = suffix
        suffix = u""

    # special cases cleanup
    if name.title == u"Royal":
        name.title = ""
        title = ""
        name.middle = name.first if not name.middle else "{} {}".format(
            u"Royal", name.middle)
        name.first = u"Royal"

    if name.title == u"Queen of Great":
        title = name.title + u" Britain"
        name.first = u""

    if name.title == u"Lama":
        title = u"Dalai Lama XIV"
        name.first = u""
        name.middle = u""

    if name.title == u"Marquis":
        title = u""
        name.first = u"Marquis"
        name.middle = u"W."

    if suffix == u"1941":
        birth_date = suffix
        suffix = u""

    if suffix in [u"18", u"b."]:
        suffix = u""

    if suffix == u"Jr":
        suffix += u"."

    if ", fl. 17th cent" in suffix:
        suffix = u"sieur de"
        dates_string = u"fl. 17th cent"

    rest_of_name = u"{0} {1}".format(name.first, name.middle).rstrip()
    if rest_of_name == u"Christella D. Personal journey through South Africa. 1991":
        rest_of_name = u"Christella D."

    # People with single-part names (like Keewaydinoquay) are mis-assigned. Have to fix those
    primary_name = name.last
    if rest_of_name and not primary_name:
        primary_name = rest_of_name
        rest_of_name = ""

    # create the parsed name dictionary
    name_parsed = {
        u"title": unicode(title),
        u"primary_name": unicode(primary_name),
        u"rest_of_name": rest_of_name,
        u"suffix": unicode(suffix),
        u"fuller_form": unicode(name.nickname),
        u"numbers": unicode(number),
        u"birth_date": unicode(birth_date),
        u"death_date": unicode(death_date),
        u"date_string": unicode(dates_string),
        u"authority_id": unicode(auth),
        u"source": unicode(source),
        u"name_order": u"inverted",
        u"sort_name_auto_generate": True
    }

    # remove empty fields
    for key, value in name_parsed.items():
        if not value:
            del name_parsed[key]

    return name_parsed
def parse_persname(persname, auth="", source=""):
    name, birth_date, death_date = extract_birth_death_dates(persname)
    birth_date, death_date = validate_dates(birth_date, death_date)
    dates_string = make_date_string(birth_date, death_date)
    name = HumanName(name)

    titles = ["sir", "mr", "mrs", "baron", "dame", "madame", "viscount", "conte"]
    numbers = ["II", "III"]
    title = name.title
    suffix = name.suffix
    number = u""

    # check if the suffix should actually be a title
    if not title and any(suffix.lower().strip(". ") == title for title in titles):
        title = suffix.capitalize()
        if "mr" in title.lower() and not title.endswith("."):
            title += "."
        suffix = u""

    # extract numbers from the suffix
    if suffix in numbers:
        number = suffix
        suffix = u""

    # special cases cleanup
    if name.title == u"Royal":
        name.title = ""
        title = ""
        name.middle = name.first if not name.middle else "{} {}".format(u"Royal", name.middle)
        name.first = u"Royal"

    if name.title == u"Queen of Great":
        title = name.title + u" Britain"
        name.first = u""

    if name.title == u"Lama":
        title = u"Dalai Lama XIV"
        name.first = u""
        name.middle = u""

    if name.title == u"Marquis":
        title = u""
        name.first = u"Marquis"
        name.middle = u"W."

    if suffix == u"1941":
        birth_date = suffix
        suffix = u""

    if suffix in [u"18", u"b."]:
        suffix = u""

    if suffix == u"Jr":
        suffix += u"."

    if ", fl. 17th cent" in suffix:
        suffix = u"sieur de"
        dates_string = u"fl. 17th cent"

    rest_of_name = u"{0} {1}".format(name.first, name.middle).rstrip()
    if rest_of_name == u"Christella D. Personal journey through South Africa. 1991":
        rest_of_name = u"Christella D."

    # People with single-part names (like Keewaydinoquay) are mis-assigned. Have to fix those
    primary_name = name.last
    if rest_of_name and not primary_name:
        primary_name = rest_of_name
        rest_of_name = ""

    # create the parsed name dictionary
    name_parsed = {u"title": unicode(title),
                   u"primary_name": unicode(primary_name),
                   u"rest_of_name": rest_of_name,
                   u"suffix": unicode(suffix),
                   u"fuller_form": unicode(name.nickname),
                   u"numbers": unicode(number),
                   u"birth_date": unicode(birth_date),
                   u"death_date": unicode(death_date),
                   u"date_string": unicode(dates_string),
                   u"authority_id": unicode(auth),
                   u"source": unicode(source),
                   u"name_order": u"inverted",
                   u"sort_name_auto_generate": True}

    # remove empty fields
    for key, value in name_parsed.items():
        if not value:
            del name_parsed[key]

    return name_parsed
    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 --#