AutoHotkey Community

It is currently May 27th, 2012, 4:37 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Sentence Case
PostPosted: March 5th, 2010, 7:44 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2010, 8:38 pm 
StringLower / StringUpper


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2010, 10:35 pm 
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."


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2010, 7:30 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2010, 7:54 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2010, 6:45 pm 
Laszlo:

I haven't taken the time to get familiar with RegEx. This works like a charm, though.

Thanks!


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group