 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
andric0n Guest
|
Posted: Fri Mar 05, 2010 6:44 pm Post subject: Sentence Case |
|
|
I've automated most of my job functions with AHK over the last couple years. I've been meaning to tackle this problem for quite some time, but I finally got down to actually doing it today.
We have a ticketing system that I pull time-stamped notes from for different orders. The problem I have is that the notes that I scrape from the system are in all caps. I've searched the forums and haven't found anyone who's successfully created a sentence case script, so I decided to write it myself.
The script has two main functions; To split a string by common sentence-ending punctuation and make only the first character capitalized, and to make specific words either Title case or UPPER case.
For the latter, I'm using an INI file to save the lists of words that should be either title or upper case. For the former, I'm splitting the entire string by A_Space, checking to see if it should be upper or title case, and combining all the words from each sentence into seperate sentence variables, which are then combined back into the full string.
The GUI pulls the case lists from the INI file and provides a edit field to enter the text you want to alter.
The two subroutines (AddTC and AddUC) take whatever is in the clipboard, remove the spaces and punctuation, and add them to the appropriate case lists in the INI file.
The AlterCase subroutine reads the case lists, and alters the text.
Feel free to modify and redistribute.
| Code: | #SingleInstance, Force
IniRead, TCList, SystemData.ini, CaseLists, TCList, ERROR
IniRead, UCList, SystemData.ini, CaseLists, UCList, ERROR
Gui, Add, GroupBox, x6 y7 w600 h150 , String
Gui, Add, Edit, x16 y27 w580 h120 vString,
Gui, Add, Button, x506 y167 w100 h30 gAlterCase, Alter Case
Gui, Add, Button, x6 y167 w100 h30 gAddTC, Add to Title Case
Gui, Add, Button, x116 y167 w110 h30 gAddUC, Add to Upper Case
Gui, Show, h209 w619, Sentence Case Testing GUI
Return
GuiClose:
ExitApp
AddTC:
IniRead, TCList, SystemData.ini, CaseLists, TCList, ERROR
clipboard = %clipboard%
clipboard := RegExReplace(clipboard, "[\.,\?!""]")
TCList := TCList . "," . clipboard
IniWrite, %TCList%, SystemData.ini, CaseLists, TCList
GoSub, AlterCase
Return
AddUC:
IniRead, UCList, SystemData.ini, CaseLists, UCList, ERROR
clipboard = %clipboard%
clipboard := RegExReplace(clipboard, "[\.,\?!""]")
UCList := UCList . "," . clipboard
IniWrite, %UCList%, SystemData.ini, CaseLists, UCList
GoSub, AlterCase
Return
AlterCase:
IniRead, TCList, SystemData.ini, CaseLists, TCList, ERROR
IniRead, UCList, SystemData.ini, CaseLists, UCList, ERROR
GuiControlGet, String
NewText=
DelimiterList := ".,?,!"
StringSplit, TextArray, String, %A_Space%
LineCount := 1
Loop, %TextArray0%
{
CurrentVar := TextArray%A_Index%
CurrentVar = %CurrentVar%
StringLower, CurrentVar, CurrentVar
TestVar := RegExReplace(CurrentVar, "[\.,\?!""]")
If TestVar in %UCList%
StringUpper, CurrentVar, CurrentVar
If TestVar in %TCList%
StringUpper, CurrentVar, CurrentVar, T
Sentence%LineCount% := Sentence%LineCount% . " " . CurrentVar
If Currentvar contains %DelimiterList%
{
CurrentSentence := Sentence%LineCount%
Sentence%LineCount% :=
CurrentSentence = %CurrentSentence%
StringLeft, CurrentSentenceFirstChar, CurrentSentence, 1
StringTrimLeft, CurrentSentenceRestChars, CurrentSentence, 1
StringUpper, CurrentSentenceFirstChar, CurrentSentenceFirstChar
CurrentSentence := CurrentSentenceFirstChar . CurrentSentenceRestChars
NewText = %NewText% %CurrentSentence%
LineCount += 1
}
}
Loop, %LineCount%
Sentence%LineCount% := ""
Loop, %TextArray0%
TextArray%A_Index% := ""
GuiControl,, String, %NewText%
Return |
|
|
| Back to top |
|
 |
visitor Guest
|
|
| Back to top |
|
 |
andric0n Guest
|
Posted: Fri Mar 05, 2010 9:35 pm Post subject: |
|
|
StringLower makes all the characters lowercase.
StringUpper makes all the characters uppercase.
Adding the parameter "T" at the end of either makes the words Title Case, which is not the same as sentence case.
StringLower: "this is what it looks like."
StringUpper: "THIS IS WHAT IT LOOKS LIKE."
T Parameter: "This Is What It Looks Like."
My Script: "This is what it looks like." |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sun Mar 07, 2010 6:30 pm Post subject: |
|
|
If you are not concerned with exceptions (some letters of some words always capitalized, like I, AHK, AutoHotkey...), the following simple script does the work, too: | Code: | T =
(
AUTOHOTKEY SENDS SEVERAL TOOLS INTO RETIREMENT
it unites hotkey and text macros! IT offers a scripting-language, more powerful? easy TO LEARN 1.x. Beginners and I start fasT
)
T := RegExReplace(T, "[\.\!\?]\s+|\R+", "$0þ") ; mark 1st letters of sentences with char 254
Loop Parse, T, þ
{
StringLower L, A_LoopField
I := Chr(Asc(A_LoopField))
StringUpper I, I
S .= I SubStr(L,2)
}
MsgBox %S% |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sun Mar 07, 2010 6:54 pm Post subject: |
|
|
This version handles a list of exception words, stored comma delimited in the variable X. | Code: | T =
(
AUTOHOTKEY SENDS SEVERAL TOOLS INTO RETIREMENT
it unites hotkey and text macros! IT offers a scripting-language, more powerful? easy TO LEARN ahk1.x. Beginners and I start fasT
)
X = I,AHK,AutoHotkey
T := RegExReplace(T, "[\.\!\?]\s+|\R+", "$0þ") ; mark 1st letters of sentences with char 254
Loop Parse, T, þ
{
StringLower L, A_LoopField
I := Chr(Asc(A_LoopField))
StringUpper I, I
S .= I SubStr(L,2)
}
Loop Parse, X, `,
S := RegExReplace(S,"i)\b" A_LoopField "\b", A_LoopField)
MsgBox %S% | After converting everything to lower case, and making the 1st letters of the sentences upper case, the parse loop scans the exceptions. When one is found in word boundaries ("\b") with the upper/lower case ignored by the RegEx directive "i)", it is replaced with the proper capitalization.
Of course, the exceptions and the text can be read from files to the variable X and T. |
|
| Back to top |
|
 |
andric0n Guest
|
Posted: Wed Mar 10, 2010 5:45 pm Post subject: |
|
|
Laszlo:
I haven't taken the time to get familiar with RegEx. This works like a charm, though.
Thanks! |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|