kinkou
kinkou

Reputation: 303

ALL CAPS to Normal case

I'm trying to find an elegant solution on how to convert something like this

ALL CAPS TEXT. "WHY ANYONE WOULD USE IT?" THIS IS RIDICULOUS! HELP.

...to regular-case. I could more or less find all sentence-starting characters with:

(?<=^|(\. \"?)|(! ))[A-Z] #this regex sure should be more complex

but (standard) Ruby neither allows lookbehinds, nor it is possible to apply .capitalize to, say, gsub replacements. I wish I could do this:

"mytext".gsub(/my(regex)/, '\1'.capitalize)

but the current working solution would be to

"mytext".split(/\. /).each {|x| p x.capitalize } #but this solution sucks

Upvotes: 4

Views: 751

Answers (1)

sawa
sawa

Reputation: 168239

First of all, notice that what you are trying to do will only be an approximation.

  1. You cannot correctly tell where the sentence boundaries are. You can approximate it as The beginning of the entire string or right after a period, question mark, or exclamation mark followed by spaces. But then, you will incorrectly capitalize "economy" in "U.S. economy".

  2. You cannot correctly tell which words should be capitalized. For example, "John" will be "john".

You may want to do some natural language processing to give you a close-to-correct result in many cases, but these methods are only probablistically correct. You will never get a perfect result.

Understanding these limitations, you might want to do:

mytext.gsub(/.*?(?:[.?!]\s+|\z)/, &:capitalize)

Upvotes: 4

Related Questions