By Erich's request, here is a version of my previous function where the apostrophe is only special cased when in the middle of a word, so that it can still used as quotes. This usage, although syntactically incorrect now that we have Unicode, is very popular.
El sáb, 03-11-2007 a las 17:46 +0100, Erich Schubert escribió:
> Javier mentioned that some languages usually don't uppercase everything
> - well, these users just shouldn't use the Title-case function. There is
> no way to reliably Titlecase personal names automatically.
> They might want a "uppercase first letter" option though.
That's exactly why I mentioned it. Some people love to uppercase
everything in any language and I usually find myself fixing it by hand.
Ignoring proper names, the conversion is trivial ("s[0].upper() +
s[1:].lower()" will do it), but I was a bit lazy to actually write the
plug-in...
> I didn't read any of the unicode standards. Does it actually suggest to
> substitute it with SS, or is it just for displaying?
To be honest, I haven't read them either. The file I mentioned also
contains uppercase mapping for lowercase Latin ligatures, Greek and
Turkish letters, among others. I seem to recall reading that the case
with the dotless Turkish i is a common problem, because its
transformation conflicts with the ASCII mapping i -> I and I -> i
(apparently in Turkish it's uc(i) == İ and lc(I) == ı), so you have to
rely on the locale to know what to do.
In anyway, I don't think that we need to go out of our way to handle
that. Python should respect the environment's locale, and if not, it
should be fixed there.
Greetings,
--
Javier Kohen <[EMAIL PROTECTED]>
ICQ: blashyrkh #2361802
Jabber: [EMAIL PROTECTED]
#!/usr/bin/python
# -*- coding: utf-8 -*-
import unicodedata
def iswbound(char):
"""Returns whether the given character is a word boundary."""
category = unicodedata.category(char)
# If it's a space separator or punctuation
return 'Zs' == category or 'P' == category[0]
def utitle(string):
"""Title-case a string using a less destructive method than str.title."""
new_string = string[0].capitalize()
cap = False
for i in xrange(1, len(string)):
s = string[i]
# Special case apostrophe in the middle of a word.
if u"'" == s and string[i-1].isalpha(): cap = False
elif iswbound(s): cap = True
elif cap and s.isalpha():
cap = False
s = s.capitalize()
else: cap = False
new_string += s
return new_string
from types import UnicodeType
from locale import getpreferredencoding
def title(string):
"""Title-case a string using a less destructive method than str.title."""
if not string: return ""
if (not isinstance(string, UnicodeType)):
string = unicode(string.decode(getpreferredencoding()))
return utitle(string)
assert u"Mama's Boy" == title(u"mama's boy")
# This character is not an apostrophe, it's a single quote!
assert u"Mama’S Boy" == title(u"mama’s boy")
assert u"The A-Sides" == title(u"the a-sides")
assert u"Hello Goodbye" == title(u"hello goodbye")
assert u"HELLO GOODBYE" == title(u"HELLO GOODBYE")
assert u"Hello Goodbye (A Song)" == title(u"hello goodbye (a song)")
assert u"Hello Goodbye 'A Song'" == title(u"hello goodbye 'a song'")
assert u"Hello Goodbye \"A Song\"" == title(u"hello goodbye \"a song\"")
assert u"Hello Goodbye „A Song”" == title(u"hello goodbye „a song”")
assert u"Hello Goodbye ‘A Song’" == title(u"hello goodbye ‘a song’")
assert u"Hello Goodbye “A Song”" == title(u"hello goodbye “a song”")
assert u"Hello Goodbye »A Song«" == title(u"hello goodbye »a song«")
assert u"Hello Goodbye «A Song»" == title(u"hello goodbye «a song»")
assert u"Fooäbar" == title(u"fooäbar")
assert u"Los Años Felices" == title(u"los años felices")
assert u"Ñandú" == title(u"ñandú")
# Not a real word, but still Python doesn't capitalize the es-zed properly.
#assert u"SSbahn" == title(u"ßbahn")
assert u"Fooäbar" == title("fooäbar")
assert u"Los Años Felices" == title("los años felices")
assert u"Ñandú" == title("ñandú")
signature.asc
Description: Esta parte del mensaje está firmada digitalmente

