Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Make sentences start with capitals


  • Please log in to reply
53 replies to this topic
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
If you finish a sentence with ".", "?" or "!" followed by white space or new line, we know that a new sentence follows, which should start with a capital (uppercase) letter. Here is a simple script, which fixes accidental lower case there, as it was discussed here.

It is a simple state machine, monitoring key presses. If ever a mouse key or special keyboard key is pressed, like {Up}, {RShift} or {F1} we get to the initial state, waiting for a safe sign of a sentence ending. If a sentence terminator is entered (.?!), we move to state 1 (forgetting everything beforehand). If a white space character is entered, it is ignored, unless we are in state 1, when we proceed to state 2. Letters (converted to upper case) are replaced in the text only if we are in state 2. At other than whitespace characters and terminators (like letters) we move back to the initial state, waiting for the end of another sentence.
#SingleInstance force
#NoEnv
Process Priority,,High
SetBatchLines -1

Loop {
   Input key, I L1 V,
(  Join
{ScrollLock}{CapsLock}{NumLock}{Esc}{BS}{PrintScreen}{Pause}{LControl}{RControl}
{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}
{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}
)
   If StrLen(ErrorLevel) > 3  ; NewInput, EndKey
      state =
   Else If InStr(".!?",key)   ; Sentence end
      state = 1
   Else If InStr("`t `n",key) ; White space
      state += (state = 1)    ; state1 -> state2
   Else {
      StringUpper key, key
      If state = 2            ; End-Space*-Letter
         Send {BS}{%key%}     ; Letter -> Upper case
      state =
   }
}

~LButton::
~RButton::
~MButton::
~WheelDown::
~WheelUp::
~XButton1::
~XButton2::State =


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
.
This is wondeful coding Laszlo.
I find this useful.
Oh my :shock: ...
Your code also replaces in the next line..

Absolute beauty.. It even replaces with a line in between..

Thanks a lot.
This code will be very useful to me.

Regards, :D...

Ps: i never used shift to type this post. :D
kWo4Lk1.png

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
The code demonstrates the power and simplicity of state machines. Try writing a parser for real numbers with optional sign, decimal point, signed or unsigned exponent. With state machines you might succeed with less than 20 lines of code.

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
I'm running it right now... And i think its really good and useful... Thanks for sharing it Laszlo!

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
You can remove SetBatchLines and Priority and use SendInput instead of Send - it's faster and much less likely to cause system instability.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
SetBatchLines determines how fast the script runs, that is, how often it sleeps. Because the script has an always running loop, it must finish its job fast, otherwise it will miss keys at fast typing. In slower PC's SetBatchLines -1 looks necessary.

Similarly, "Process Priority,,High" ensures that when other tasks consume CPU cycles, this script still reacts to key presses quickly. You should even consider "Realtime", if you experience keys appearing slowly in the application.

Since Send only sends 2 keys in the beginning of sentences, the speed advantage of SendInput is not noticeable. On the other hand, some applications don't take SendInput keys (like my MultiEdit does not like SendInput ^v), so you have to use Send (= SendEvent) or even SendPlay. I found Send is the best tradeoff between compatibility and speed. If it is different in your PC, just change it.

Kai
  • Guests
  • Last active:
  • Joined: --
Just a suggestion...

The letter "I" between spaces to be in capital too?

Kai
  • Guests
  • Last active:
  • Joined: --
oh anyway.. Mine didnt work properly >.< it wont start with capital unlses i have a "." in front >.< or ! etc..

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

The letter "I" between spaces to be in capital too?

Not for me! If anyone types program code, math formulae, algorithms, the capitalized variable i drives him crazy. It happens to be the imaginary unit and the most common index variable.

oh anyway.. Mine didnt work properly >.< it wont start with capital unless i have a "." in front >.< or ! etc..

This was the purpose. "If you finish a sentence with ".", "?" or "!" followed by white space or new line, a new sentence follows".

Soldier3570
  • Members
  • 10 posts
  • Last active: Sep 20 2006 09:55 AM
  • Joined: 30 Aug 2006
im using it right now and i dont think it is working

im not sure whats wrong maybe its my computer :oops:
Soldier3570

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
In what application, Windows version, AHK version? What happens if you type ". a"?

d-man
  • Members
  • 290 posts
  • Last active: Jun 28 2015 09:26 AM
  • Joined: 08 Jun 2006
Here's a version I use where APPsKEY turns it on or off, as sometimes it is annoying. This also changes apostrophe's and quotations.

AppsKey::
onn:=!onn
SetTimer, Start, 250
return

Start:
Loop
{ 
  Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}
  StringUpper key, key  
  If InStr(ErrorLevel,"EndKey") 
    state = 
  Else If InStr(".!?",key) 
    state = 1
  Else If InStr("`t `n",key) 
  { 
      If state = 1 
        state = 2 
  } 
  Else 
  { 
    If state = 2 
      Send {BS}{%key%} 
      state = 
  } 
  if !onn
	{
	 SetTimer, Start, off
	break
	}
}
return

$'::
if onn = 1
send ’
else
send '
return

$":: 
if onn = 1
{
        Send, {ASC 0147} 
        Send, {ASC 0148}{left}
}
else
send "
return


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Turning it on or off is a good idea! Here is a somewhat simpler version of your variant. (It does not break out at NewInput of the Input command, Ctrl-H, Alt-Tab, etc.)
Loop

{

   if !onn {

     Sleep 100

     Continue

   }

   Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}

   StringUpper key, key

   If InStr(ErrorLevel,"EndKey")

     state =

   Else If InStr(".!?",key)

     state = 1

   Else If InStr("`t `n",key) {

     If state = 1

        state = 2

   } Else {

     If state = 2

        Send {BS}{%key%}

     state =

   }

}

return



AppsKey::onn:=!onn


Riley
  • Guests
  • Last active:
  • Joined: --
hi. Im just a beginner and im trying to learn and study your script.

how do i modify the script to capitalize the first letter of every word i type?

Thank you.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Something like this.
Loop {

   if !onn {

     Sleep 100

     Continue

   }

   Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}

   if !onn

     Continue

   StringUpper key, key

   If InStr(ErrorLevel,"EndKey")

     state =

   Else If InStr(".,!? `t`n",key)

     state = 1

   Else {

     If state = 1

        Send {BS}{%key%}

     state =

   }

}

return



AppsKey::

   onn:=!onn

   state = 1

   TrayTip,,% "Capitals correction is now O". (onn ? "N" : "FF")

Return
Don't forget, this is a very simple script: if you move around with the arrow keys, PgDn, End, etc., it cannot tell if at the new caret position a new word starts, or we are in the middle of a word.