-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.py
69 lines (56 loc) · 2.44 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from flask import Flask, request
from parser import Parser
from lojbansuggest.suggest import LojbanText, LojbanSentence, UnparsableText
from urllib2 import quote
app = Flask(__name__)
htmltempl = """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>learn how to parse lojban in your head</title>
<link rel="stylesheet" href="/static/boxes.css" type="text/css">
</head>
<body>
<form method="GET" action="explain"><input name="text"/><input type="submit"/></form><br/><br/>
%s
</body>
</html>"""
def rendersugs(sent):
return "".join("""<li>%s</li>""" % sent.sug)
@app.route("/")
def main():
return htmltempl % ("""input some (very) simple lojbanic text and see what happens!""")
@app.route("/explain", methods=["POST", "GET"])
def explain():
text = request.form["text"] if "text" in request.form else request.args.get("text", "coi do lo gerku ui ku klama lo zarci")
lojbansugoutput = ""
# run the text throug lojbansuggest to find out if it parses at all and has common newbie mistakes
try:
lt = LojbanText(text)
if len(lt.items) > 1:
sentencelist = "".join(["""<li><a href="/explain?text=%(textenc)s">%(text)s</a></li>\n""" % {"textenc": quote(sent.td), "text": sent.td} for sent in lt.items])
return htmltempl % ("""
<p>This software can only work with one sentence at a time.</p>
<p>Try a single one instead:</p>
<ul>
%s
</ul>""" % (sentencelist, ))
else:
thesent = lt.items[0]
if isinstance(thesent, unparsabletext):
return htmltempl % (""" <p>The sentence you supplied could not be parsed by the official parser.</p>
<p>Look at these suggestions or ask in the lojban irc channel or the mailing list for help</p>
<ul>
%s
</ul>""" % rendersugs(thesent))
else:
if len(thesent.sug) > 0:
lojbansugoutput = """ <p>I may have found possible mistakes in your sentence. Please consider them and ask on the mailing list or IRC channel if you're unsure.<p>
<ul>
%s
</ul><br/>""" % rendersugs(thesent)
except Exception, e:
print e
p = Parser(text)
return htmltempl % (lojbansugoutput + p())
if __name__ == "__main__":
app.run(host="0.0.0.0")