Jump to content

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

Autocorrect Anything


  • Please log in to reply
16 replies to this topic
aaston86
  • Members
  • 2 posts
  • Last active: Feb 12 2014 06:43 PM
  • Joined: 03 Jan 2014

I use Google as my go-to spell check since it always seems to know what I mean. The below script lets you use a hotkey to autocorrect the spelling of highlighted text (including sentences containing multiple misspelled words) in any program. It's fast and very effective, but does need an internet connection to work.

; Ctrl+Alt+c autocorrect selected text
^!c::
clipback := ClipboardAll
clipboard=
Send ^c
ClipWait, 0
UrlDownloadToFile % "https://www.google.com/search?q=" . clipboard, temp
FileRead, contents, temp
FileDelete temp
if (RegExMatch(contents, "(Showing results for|Did you mean:)</span>.*?>(.*?)</a>", match)) {
   StringReplace, clipboard, match2, <b><i>,, All
   StringReplace, clipboard, clipboard, </i></b>,, All
}
Send ^v
Sleep 500
clipboard := clipback
return

 

To use the it, change the hotkey combination to whatever suits you best, put it in a script set to run on startup with any other hotkeys you use (make sure you have write and delete permission to whatever directory the script is in), and then enjoy seamless autocorrection of anything anywhere at the touch of a button.

 

The script works by backing up your clipboard contents, sending ctrl-c to grab whatever text you've highlighted, searching Google with your text, parsing the response with regex to see if Google autocorrected anything, pasting the corrected text over your original text, and finally restoring your clipboard.

 

Note that while the script can theoretically handle entire sentences, the expected usage is to highlight and correct a single word you think you may have misspelled.

 

Edit: As various posters have pointed out below, there are a number of problems and possible enhancements for the script. The URL I use is the default Google search URL for english-US and may need to be localized for international users. Google removes newlines from your search criteria so the script would need to encode newlines as a special character and decode them back to newlines after the search if you want your newlines preserved. Furthermore some HTML special characters (like apostrophes) will also need to be decoded to come back correctly. The regex I use is very brittle and will probably break if Google changes the format of their search response. And finally, while Google provides the smartest autocorrect experience I've ever seen, it isn't perfect and can potentially identify misspelled words as valid or correct words to the wrong thing.



alvitawa
  • Members
  • 98 posts
  • Last active: Oct 14 2017 02:16 PM
  • Joined: 07 Feb 2013

Thanks aaston86! This is awesome! :)



Scoox
  • Members
  • 194 posts
  • Last active: Jun 09 2017 03:34 AM
  • Joined: 28 Nov 2010

Brilliant idea, I am going to see what I can do with Google Translate. The PITA is that here in China Google is blocked most of the time, and having to swtich the VPN on and off all the time is a drag (because I also need to access Chinese websites without using the VPN).


RegJump() - Jump to registry path in Regedit

HoverScroll() - HoverScroll() - Scroll controls without focus


ErrorOnLine1
  • Members
  • 230 posts
  • Last active: Oct 05 2015 02:46 PM
  • Joined: 25 Dec 2012
Hi aaston86, 
 
I hesitate to mention this problem because I'm using an outdated version of AHk, and an outdated operating system, on an outdated computer.   :D
 
But anyway ... 
 
Highlighting these two sentences at the same time ... 
 
______________________ 
Mary had a litle lamb whouse fleeze wasn't white. 
 
The browne cow didn't jump over the moone. 
______________________ 
 
returns this ... 
 
Mary had a little lamb whose fleece wasn&#39;t white. The brown cow didn&#39;t jump over the moone. 


alvitawa
  • Members
  • 98 posts
  • Last active: Oct 14 2017 02:16 PM
  • Joined: 07 Feb 2013

I also get that, ' is replaced with &#39; a little filter should repair that :).



ErrorOnLine1
  • Members
  • 230 posts
  • Last active: Oct 05 2015 02:46 PM
  • Joined: 25 Dec 2012

I also get that, ' is replaced with &#39; a little filter should repair that :).

It's a little more than just the apostrophe. The formatting between the sentences has been lost and moon is still misspelled in the 2nd sentence.



bhawk
  • Members
  • 15 posts
  • Last active: Jan 19 2014 08:21 PM
  • Joined: 09 Mar 2009

