Subversion Repositories navi

Rev

Rev 38 | Rev 46 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 szabot 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
 
4
import tornado.httpserver
5
import tornado.ioloop
6
import tornado.web
7
import tornado.autoreload
8
import tornado.database
9
 
10
import os
16 szabot 11
import re
2 szabot 12
 
4 szabot 13
from tsimapiak.parsenum import parse
2 szabot 14
 
15
class Index(tornado.web.RequestHandler):
16
    def get(self):
17
        self.redirect("/number")
18
 
19
class Number(tornado.web.RequestHandler):
20
    def get(self):
21
        self.render("templates/number.html", last="", numout=None)
22
 
23
    def post(self):
24
        try:
25
            num = self.get_argument("num").strip()
26
        except:
27
            self.redirect("/number")
28
        numout = parse(num.replace(" ",""))
29
        if numout == None:
30
            numout = -1
31
        self.render("templates/number.html", last=num, numout=numout)
32
 
33
class Restart(tornado.web.RequestHandler):
34
    def get(self):
35
        os.system("/usr/bin/restartnavi")
36
 
31 szabot 37
def getwords():
38
    ret = []
32 szabot 39
    current = u""
31 szabot 40
    db = tornado.database.Connection("127.0.0.1", "navi", user="navi", password="navi")
41
    for row in db.query("""
42
    SELECT *, CHAR_LENGTH(navi) AS NL
43
    FROM `metaWords`
44
    ORDER BY NL DESC"""):
45
        if row["partOfSpeech"] in (u"v.", u"vin.", u"vtr."):
46
            current = unicode(row["ipa"])
47
            current = current.replace(ur"ɛ",ur"e").replace(ur".",ur"").replace(ur"ɾ",ur"r") \
14 szabot 48
           .replace(ur"ɪ",ur"ì").replace(ur"ˈ",ur"").replace(ur"'",ur"x") \
49
           .replace(ur"ŋ",ur"ng").replace(ur"j",ur"y").replace(ur"ʔ",ur"'") \
50
           .replace(ur"æ",ur"ä").replace(ur"ˌ",ur"").replace(ur"\t{ts}",ur"ts") \
16 szabot 51
           .replace(ur"ṛ",ur"rr").replace(ur"ḷ",ur"ll").replace(ur"k̚",ur"k ") \
52
           .replace(ur"p̚",ur"p ").replace(ur"t̚",ur"t ").replace(ur"'̚",ur"' ") \
53
           .replace(u"\\",ur"").replace(ur"(",ur"").replace(ur")",ur"") \
26 szabot 54
           .replace(ur"[",ur"").replace(ur"]",ur"").replace(ur"  "," ") \
31 szabot 55
           .strip()
56
            current = re.sub(ur" or.*","",current)
57
            current = re.sub(ur"z(.*)engk(.*)e",ur"z\1enk\2e",current)
58
            current = re.sub(ur"t(.*)ì(m|n)\ ",ur"t\1ìng ",current)
35 szabot 59
            current = current.split(ur"$cdot$")
31 szabot 60
            if len(current) == 3:
61
                current = current[0] + u"<0><1>" + current[1] + u"<2>" + current[2]
33 szabot 62
            elif len(current) == 2:
63
                current = current[0] + u"<0><1><2>" + current[1]
31 szabot 64
            else:
33 szabot 65
                current = u"<0><1><2>" + current[0]
31 szabot 66
        else:
67
            current = unicode(row["navi"])
68
        ret.append([row["id"], row["navi"], current, row["partOfSpeech"]])
69
    return ret
10 szabot 70
 
2 szabot 71
class TestDB(tornado.web.RequestHandler):
72
    def get(self):
31 szabot 73
        lis = getwords()
39 szabot 74
        text = u"id | navi | infix | partofspeech<br />"
75
        text += u"<br />".join(u" | ".join(unicode(y) for y in x) for x in lis)
2 szabot 76
        self.write(text)
77
 
78
application = tornado.web.Application([
79
    ("/", Index),
80
    ("/number", Number),
81
    ("/restart", Restart),
82
    ("/testdb", TestDB)
83
])
84
 
85
if __name__ == "__main__":
86
    http_server = tornado.httpserver.HTTPServer(application)
87
    http_server.listen(1337)
88
    tornado.autoreload.start()
89
    tornado.ioloop.IOLoop.instance().start()