Last active
August 29, 2015 14:06
-
-
Save bbengfort/a4baa4752b17ae7e3e03 to your computer and use it in GitHub Desktop.
In class example for Georgetown Data Analytics (Software Engineering) Cohort 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Code for class | |
""" | |
tooshort = ["a", "an", "the", "at", "by", "for", "in", "of", "on", "to", | |
"up", "and", "as", "but", "it", "or", "nor"] | |
def apa_title(text): | |
""" | |
This does an APA acceptable title case of a string by ensuring that | |
short words remain lowercase unless at the beginning of a sentene and | |
by moving articles to the front of the tile. | |
Arguments: text - the text of a title to title case correctly. | |
""" | |
text = text.lower() | |
words = text.split() | |
# Moving the article from the end MUST happen before capitializaton | |
# Otherwise the article may accidentally remain lowercase. | |
if words[-2].endswith(",") and words[-1] in ("the", "an", 'a'): | |
words.insert(0, words.pop()) | |
words[-1] = words[-1].replace(",", "") | |
# We have to track position to modify the words list and also to ensure | |
# that short words at the beginning of the sentence get capitialized. | |
for idx, word in enumerate(words): | |
print word | |
if word in tooshort and idx > 2: | |
continue | |
words[idx] = word.title() | |
print words[idx] | |
return " ".join(words) | |
if __name__ == '__main__': | |
print apa_title("time for sleeping, a") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tooshort = ["a", "an", "the", "at", "by", "for", "in", "of", "on", "to", | |
"up", "and", "as", "but", "it", "or", "nor"] | |
def printli(*args): | |
print "%i: %r" % args | |
def apa_title(text): | |
printli(8, text) | |
text = text.lower() | |
printli(10, text) | |
words = text.split() | |
printli(12, words) | |
printli(19, words[-2]) | |
printli(19, words[-2].endswith) | |
printli(19, words[-1]) | |
if words[-2].endswith(",") and words[-1] in ("the", "an", 'a'): | |
words.insert(0, words.pop()) | |
printli(20, words) | |
words[-1] = words[-1].replace(",", "") | |
printli(22, words) | |
for idx, word in enumerate(words): | |
printli(25, idx) | |
printli(25, word) | |
if word in tooshort and idx > 0: | |
printli(28, "continuing") | |
continue | |
words[idx] = word.title() | |
printli(31, words) | |
val = " ".join(words) | |
printli(36, val) | |
return val | |
if __name__ == '__main__': | |
print apa_title("the continuing STORY of a bad spelr") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment