codetyphon下调用flask+LTP实现中文分词

Delphi等pascal语言在自然语言处理,特别是中文分词方面控件和开源框架不多,相关后台服务程序不容易开发。本文介绍一种调用python实现这一功能的方法,方便大家取长补短,丰富服务端功能。在NLP方面中文分词CWS现如今LTP的准确率相当高。不过NLP和CWS一般都用python来实现。为了方便codetyphon,delphi等调用分词功能,所有用flask做了一个本机的HTTP服务程序。直接上python代码:

from flask import Flask, redirect, url_for, request, render_template
from waitress import serve
from ltp import LTP
 

app = Flask(__name__)

ltp = LTP()

@app.route('/')
def index():
  return {'message': 'ltp running'}


@app.route('/ltp')
def do_ltp():
  a =  request.args.get('a')
  output = ltp.pipeline( [a], tasks=["cws", "pos", "ner", "srl", "dep", "sdp"])
  return {'result' : output}


@app.route('/cws')
def do_cws():
  a =  request.args.get('a')
  output = ltp.pipeline( [a], tasks=["cws","pos"])
  return {'result' : {'cws':output.cws[0],'pos':output.pos[0]}}

@app.route('/srl')
def do_srl():
  a =  request.args.get('a')
  output = ltp.pipeline( [a], tasks=["srl"])
  return {'result' : output.srl[0]}



if __name__ == '__main__':
  app.run(host='127.0.0.1', port=5080)

现在可以用python XXX.py 启动服务。在codetyhon下,可以用httpcliet来调用。本例用mormot里面的TSimpleHttpClient参考代码如下:

var
   Clocal: TSimpleHttpClient;
   aa, I:integer;
   sraw, sR:rawUtf8;
   v,vr, vcws:variant;
begin
   Clocal:= TSimpleHttpClient.Create;
   try
   sraw:=  UnicodeStringToUtf8(widestring(edtFind.text));
   sraw:='http://127.0.0.1:5080/cws?a='+UrlEncode( sraw);
   memo2.Append(sraw);
    aa:= Clocal.Request(sraw,'GET','"Content-Type": "application/json"');
    sR:= clocal.Body ;
    memo2.Append(sR);
    v := _Json(sR);
    vr :=v.result ;
    vcws:=vr.cws;
    memo2.Append(TDocVariantData(vcws).ToJson)  ;
     memo2.Append(inttostr(vcws._Count));
    for I:=0 to vcws._Count -1 do
    begin
      memo2.Append(vcws.Value(I));
    end;

   finally
     clocal.FREE;
   end;
end;                                   

也可以用THTTPSend等类似组件代替,关键要注意字符串编码。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值