Skip to content

Instantly share code, notes, and snippets.

@Turin86
Last active August 29, 2015 14:23
Show Gist options
  • Save Turin86/e67f2f491383b30fa0dd to your computer and use it in GitHub Desktop.
Save Turin86/e67f2f491383b30fa0dd to your computer and use it in GitHub Desktop.
Utility function to properly capitalize names
def capnames(line):
if isinstance(line, basestring):
was_string = isinstance(line, str)
if was_string:
line = line.decode('utf-8')
line = line.lower() \
.replace(u'otoole', u'o\'toole') \
.replace(u'\\', u'')
line = u'-'.join(_.strip() for _ in line.split(u'-'))
line = u"'".join(_.strip() for _ in line.split(u"'"))
line = u'ª '.join(_.strip() for _ in line.split(u'ª'))
line = u'º '.join(_.strip() for _ in line.split(u'º'))
line = u', '.join(_.strip() for _ in line.split(u','))
line = u'. '.join(_.strip() for _ in line.split(u'.'))
names = line.split()
for i in range(len(names)):
if names[i] not in (u'de', u'del', u'el', u'la', u'las',
u'los', u'do', u'von', u'van', u'der', u'ben', u'ibn', u'ebn',
u'bin', u'of', u'y', u'i', u'eta', u'and', u'und', u'du', u'dos',
u'les', u'da', u'den', u'ten', u'zu', u'z', u'af', u'av', u'ver',
u'op', u'della', u'di', u'lo', u'zum', u'vor', u'dem', u'auf',
u'als', u'sich', u'ein', u'mit', u'degli', u'zur'):
for special_char in (u'.', u',', u'-', u"'"):
name_parts = names[i].split(special_char)
for j in range(len(name_parts)):
if len(name_parts[j]) > 0:
name_parts[j] = name_parts[j][0].upper() + name_parts[j][1:]
for prefix in (u'Mc',):
if name_parts[j].startswith(prefix):
name_parts[j] = prefix + name_parts[j][len(prefix)].upper() + name_parts[j][(len(prefix)+1):]
names[i] = special_char.join(name_parts)
line = u' '.join(names)
if was_string:
line = line.encode('utf-8')
return line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment