AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Make sentences start with capitals
Goto page 1, 2, 3, 4  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Wed Jul 26, 2006 4:07 pm    Post subject: Make sentences start with capitals Reply with quote

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.
Code:
#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 =
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Wed Jul 26, 2006 4:28 pm    Post subject: Reply with quote

.
This is wondeful coding Laszlo.
I find this useful.
Oh my Shocked ...
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, Very Happy...

Ps: i never used shift to type this post. Very Happy
_________________
URLGet - Internet Explorer based Downloader


Last edited by SKAN on Wed Jul 26, 2006 5:07 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Wed Jul 26, 2006 4:45 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Rajat



Joined: 28 Mar 2004
Posts: 1687

PostPosted: Wed Jul 26, 2006 6:57 pm    Post subject: Reply with quote

I'm running it right now... And i think its really good and useful... Thanks for sharing it Laszlo!
_________________
Back to top
View user's profile Send private message
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Wed Jul 26, 2006 7:13 pm    Post subject: Reply with quote

You can remove SetBatchLines and Priority and use SendInput instead of Send - it's faster and much less likely to cause system instability.
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Wed Jul 26, 2006 7:45 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Kai
Guest





PostPosted: Mon Sep 18, 2006 4:07 am    Post subject: Reply with quote

Just a suggestion...

The letter "I" between spaces to be in capital too?
Back to top
Kai
Guest





PostPosted: Mon Sep 18, 2006 4:13 am    Post subject: Reply with quote

oh anyway.. Mine didnt work properly >.< it wont start with capital unlses i have a "." in front >.< or ! etc..
Back to top
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Mon Sep 18, 2006 4:38 am    Post subject: Reply with quote

Kai wrote:
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.
Kai wrote:
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".
Back to top
View user's profile Send private message
Soldier3570



Joined: 30 Aug 2006
Posts: 10

PostPosted: Mon Sep 18, 2006 7:18 am    Post subject: Reply with quote

im using it right now and i dont think it is working

im not sure whats wrong maybe its my computer Embarassed
_________________
Soldier3570
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Mon Sep 18, 2006 3:56 pm    Post subject: Reply with quote

In what application, Windows version, AHK version? What happens if you type ". a"?
Back to top
View user's profile Send private message
d-man



Joined: 08 Jun 2006
Posts: 285

PostPosted: Mon Sep 18, 2006 4:34 pm    Post subject: Reply with quote

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.

Code:
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
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Mon Sep 18, 2006 8:00 pm    Post subject: Reply with quote

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.)
Code:
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
Back to top
View user's profile Send private message
Riley
Guest





PostPosted: Fri Aug 10, 2007 11:02 pm    Post subject: Reply with quote

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.
Back to top
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Fri Aug 10, 2007 11:40 pm    Post subject: Reply with quote

Something like this.
Code:
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.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group