很简单,可是却也折腾了不是时间,走了不少弯路。在此备注记录一下,以供后需。
# web_server.py
#!/usr/bin/env python # coding=utf-8 import os.path import tornado.httpserver import tornado.options import tornado.web from auto_gen_jd import AutoGenJD from jd_parser import JdParser from tornado.options import define,options import sys reload(sys) sys.setdefaultencoding('utf-8') define("port",default = 8086,help="run on the given port",type=int) class IndexHandler(tornado.web.RequestHandler): def get(self): result = self.get_argument("result",[]) num = self.get_argument("num",5) source_text = self.get_argument("source","") self.render("inputgen.html",source = source_text,result = result,num=num) def post(self): result = self.get_argument("result",[]) source_text = self.get_argument("source","") num = self.get_argument("num",5).encode("utf-8") result = genjd.get_jd_with_kmeans(source_text,num) self.render("outputgen.html",source = source_text,result = result,num=num) class GenJdHandler(tornado.web.RequestHandler): def get(self): result = self.get_argument("result",[]) num = self.get_argument("num",5) source_text = self.get_argument("source","") self.render("inputgen.html",source = source_text,result = result,num=num) def post(self): result = self.get_argument("result",[]) source_text = self.get_argument("source","") num = self.get_argument("num",5).encode("utf-8") result = genjd.get_jd_with_kmeans(source_text,num) self.render("outputgen.html",source = source_text,result = result,num=num) class JdParserHandler(tornado.web.RequestHandler): def get(self): result = self.get_argument("result",[]) source_text = self.get_argument("source","") self.render("inputjd.html",source=source_text,result=result) def post(self): source_text = self.get_argument("source","") result = extr.split_multi_jd(source_text) self.render("outputjd.html",source=source_text,result=result) if __name__ == "__main__": genjd = AutoGenJD() extr = JdParser() tornado.options.parse_command_line() app = tornado.web.Application( handlers = [(r'/',IndexHandler),(r'/genjd',GenJdHandler),(r'/jdparser',JdParserHandler)], template_path = os.path.join(os.path.dirname(__file__),"templates"), debug=True, autoescape=None, ) print "starting tornado..." http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
模板文件,共有四个,大体雷同:
outputjd.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" > <title>技能在线抽取</title> <link rel = "stylesheet" href="http://cdn.amazeui.org/amazeui/2.4.0/css/amazeui.min.css"> <link rel = "icon" href="head.ico" > <script> function get_result(){ document.getElementById("res_hint").innerHTML='<div class="am-active">'+'解析结果:'+'</div>'; return true } </script> </head> <body> {% autoescape None %} <div class="am-topbar"> <h1 class="am-active">在线演示</h1> </div> <div class="am-g"> <div class="am-u-sm-7 am-u-sm-centered"> <div class="am-intro-left am-active" style="height:80px"> <label ><em>使用说明</em></label> <p class="am-serif">粘贴企业招聘信息到文本框,再点击提交,可获得jd解析结果。<a href="/genjd" title="自动生成JD">自动生成JD</a></p><br /> </div> <form class="am-form am-form-horizontal" role="form" action="/jdparser" method="post" onsubmit="return get_result()"> <textarea class="am-form-field am-radius" name="source" rows="20" cols="110" placeholder="input job description here" autofocus="True" >{{source}}</textarea> <div class="am-u-sm-centered" style="text-align:center"> <button type="submit" onclick="get_result()" class="am-btn am-btn-primary" value="提交">submit</button> </div> </form> </div> <br /> <div class="am-u-sm-6 am-u-sm-centered"> <div id='res_hint'></div> <div id="result"> {% for i,jd in enumerate(result,1) %} <br /> jd #{{ i }}==================== <br /> {% set hint = {'sex':'年龄','cert':"证书","pay":"薪酬","job_tag":"职业标注","inc_tag":"公司标注","inc_name":"公司标注","demand":"工作要求","duty":"工作内容","benefits":"福利待遇","other":"其它未处理的句子","pub_time":"发布时间","end_time":"截止时间"} %} <ul> {% for k,v in jd.iteritems() %} {% if k=="demand" or k=="duty" or k=="benefits" or k=="other" %} <li title={{hint[k]}}>{{k}}: <pre>{{ v }} </pre></li> {% else %} <li title={{k}}>{{k}} :<pre/> {%raw v %}</pre></li> {% end %}{% end %}{% end %} </ul> </div> <script type="text/javascript" src="http://cdn.amazeui.org/amazeui/2.4.0/js/amazeui.min.js"</script> </body> </html>
outputgen.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" > <title>自动生成JD</title> <link rel = "stylesheet" href="http://cdn.amazeui.org/amazeui/2.4.0/css/amazeui.min.css"> <link rel = "icon" href="head.ico" > <script> function get_result(){ document.getElementById("res_hint").innerHTML='<p class="am-active">正在生成结果,请稍等...'+'<i class="am-icon-spinner am-icon-spin"></i></p>'; document.getElementById("res_hint").style.color="#c60a00"; num = document.getElementById("num").value; jobname = document.getElementById("jobname").value; return true } </script> </head> <body> <div class="am-topbar"> <h1 class="am-active">在线演示</h1> </div> <div class="am-g"> <div class="am-u-sm-6 am-u-sm-centered"> <div class="am-intro-left" style="height:180px"> <label class="am-active"><em>使用说明</em></label> <p class="am-serif">输入职位名和所需生成jd句子数,再点击提交,可获得jd结果。</p><br /> </div> <form class="am-form-inline" role="form" action="/genjd" method="post" onsubmit="return get_result()"> <input id="jobname" class="am-form-field am-radius" type="text" name="source" style="500px" placeholder="input jobname here" autofocus="True" value={{source}}></input> <input id="num" class="am-form-field am-round" type="number" min="1" max="50" name="num" title="input number of sententces" value={{num}}></input> <button type="submit" onclick="get_result()" class="am-btn am-btn-primary" value="提交">submit</button> </form> </div> <br /> <div class="am-u-sm-6 am-u-sm-centered"> <div id='res_hint' class="am-active">生成结果: <a href="/jdparser" title="jd解析">JD解析</a></div> <div id="result" style="background-color:#f8f8f8"> <ul > {% for i,s in enumerate(result) %} <li>{{i+1}}. {{s}} </li> {%end%} </ul> </div> <script type="text/javascript" src="http://cdn.amazeui.org/amazeui/2.4.0/js/amazeui.min.js"></script> </body> </html>