Skip to content

Instantly share code, notes, and snippets.

@shizeeg
Created May 17, 2013 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shizeeg/5598723 to your computer and use it in GitHub Desktop.
Save shizeeg/5598723 to your computer and use it in GitHub Desktop.
Yandex.Speller command-line client (alpha) Licensed under terms of GPL
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ts=4:st=4:et
"""
Yandex.Speller-cli For more info, see <http://www.yandex.net/speller>
Copyright (C) 2013 sh!zeeg <shizeeque@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
# set default encoding
if sys.version_info < (3, 0):
reload(sys)
sys.setdefaultencoding('utf-8')
# try simplejson first, then json from Python 2.6+
try: import simplejson as json
except ImportError:
try: import json; json.dumps
except (ImportError, AttributeError):
sys.stderr.write("Please install simplejson or Python 2.6 or higher.")
sys.exit(2)
# trying to play cross-version way.
try:
from urllib import request, error
from urllib.error import HTTPError
from urllib.parse import quote
except ImportError:
import urllib2 as request
from urllib2 import HTTPError
from urllib import quote
if len(sys.argv) > 1:
text = ' '.join(sys.argv[1:])
import urllib
try:
resp = request.urlopen('http://speller.yandex.net/services/spellservice.json/checkText?ie=%s&text=%s' % ('utf-8', quote(text)))
except HTTPError:
sys.stderr.write("%s\n" % sys.exc_info()[1])
sys.exit(1)
json_data = json.loads(resp.read().decode('utf-8'))
# debug
#print(json.dumps(json_data, indent=4))
#
if len(json_data) > 0:
word = json_data[0]['word']
s = ', '.join(json_data[0]['s'])
sys.stdout.write("%s\n" % (s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment