# This is derived from code provided by the Django Project.
# Original code is licensed under the terms of the BSD license.
# See https://github.com/django/django/blob/master/LICENSE for full original license terms
import re
re_caps = re.compile(r'(?:^|(?<=[\.\?\!]))(^\s*|\s+)([a-z])')
def recapitalize(text):
"""Recapitalizes text, placing caps after end-of-sentence punctuation."""
text = text.lower()
text = re_caps.sub(lambda x: x.group(1) + x.group(2).upper(), text)
return text
if __name__ == "__main__":
print(recapitalize("first sentence. SECOND SENTENCE? tHiRd SeNtEnCe, "
"and it is still third sentence! FOURTH sentence."))
print(recapitalize("\r\nfirst sentence. second.sentence?\n"
"tHiRd;SeNtEnCe, and it is still third sentence!"
"\tFOURTH sentence."))
Diff to Previous Revision
--- revision 1 2013-12-25 15:11:06
+++ revision 2 2014-01-05 14:11:00
@@ -1,3 +1,6 @@
+# This is derived from code provided by the Django Project.
+# Original code is licensed under the terms of the BSD license.
+# See https://github.com/django/django/blob/master/LICENSE for full original license terms
import re
re_caps = re.compile(r'(?:^|(?<=[\.\?\!]))(^\s*|\s+)([a-z])')