Convert into Title Case under a condition

381 views
Skip to first unread message

Mauricio Salazar

unread,
Feb 13, 2014, 11:55:01 AM2/13/14
to openr...@googlegroups.com

Hi everybody, I want to convert into TitleCase some words that are into UpperCase 

For instance i got the phrase: 

Includes the newspaper edition, BREAKING news. 

to 

Includes the newspaper edition, Breaking news. 


In this case I converted the word BREAKING into Breaking.

Best Regards






Chris Barnett

unread,
Feb 20, 2014, 1:51:14 PM2/20/14
to openr...@googlegroups.com

If you only want to convert certain words that could be in the middle of text then

replace(value,"BREAKING","breaking")

would be an option.

Steve W

unread,
Mar 7, 2014, 8:26:08 PM3/7/14
to openr...@googlegroups.com
If there's just 1 All Caps word you might try something like:
with(value.match(/([A-Z]+).*/)[0],r,replace(value,/([A-Z]+)/,toTitlecase(r)))

If the value contains multiple All Caps words, you might need to incorporate a forEach()

Owen Stephens

unread,
Mar 8, 2014, 5:39:11 AM3/8/14
to openr...@googlegroups.com
Another approach might be to create a facet of all uppercase words so you can see what you are dealing with.
E.g. Create a 'custom text facet' with the expression

forEach(value.split(' '),v,match(v,/([^a-z]*)/)[0])

This splits down each text string into words and populates the facet only with those that contain no lowercase letters in it (I use this rather than matching on uppercase letters to catch hyphenated words etc.)

The resulting facet could help you decide the best strategy for fixing the problem. It might also help identify any uppercase words that should not be converted to titlecase (usually acronyms) e.g. BBC, CNN)

Owen

Owen Stephens

unread,
Mar 8, 2014, 6:00:53 AM3/8/14
to openr...@googlegroups.com
On Saturday, March 8, 2014 1:26:08 AM UTC, Steve W wrote:
If there's just 1 All Caps word you might try something like:
with(value.match(/([A-Z]+).*/)[0],r,replace(value,/([A-Z]+)/,toTitlecase(r)))

This isn't quite right - it will find any set of one or more uppercase letters (rather than word) and then replace other sequences of uppercase letters with the first sequence found. So if you run this on
ABI Technik
Accounting, Economics and Law

You get
Abi Abiechnik
Accounting, Aconomics and Aaw

My one-liner to find all words with no lowercase letters and converting to titlecase is:

forEach(value.split(' '),v,if(isNonBlank(v.match(/([^a-z]*)/)[0]),toTitlecase(v.match(/([^a-z]*)/)[0]),v)).join(" ")

Splits string into array of words (split on space), checks if word contains no lowercase and if so converts to titlecase, joins array back together into string using space as join. To use 'only uppercase' rather than 'no lowercase' you can use the very similar

forEach(value.split(' '),v,if(isNonBlank(v.match(/([A-Z]*)/)[0]),toTitlecase(v.match(/([A-Z]*)/)[0]),v)).join(" ")

Owen

Steve W

unread,
Mar 10, 2014, 11:17:32 PM3/10/14
to openr...@googlegroups.com
That makes sense. As you pointed out earlier though you'd still need to use filters to catch and hide any acronyms you didn't want to change as this would produce "Abi Technik".  
Reply all
Reply to author
Forward
0 new messages