Capitalization script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jim7181812
Posts: 16
Joined: 10 Jun 2016, 10:46

Capitalization script

28 Jun 2016, 11:10

anyone have a script that will make the first letter of every sentence capitalize? Say I type out a bunch of private notes into notepad then want to make it public so I want to properly edit it and capitalize. Is there a script that will do this for me?


I posted this in another thread but I'm not sure it will be read.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Capitalization script

28 Jun 2016, 11:32

My suggestion would be just copy and paste it into Microsoft Word and do a Grammar/Spellcheck on it. Then you can copy the corrected text in Word back into Notepad if you need to. It will be even more thorough and can catch typos or things like an uncapitalized word i. I don't happen to have an autocapitalization script off hand, but I'd imagine it could be done with RegExReplace(). You'd search for a sequence of an ending punctuation, a space, and then a lowercase letter. Then you'd replace with the uppercase letter. I'm just not entirely sure how best to do that. My solution is to just loop through the string 26 times to get each letter. The lone regex fails, but the ones in the loops work alright.

Do note the first sentence is not capitalized, but that can easily be done manually.

Code: Select all

^6::
;FileRead, var, notcapitalized.txt
var:="the first sentence is not capitalized. but all other sentences are! check it out please? thanks. bye."
zar:=RegExReplace(var,"([\.\?\!]\s*[a-z])",Format("{:U}","$1"))
MsgBox % zar ; fails
tar:=var
Loop, 26
tar:=RegExReplace(tar,"([\.\?\!]\s*)" Chr(96+A_Index),"$1" Chr(64+A_Index))
MsgBox % tar
return
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Capitalization script

28 Jun 2016, 12:32

Try this:

Code: Select all

Example := "the first sentence is now capitalized. all other sentences are too! check it out please? thanks. bye."
MsgBox % Capitalize_Sentences(Example)


;-------------------------------------------------------------------------------
Capitalize_Sentences(Text) { ; parts taken from AutoHotkey Help
;-------------------------------------------------------------------------------
    static Delimiters := ".!?"

    Loop, Parse, Text, %Delimiters%
    {
        ; Calculate the position of the delimiter at the end of this field
        Position += StrLen(A_LoopField) + 1

        ; Retrieve the delimiter found by the parsing loop
        Delimiter := SubStr(Text, Position, 1)

        ; capitalize the first character
        StringUpper, FirstChar, % SubStr(Trim(A_LoopField), 1, 1)

        ; putting it all together
        Result .= FirstChar SubStr(Trim(A_LoopField), 2) Delimiter A_Space
    }

    Return, Result
}
I hope that helps.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: bobstoner289, peter_ahk, Spawnova and 342 guests