'Title case with a list of exception words
Im trying to come up with something that will "title" a string of words. It should capitalize all words in the string unless given words not to capitalize as an argument. But will still capitalize the first word no matter what. I know how to capitalize every word, but I dont know how to not capitalize the exceptions. Kind of lost on where to start, couldnt find much on google.
def titlemaker(title, exceptions):
return ' '.join(x[0].upper() + x[1:] for x in title.split(' '))
or
return title.title()
but I found that will capitalize a letter after an apostrophe so I dont think I should use it. Any help on how I should take into account the exceptions would be nice
example: titlemaker('a man and his dog', 'a and') should return 'A Man and His Dog'
Solution 1:[1]
def titlemaker(title,exceptions):
exceptions = exceptions.split(' ')
return ' '.join(x.title() if nm==0 or not x in exceptions else x for nm,x in enumerate(title.split(' ')))
titlemaker('a man and his dog','a and') # returns "A Man and His Dog"
The above assumes that the input string and the list of exceptions are in the same case (as they are in your example), but would fail on something like `titlemaker('a man And his dog','a and'). If they could be in mixed case do,
def titlemaker(title,exceptions):
exceptionsl = [x.lower() for x in exceptions.split(' ')]
return ' '.join(x.title() if nm==0 or not x.lower() in exceptions else x.lower() for nm,x in enumerate(title.split(' ')))
titlemaker('a man and his dog','a and') # returns "A Man and His Dog"
titlemaker('a man AND his dog','a and') # returns "A Man and His Dog"
titlemaker('A Man And His DOG','a and') # returns "A Man and His Dog"
Solution 2:[2]
Try with this:
def titleize(text, exceptions):
exceptions = exceptions.split()
text = text.split()
# Capitalize every word that is not on "exceptions" list
for i, word in enumerate(text):
text[i] = word.title() if word not in exceptions or i == 0 else word
# Capitalize first word no matter what
return ' '.join(text)
print titleize('a man and his dog', 'a and')
Output:
A Man and His Dog
Solution 3:[3]
def titleize(text, exceptions):
return ' '.join([word if word in exceptions else word.title()
for word in text.split()]).capitalize()
Solution 4:[4]
import re
import nltk
from nltk.corpus import stopwords
from itertools import chain
def setTitleCase(title):
exceptions = []
exceptions.append([word for word in stopwords.words('english')])
exceptions.append([word for word in stopwords.words('portuguese')])
exceptions.append([word for word in stopwords.words('spanish')])
exceptions.append([word for word in stopwords.words('french')])
exceptions.append([word for word in stopwords.words('german')])
exceptions = list(chain.from_iterable(exceptions))
list_of_words = re.split(' ', title)
final = [list_of_words[0].capitalize()]
for word in list_of_words[1:]:
word = word.lower()
if word in exceptions:
final.append(word)
else:
final.append(word.capitalize())
return " ".join(final)
print(setTitleCase("a Fancy Title WITH many stop Words and other stuff"))
Wich gives you as answer: "A Fancy Title with Many Stop Words and other Stuff"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Waldeyr Mendes da Silva |
