Permalink
Browse files

support utf-8 and sort

  • Loading branch information...
1 parent 1b1a1d0 commit 9457d2d0e4279d71395f2c0d5afb21fdafd99fce @shuyo committed Feb 28, 2012
Showing with 68 additions and 107 deletions.
  1. +7 −3 server.py
  2. +0 −67 static/index.BAK
  3. +61 −37 static/index.html
View
@@ -5,16 +5,19 @@
# This code is available under the MIT License.
# (c)2011 Nakatani Shuyo / Cybozu Labs Inc.
-import os
+import sys, os, codecs
import BaseHTTPServer
import urlparse
import optparse
import json
import numpy
import ldig
+#sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
+
parser = optparse.OptionParser()
parser.add_option("-m", dest="model", help="model directory")
+parser.add_option("-p", dest="port", help="listening port number", type="int", default=48000)
(options, args) = parser.parse_args()
if not options.model: parser.error("need model directory (-m)")
@@ -52,7 +55,8 @@ def do_GET(self):
localpath = basedir + path
if path == "/detect":
params = urlparse.parse_qs(url.query)
- json.dump(detector.detect(params['text'][0]), self.wfile)
+ text = unicode(params['text'][0], 'utf-8')
+ json.dump(detector.detect(text), self.wfile)
elif os.path.exists(localpath):
self.send_response(200)
if path.endswith(".html"):
@@ -67,6 +71,6 @@ def do_GET(self):
self.send_header("Expires", "Fri, 31 Dec 2100 00:00:00 GMT")
self.end_headers()
-server = BaseHTTPServer.HTTPServer(('', 48000), LdigServerHandler)
+server = BaseHTTPServer.HTTPServer(('', options.port), LdigServerHandler)
print "ready."
server.serve_forever()
View
@@ -1,67 +0,0 @@
-<html>
-<head>
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
-<title>ldig server</title>
-</head>
-<body>
-
-<textarea id="detectText" cols="40" rows="4">
-I am a pen.
-</textarea>
-<input id="detectButton" type="button" value="detect"/>
-
-<table id="probTable">
-<thead id="probHead"></thead>
-<tbody id="probBody"></tbody>
-</table>
-
-<table id="featTable">
-<thead id="featHead"></thead>
-<tbody id="featBody"></tbody>
-</table>
-
-
-<script lang="text/javascript">
-$(document).ready(function() {
- $("#detectButton").click(function(){
- $.getJSON('/detect?text=' + encodeURIComponent($("#detectText").val()), function(obj, textStatus, jqXHR){
- var data = obj["data"];
- var labels = obj["labels"];
- var prob = obj["prob"];
-
- var feathead = $("<tr/>")
- $("#featHead").empty().append(feathead);
- var probhead = $("<tr/>")
- $("#probHead").empty().append(probhead);
- var probbody = $("<tr/>")
- $("#probBody").empty().append(probbody);
-
- feathead.append($("<th/>").text("id"));
- feathead.append($("<th/>").text("text"));
- for (var i=0;i<labels.length;++i) {
- feathead.append($("<th/>").text(labels[i]));
- probhead.append($("<th/>").text(labels[i]));
- probbody.append($("<td/>").text(prob[i]));
- }
-
- var body = $("#featBody").empty();
- for (var j=0;j<data.length;++j) {
- var item = data[j];
- var col = $("<tr/>")
- body.append(col);
- col.append($("<td/>").text(item["id"]));
- col.append($("<td/>").text("'" + item["feature"] + "'"));
- var phi = item["phi"];
- for (var i=0;i<phi.length;++i) {
- col.append($("<td/>").text(phi[i]=="0.000"?"":phi[i]));
- }
- }
- });
- });
-});
-</script>
-
-
-</body>
-</html>
View
@@ -1,15 +1,21 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<title>ldig server</title>
+<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
+<style type="text/css">
+table { border-collapse: collapse; }
+td, th { border: 1px solid gray; padding: 2px; }
+td.c0 { text-align: right; background-color: #eeeeff; }
+td.c1 { text-align: right; background-color: #ddddff; }
+</style>
</head>
<body>
-<textarea id="detectText" cols="40" rows="4">
+<textarea id="detectText" cols="60" rows="4">
I am a pen.
</textarea>
-<input id="detectButton" type="button" value="detect"/>
+<!-- input id="detectButton" type="button" value="detect"/ -->
<table id="probTable">
<thead id="probHead"></thead>
@@ -23,45 +29,63 @@
<script lang="text/javascript">
-$(document).ready(function() {
- $("#detectButton").click(function(){
- $.getJSON('/detect?text=' + encodeURIComponent($("#detectText").val()), function(obj, textStatus, jqXHR){
- var data = obj["data"];
- var labels = obj["labels"];
- var prob = obj["prob"];
- var feathead = $("<tr/>")
- $("#featHead").empty().append(feathead);
- var probhead = $("<tr/>")
- $("#probHead").empty().append(probhead);
- var probbody = $("<tr/>")
- $("#probBody").empty().append(probbody);
+function sortHandler(i) {
+ return (function(){
+ var list = $("#featBody > tr");
+ list.detach();
+ list.sort(function(a,b){ return a.childNodes[i].innerText - 0 < b.childNodes[i].innerText - 0 ? 1 : -1 });
+ var body = $("#featBody");
+ for (var j=0;j<list.length;++j) {
+ body.append(list[j]);
+ }
+ });
+};
+
+function detectHanlder(obj, textStatus, jqXHR) {
+ var data = obj["data"];
+ var labels = obj["labels"];
+ var prob = obj["prob"];
+
+ var feathead = $("<tr/>");
+ $("#featHead").empty().append(feathead);
+ var probhead = $("<tr/>");
+ $("#probHead").empty().append(probhead);
+ var probbody = $("<tr/>");
+ $("#probBody").empty().append(probbody);
- feathead.append($("<th/>").text("id"));
- feathead.append($("<th/>").text("text"));
- for (var i=0;i<labels.length;++i) {
- feathead.append($("<th/>").text(labels[i]));
- probhead.append($("<th/>").text(labels[i]));
- probbody.append($("<td/>").text(prob[i]));
- }
+ feathead.append($("<th/>").text("id").click(sortHandler(0)));
+ feathead.append($("<th/>").text("text").click(sortHandler(1)));
- var body = $("#featBody").empty();
- for (var j=0;j<data.length;++j) {
- var item = data[j];
- var col = $("<tr/>")
- body.append(col);
- col.append($("<td/>").text(item["id"]));
- col.append($("<td/>").text('"' + item["feature"] + '"'));
- var phi = item["phi"];
- for (var i=0;i<phi.length;++i) {
- col.append($("<td/>").text(phi[i]=="0.000"?"":phi[i]));
- }
- }
- });
+ for (var i=0;i<labels.length;++i) {
+ probhead.append($("<th/>").text(labels[i]));
+ probbody.append($("<td/>").text(prob[i]));
+ feathead.append($("<th/>").text(labels[i]).click(sortHandler(i+2)));
+ }
+
+ var body = $("#featBody").empty();
+ for (var j=0;j<data.length;++j) {
+ var item = data[j];
+ var col = $("<tr/>")
+ body.append(col);
+ col.append($("<td/>").text(item["id"]));
+ col.append($("<td/>").text('"' + item["feature"] + '"'));
+ var phi = item["phi"];
+ var c = 0;
+ for (var i=0;i<phi.length;++i) {
+ col.append($("<td/>").addClass('c' + c).text(phi[i]=="0.000"?"":phi[i]));
+ c = 1 - c;
+ }
+ }
+}
+
+$(document).ready(function() {
+ $("#detectText").keyup(function(){
+ var text = $("#detectText").val();
+ if (text != "") $.getJSON('/detect?text=' + encodeURIComponent(text), detectHanlder);
});
});
-</script>
-
+</script>
</body>
</html>

0 comments on commit 9457d2d

Please sign in to comment.