There have been a few attempts, here at AHK, to solve the problem of changing the case of a sentence to true title case, i.e. especially prepositions should written using lowercase. To my best knowledge, however, no solution has dealt with prepositions that start a sentence and end it. They should all be upper case.
Let's take the following sentence as an example.
The quick brown fox jumps over the lazy dog
This should be changed to:
The Quick Brown Fox Jumps over the Lazy Dog.
Over and the has not been written in upper case.
Let's consider the following nonsense sentence:
The quick brown fox jumps. over the lazy dog
This should be look this way:
The Quick Brown Fox Jumps. Over the Lazy Dog.
Over is in upper-case because it starts a new sentence.
A sentence can start with a dot and a space (. ), a question mark and a space (? ), an exclamation mark and a space (? ) and quite often a colon and a space (: ).
Quite often : goes after ? and ! i.e. ?: and !:
And the last thing: when a preposition ends a sentence it should be also in upper case.
Let consider the following two nonsense sentence:
The quick brown fox jumps at. over the lazy dog
-> The Quick Brown Fox Jumps At: Over the lazy dog
and another:
The quick brown lazy dog at
-> The Quick Brown Lazy Dog At
Here's my very simple attempt. I tried to list all the possibilities for "A" : ". A ", "? A " "! A " ": A " "?!: " etc.
Code: Select all
; based on: autohotkey.com/board/topic/56112-converting-to-proper-title-case/?p=352408
exclusions = |: A |?: A |. A |. An |. The | a | about| above| across| after | against| among| and | and,| around| as | as,| at | before|
Loop, parse, Clipboard, |
{
if A_LoopField not in %exclusions%
StringUpper, Clipboard, A_LoopField, T
else
oVar := A_LoopField
list .= oVar " "
oList .= A_LoopField " "
}
exclusions = a,an,and,as,at,between,but,by,for,if,in,of,on,or,the,to,with
Thanks for your help!
EDIT: Here's a code used to deal with this problem in Word using VBA
Code: Select all
Sub TrueTitleCase()
Dim sText As Range
Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long
Dim k As Long
Dim m As Long
Set sText = selection.Range
'count the characters in the selected string
k = Len(sText)
If k < 1 Then
'If none, then no string is selected
'so warn the user
MsgBox "Select the text first!", vbOKOnly, "No text selected"
Exit Sub 'and quit the macro
End If
'format the selected string as title case
sText.Case = wdTitleWord
'list the exceptions to look for in an array
vFindText = Array("A", "An", "And", "As", "At", "Between", "But", "By", "For", _
"If", "In", "Of", "On", "Or", "The", "To", "With")
'list their replacements in a matching array
vReplText = Array("a", "an", "and", "as", "at", "between", "but", "by", "for", _
"if", "in", "of", "on", "or", "the", "to", "with")
With sText
With .Find
'replace items in the first list
'with the corresponding items from the second
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute Replace:=wdReplaceAll
Next i
End With
'Reduce the range of the selected text
'to encompass only the first character
.MoveEnd Unit:=wdCharacter, Count:=-Len(sText) + 1
'format that character as upper case
.Case = wdUpperCase
'restore the selected text to its original length
.MoveEnd Unit:=wdCharacter, Count:=k
'and check to see if the string contains a colon
If InStr(1, sText, ":") > 0 Then
'If it does note the position of the character
'after the first colon
m = InStr(1, sText, ":") + 1
'and set that as the new start of the selected text
.MoveStart wdCharacter, m
'set the end of the selected text to include
'one extra character
.MoveEnd Unit:=wdCharacter, Count:=-Len(sText) + 1
'format that character as upper case
.Case = wdUpperCase
End If
End With
End Sub