Skip to content

Instantly share code, notes, and snippets.

@ispedals
Created May 20, 2013 21:12
Show Gist options
  • Select an option

  • Save ispedals/5615614 to your computer and use it in GitHub Desktop.

Select an option

Save ispedals/5615614 to your computer and use it in GitHub Desktop.
Example wrapping reading.mecab from Anki with a JSON web interface
# -*- coding: utf-8 -*-
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from urlparse import urlparse
from reading import mecab
import json, urllib
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
expr = u"カリン、自分でまいた種は自分で刈り取れ"
# t = "カリン、自分でまいた種は自分で刈り取れ"
url = urlparse(self.path)
params = dict([part.split('=') for part in url[4].split('&')])
expr=urllib.unquote(params['text']).decode('utf8')
t= mecab.reading(expr).encode('utf8')
t=json.dumps({'text':t})
self.send_response(200)
self.send_header('Content-Type', 'application/json; charset=UTF-8')
self.end_headers()
self.wfile.write('%s(%s);'% (params['jsoncallback'],t))
return
try:
server=HTTPServer(('',8080), MyHandler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment