Subversion Repositories navi

Rev

Rev 21 | Rev 23 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-

import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.autoreload
import tornado.database

import os
import re

from tsimapiak.parsenum import parse

class Index(tornado.web.RequestHandler):
    def get(self):
        self.redirect("/number")

class Number(tornado.web.RequestHandler):
    def get(self):
        self.render("templates/number.html", last="", numout=None)

    def post(self):
        try:
            num = self.get_argument("num").strip()
        except:
            self.redirect("/number")
        numout = parse(num.replace(" ",""))
        if numout == None:
            numout = -1
        self.render("templates/number.html", last=num, numout=numout)

class Restart(tornado.web.RequestHandler):
    def get(self):
        os.system("/usr/bin/restartnavi")

def clear(s):
    s = s.replace(ur"ɛ",ur"e").replace(ur".",ur"").replace(ur"ɾ",ur"r") \
           .replace(ur"ɪ",ur"ì").replace(ur"ˈ",ur"").replace(ur"'",ur"x") \
           .replace(ur"ŋ",ur"ng").replace(ur"j",ur"y").replace(ur"ʔ",ur"'") \
           .replace(ur"æ",ur"ä").replace(ur"ˌ",ur"").replace(ur"\t{ts}",ur"ts") \
           .replace(ur"ṛ",ur"rr").replace(ur"ḷ",ur"ll").replace(ur"k̚",ur"k ") \
           .replace(ur"p̚",ur"p ").replace(ur"t̚",ur"t ").replace(ur"'̚",ur"' ") \
           .replace(u"\\",ur"").replace(ur"(",ur"").replace(ur")",ur"") \
           .replace(ur"[",ur"").replace(ur"]",ur"") \
           .replace(ur"$cdot$",ur"").strip()
    s = re.sub(ur" or.*","",s)
    return s

class TestDB(tornado.web.RequestHandler):
    def get(self):
        text = u""
        db = tornado.database.Connection("127.0.0.1", "navi", user="navi", password="navi")
        for thing in db.query("""
        SELECT *, CHAR_LENGTH(navi) AS NL
        FROM `metaWords`
        WHERE partOfSpeech IN ('v.', 'vin.', 'vtr,')
        ORDER BY NL DESC"""
):
            text += unicode(thing["navi"]) + u" - " + clear(unicode(thing["ipa"])) + u"     -     " + unicode(unicode(thing["navi"]) == clear(unicode(thing["ipa"]))) + u"<br />"
        self.write(text)

application = tornado.web.Application([
    ("/", Index),
    ("/number", Number),
    ("/restart", Restart),
    ("/testdb", TestDB)
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(1337)
    tornado.autoreload.start()
    tornado.ioloop.IOLoop.instance().start()