Hello,

it does not work for me (i have internet connection and i have permission to create the temp file)

 

it does not correct this two words, for e.g.:

homwork

destop

 

i think it is not work because the temp file does not contained the strings you are looking for: "Showing results for" nor "Did you mean"

 

please advice

Thanks,Baruch



bhawk
  • Members
  • 15 posts
  • Last active: Jan 19 2014 08:21 PM
  • Joined: 09 Mar 2009

Problem found and fixed:

 

Problem occur only for non English user

Solution:

 replace: UrlDownloadToFile % "https://www.google.com/search?q=" . clipboard, temp_AutoCorrect.log

 with:      UrlDownloadToFile % "https://www.google.c...search?hl=en&q=" . clipboard, temp_AutoCorrect.log

 

enjoy



ldsaflsad
  • Members
  • 1 posts
  • Last active: Jan 26 2014 05:22 PM
  • Joined: 18 Jan 2014

Well done aaston86, this script is brilliant. There are a couple of things I would like added. Sometimes I have to run the script a number of times to get the fully correct result. For instance:

helllo howw aer yuo on this rahter fien day

gets corrected to:

hello how aer you on this rather fine day

and finally:

hello how are you on this rather fine day

 

Is there a line I can add to the end of the script so that it repeats itself say, five times, automatically?

 

Another feature I want is if I'm in a chat program or game like Habbo Hotel, when I finish writing something and hit Return, I want to press a key for it to automatically select everything (ctrl+a) and run the script. Maybe the Return key itself could be used for this (while not needing an extra Return keystroke for sending the text. I want the Return key to still act as it normally does, but ALSO trigger the script.)

 

Similarly, I want a key that I can press to correct the entire previous sentence or the entire current paragraph, in one keystroke (by automatically highlighting everything and running the script). 

 

If you can add these features that would be amazing.

 

Thanks.



Issvor
  • Members
  • 2 posts
  • Last active: Jan 24 2014 10:52 PM
  • Joined: 19 Jan 2014

Wow, this is pretty crazy. I didn't expect stuff like this could be made with AHK

I'm definitely gonna be looking into this more :D



alvitawa
  • Members
  • 98 posts
  • Last active: Oct 14 2017 02:16 PM
  • Joined: 07 Feb 2013

It's a little more than just the apostrophe. The formatting between the sentences has been lost and moon is still misspelled in the 2nd sentence.

 

If you search moone on google, it exists. So it doesn't correct it.



chaz
  • Members
  • 192 posts
  • Last active: Oct 01 2015 02:42 AM
  • Joined: 26 Mar 2013

Great work!

 

Note that Google also removes any linebreaks. For instance, if I input

testign testing
more testing

into the script, it returns

testing testing more testing

Find me at the other forum as timeFlies.


aaston86
  • Members
  • 2 posts
  • Last active: Feb 12 2014 06:43 PM
  • Joined: 03 Jan 2014

I've updated my original post to highlight some of the concerns you all have brought up. I'm not going to put any more work into the script since I made it for myself and it currently meets my needs, but I'm sure if anyone wants to address them and post the result it would make people happy.



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

i tested on:

 

If you search somoone on google, it exists. So it doesn't correct it.

 

and it corrected as:

 

If you search someone on google, it exists. So it doesn&#39;t correct it. :O



ozzii
  • Members
  • 167 posts
  • Last active: Oct 30 2015 04:54 PM
  • Joined: 23 Mar 2011

Just with the corrected single quote and added the '&'

 

; Ctrl+Alt+c autocorrect selected text
^!c::
clipback := ClipboardAll
clipboard=
Send ^c
ClipWait, 0
UrlDownloadToFile % "https://www.google.com/search?q=" . clipboard, temp
FileRead, contents, temp
FileDelete temp
if (RegExMatch(contents, "(Showing results for|Did you mean:)</span>.*?>(.*?)</a>", match)) {
 StringReplace, clipboard, match2, <b><i>,, All
 StringReplace, clipboard, clipboard, </i></b>,, All
 clipboard := RegExReplace(clipboard, "&#39;", "'")
 clipboard := RegExReplace(clipboard, "&amp;", "&")
}
Send ^v
Sleep 500
clipboard := clipback
return