Don't understand german? Read or subscribe to my english-only feed.

google hack for your desktop: keyword correct

Do you know the situation where you just aren’t sure whether you are spelling a word the right way? If you know how to type the word pretty well, then a dictionary might work just fine. If you aren’t sure at all you might consider using Google’s ‘Did you mean’-feature. Check out a simple demonstration for the word Wondows, which could mean ‘Windows’. ;-) If you happen to use that google-feature just too many times you might be interested in my small hack:

wget -O google_correct.py http://grml.org/scripts/google_correct.txt
chmod +x google_correct.py

This small python script uses xsel (so it currently works only on systems providing xsel ;-)) to retrieve the currently selected word from your X selection, executes a google search and parses the output for the keyword inside the ‘Did you mean’ section. Finally it sets the corrected keyword as your current X selection. If you provide the keyword directly as an option, then the script just outputs the corrected keyword without touching your X selection so you can integrate it directly inside your $EDITOR as well as an option. This gives you the flexibility to use this feature globally(!) inside your preferred window manager. Select a word, press the keybinding for executing google_correct.py and now you’ve the corrected word available for further actions…

Finally don’t forget to put the affected word (after double-checking it using a real dictionary of course) into your $EDITOR’s dictionary to keep such “help m0m, I can’t sp3ll w0rds anymore” situations at a minimum. I’m using a personal dictionary file and ‘iab wondows windows’ inside Vim to fix spelling errors for words I’m unsure how to spell or which I always get worng.

A whishlist of further ideas/features for the script can be found in the header of the script itself. Further input welcome.

2 Responses to “google hack for your desktop: keyword correct”

  1. gregor herrmann Says:

    very nice idea, thanks!

    I’m just not sure about the logic regarding STDIN — why would I need xsel if I pass the argument on the commandline and output to STDOUT? I’d reverse the check for sys.argv[] and xsel:

    — google_correct.py.orig 2008-02-11 22:23:48.000000000 +0100
    +++ google_correct.py 2008-03-09 03:51:25.000000000 +0100
    @@ -68,16 +68,16 @@
    proc.wait()

    if __name__ == ‘__main__’:
    – if which(‘xsel’):
    – if sys.argv[1:]:
    – # print it to stdout:
    – print correct_keyword(sys.argv[1])
    – # or if you prefer to put it directly to X selection:
    – #set_x_selection(correct_keyword(sys.argv[1]))
    – else:
    – set_x_selection(correct_keyword(get_x_selection()))
    + if sys.argv[1:]:
    + # print it to stdout:
    + print correct_keyword(sys.argv[1])
    + # or if you prefer to put it directly to X selection:
    + #set_x_selection(correct_keyword(sys.argv[1]))
    else:
    – sys.exit(“Sorry, xsel not found. Exiting.”)
    + if which(‘xsel’):
    + set_x_selection(correct_keyword(get_x_selection()))
    + else:
    + sys.exit(“Sorry, xsel not found. Exiting.”)

    ## END OF FILE #################################################################
    # vim: ft=python tw=100 ai et

  2. mika Says:

    @gregor: you’are absolutely right – good catch, thanks. :) I included your patch.

    regards,
    -mika-