TextGrocery短文本分类使用

来源:互联网 发布:英语朗读软件 编辑:程序博客网 时间:2024/04/28 20:23

TextGrocery是一个基于LibLinear和结巴分词的短文本分类工具,特点是高效易用,同时支持中文和英文语料。
GitHub项目链接

具体的使用可以参考:文档

1、安装

通过GitHub(最新版本)git clone https://github.com/2shou/TextGrocery.git --depth=1cd TextGrocerymake

2、通过pip(更稳定)

pip install tgrocery -i http://pypi.douban.com/simple  (这是国内的镜像源)

3、样本数据: train_ch.txt

music:你想干什么music:星火燎原music:日出(live版)music:给我感觉(live版)music:High High High(live版)music:姊妹(live版)music:站在高岗上(live版)music:感应(live版)music:了不起(live版)poem:中秋夜poem:上堂开示颂poem:从军行poem:丹阳送韦参军poem:乌衣巷poem:出塞poem:别董大poem:剑客poem:兰溪棹歌why:大飞机为什么怕小鸟why:臭鼬怎么保护自己why:冬天_嘴里为什么冒白气why:春季天空中飘的小白花是什么why:冬天为什么会冻伤呢why:飞机驾驶员怎样知道飞机的飞行高度why:冬暖夏凉的井水qa:你老家在哪?qa:你从哪里来?qa:你爸爸呢?qa:你妈妈呢?qa:你爸爸对你好吗?qa:你妈妈对你好吗?

4、 部署一个server.py脚本

#-*- coding:utf-8 -*-import BaseHTTPServerimport urllibfrom tgrocery import Grocerygrocery = Grocery('hello')grocery.train('train_ch.txt',':')grocery.save()new_grocery = Grocery('hello')new_grocery.load()class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):    '''处理请求并返回页面'''    # 页面模板    Page = '''cccccc    '''    # 处理一个GET请求    def do_GET(self):        #print self.path.split('/')[-1]        retText = classify(self.path.split('/')[-1])        print retText        self.send_response(200)        #self.send_header("Content-Type", "text/html")        self.send_header('Content-Type', 'application/json')        self.send_header("Content-Length", str(len(retText)))        self.end_headers()        self.wfile.write(retText)#----------------------------------------------------------------------def classify(text):    input_txt = urllib.unquote(text)    predict_result = new_grocery.predict(input_txt)    output_txt = predict_result.predicted_y    dict = predict_result.dec_values    if dict[predict_result.predicted_y] < 0.15 :        output_txt = ''    return output_txt#----------------------------------------------------------------------if __name__ == '__main__':#启动: nohup python server.py > server.log 2>&1 &    try:        serverAddress = ('', 8181)        server = BaseHTTPServer.HTTPServer(serverAddress, RequestHandler)        server.serve_forever()    except KeyboardInterrupt:        print '^C received ,shutting down server'

5、访问:http://ip:8181/上堂开示颂

0 0
原创粉丝点击