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 

Get google "Did you mean" suggestions anywhere

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
kyf
Guest





PostPosted: Sat Sep 12, 2009 6:20 pm    Post subject: Get google "Did you mean" suggestions anywhere Reply with quote

Hi,

i find "did you mean" is good for quickly spell checking a word, but also for grammer ....(useful if you a bit dyslexic!)

So I wrote this script so i could have the "Did you mean" suggestions at the press of a hotkey.

It currently works if you select a word or phrase and press win+d
...if there is a suggestion available, press 'enter' to replace original word, or any other key to continue

Code:

searchURL = http://www.google.com/search?hl=en&q=     ;search string... change to local version if you wish, but may need to change FileReadLine line
tempfile = %temp%\DYM  ;temp file to save search page
needle = class=spell>Did you mean  ;did you mean search string
needle2 = class=spell>
needle3 = </a>

#d::

   ClipSaved := ClipboardAll   ; Save the entire clipboard to a variable, so it isn't affected by script
   send, ^c
   ClipWait
   
   urldownloadtofile, %searchURL%%Clipboard%, %tempfile%
   
   
    FileReadLine, LineIn, %tempfile%, 5   ; read the 5th line of the search page, where the "did you mean" bit is
          IfInString, LineIn, %needle% ; check if it offers a suggestion
            {
                StringGetPos, length, LineIn, %needle% 
                length:= length + 24            
            StringTrimLeft, suggest, LineIn, %length%   ;remove everything before "did you mean"
            
            StringGetPos, length, suggest, %needle2%
            length:= length + 12
            StringTrimLeft, suggest, suggest, %length% ;remove everything before the DYM suggestion
               
            StringGetPos, length, suggest, %needle3%
            StringLeft, suggest, suggest, %length% ;remove everything after the DYM suggestion
            
            ;remove bold italic formating
              StringReplace, suggest, suggest, <b><i>,, All
                StringReplace, suggest, suggest, </i></b>,, All
               
            toolText = Did you mean: `n %suggest%
                tooltip, %toolText%   ,  A_CaretX, A_CaretY + 20
            Input, SingleKey, L1 T6, {enter} ;wait for input (for max 6 seconds)... if 'enter' pressed replace with suggestion
            if ErrorLevel <> Max
            {
             if ErrorLevel <> Timeout
             {
            Clipboard := suggest      
            Send, ^v
             }
                }
               
         
            } else {
                toolText = No suggestion
                tooltip, %toolText%   ,  A_CaretX, A_CaretY +20
            sleep, 2000
            }
                     
   tooltip         
    FileDelete, %tempfile%  ; delete the temporary file
Clipboard := ClipSaved   ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved =   ; Free the memory in case the clipboard was very large.
   
   
 return



It might be interesting to add something like this into the intellisense code
Wink
Back to top
TchnclFl



Joined: 29 Jul 2009
Posts: 48

PostPosted: Sat Sep 12, 2009 6:46 pm    Post subject: Reply with quote

Nice one! I wouldn't use it very much, but I enjoy the concept Smile.
_________________
Click here to get free games and computer supplies! It's easy and fun! CLICK ME!

EasyAHK
Back to top
View user's profile Send private message
kyfs
Guest





PostPosted: Tue Jul 13, 2010 6:46 pm    Post subject: Reply with quote

UPDATE
Just in case anyone is interested, thought i should upload an updated version of this as the one i posted previously doesn't work anymore (as google changed their code a little)

Code:

#c::
;---- set a few initial parameters......
searchURL = http://www.google.co.uk/search?hl=en&q=     ;search string... change to local version if you wish, but may need to change FileReadLine line
tempfile = %temp%\DYM  ;temp file to save search page


;----get word of interest..... (and save the current clipboard contents)

   ClipSaved := ClipboardAll   ; ----Save the entire clipboard to a variable, so it isn't affected by script
   clipboard = zzzzzz  ;use to test if anything copied
   send, ^c
   ClipWait
   
    if clipboard = zzzzzz ;if clipboard empty, try selecting a nearby word
   {
    send, ^{left}
   send, {ctrldown}{shiftdown}{right}{ctrlup}{shiftup}
   send, ^c
   ClipWait
   }
   
;---do the search and save the webpage....   
   query = %Clipboard%
   urldownloadtofile, %searchURL%%query%, %tempfile%
   ;msgbox, %searchURL%%query%

;---read through the file, to find the result amount and the did you mean suggestion
    findCount:= 0   
    Loop, read, %tempfile%
    {
       if (findCount == 0) ;-----get # of results first....
        {   
          NumString = resultStats>About
         IfInString, A_LoopReadLine, %NumString%
          {
            StringGetPos, length, A_LoopReadLine, %NumString%
            length:= length + 18
            StringTrimLeft, NumResults, A_LoopReadLine, %length%
            
            NumString = <nobr>
            StringGetPos, length, NumResults, %NumString% 
            StringLeft, NumResults, NumResults, %length%   
            
             findcount:= 1  ;acknowledge that we have now found results number
            
         }
      }
      
      if (findCount == 1) ;-----get # of results first....
      {
      
      ;---then search for the did you mean bit...
        needle = Did you mean: </span>  ;did you mean search string
          needle2 = class=spell>
          needle3 = </a>
        needle4 = Showing results for </span>
       
        IfInString, A_LoopReadLine, %needle% ; check if it offers a suggestion
         {
          suggest:= A_LoopReadLine
         StringGetPos, length, suggest, %needle% 
            length:= length + 21            
          StringTrimLeft, suggest, suggest, %length%   ;remove everything before "did you mean"

          StringGetPos, length, suggest, %needle2%
         length:= length + 12
         StringTrimLeft, suggest, suggest, %length% ;remove everything before the actual suggestion   
         
         StringGetPos, length, suggest, %needle3%
         StringLeft, suggest, suggest, %length% ;remove everything after the actual suggestion
         ;remove bold italic formating
           StringReplace, suggest, suggest, <b><i>,, All
           StringReplace, suggest, suggest, </i></b>,, All
         
            findcount:= 2  ;acknowledge that we have now found DYM suggestion         
         break
           }
      ;---...or search for the "Showing results for" bit
      IfInString, A_LoopReadLine, %needle4% ; check if it offers a suggestion
         {
          suggest:= A_LoopReadLine
         StringGetPos, length, suggest, %needle% 
            length:= length + 27            
          StringTrimLeft, suggest, suggest, %length%   ;remove everything before "did you mean"

          StringGetPos, length, suggest, %needle2%
         length:= length + 12
         StringTrimLeft, suggest, suggest, %length% ;remove everything before the actual suggestion   
         
         StringGetPos, length, suggest, %needle3%
         StringLeft, suggest, suggest, %length% ;remove everything after the actual suggestion
         ;remove bold italic formating
           StringReplace, suggest, suggest, <b><i>,, All
           StringReplace, suggest, suggest, </i></b>,, All
         
            findcount:= 2  ;acknowledge that we have now found DYM suggestion         
         break
           }
      }
    }

    ;---------------------------
      
          if (findcount == 2)
            {                  
            toolText = Did you mean: `n %suggest%
                tooltip, %toolText%   ,  A_CaretX, A_CaretY + 20
            Input, SingleKey, L1 T5, {enter} ;wait for input (for max 5 seconds)... if 'enter' pressed replace with suggestion
            if ErrorLevel <> Max
            {
             if ErrorLevel <> Timeout
             {
            Clipboard := suggest      
            Send, ^v
             }
                }
               
         
            } else {
            toolText = Google doesn't offer alternative (%NumResults%)
            ;----------
            found = 0
            Loop, read, bigList.txt
            {
            
            if (found == 0)
            {
             if  (query == A_LoopReadLine)
             {
              toolText = In dictionary `n Google doesn't offer alternative (%NumResults%)
             found = 1
             }
            }
            }
            ;---------
               
            if (found == 0)
               toolText = %toolText% `n Not in dictionary
            
                tooltip, %toolText%   ,  A_CaretX, A_CaretY +20
            sleep, 4000
            }
                     
   tooltip
    sleep, 500   
    FileDelete, %tempfile%  ; delete the temporary file
Clipboard := ClipSaved   ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved =   ; Free the memory in case the clipboard was very large.
   
   
 return
Back to top
kyfs
Guest





PostPosted: Tue Jul 13, 2010 6:48 pm    Post subject: Reply with quote

Oh... and i changed the hotkey to win+c ...incase you didn't spot that
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
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