Baidu AI Raiders: Text Correction

Call Raiders (Python3)

First of all authentication and authorization:

Need to make authentication and authorization before you start calling any API, please refer to the specific instructions:

http://ai.baidu.com/docs#/Auth/top

Gets Access Token

An authorized service address https://aip.baidubce.com/oauth/2.0/token send a request (recommended POST), and bring the following parameters in the URL:

grant_type :? parameters must be fixed at client_credentials;

client_id :? must parameters, API Key applications;

client_secret :? must parameters, application of Secret Key;

E.g:

https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&

DETAILED Python3 code is as follows:

# -*- coding: utf-8 -*-

#!/usr/bin/env python

import urllib

import json

#client_id official net acquisition of AK, client_secret official network acquired SK

client_id = [AK Baidu cloud applications]

client_secret = [SK] Baidu cloud applications

# Get token

def get_token():

    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret

    request = urllib.request.Request(host)

    request.add_header('Content-Type', 'application/json; charset=UTF-8')

    response = urllib.request.urlopen(request)

    token_content = response.read()

    if token_content:

        token_info = json.loads(token_content)

        token_key = token_info['access_token']

    return token_key

Text correction call:

For more information, refer to: http://ai.baidu.com/docs#/NLP-API/741e48da

Interface Description

Identifying an error in the input text segment, an error results and gives the correct text. Support short text, long text, voice and other identifying an error, the error correction is a search engine, one of the basic voice recognition module, content review and other functions run better.

Request Description

HTTP methods: POST

Request the URL of:  https://aip.baidubce.com/rpc/2.0/nlp/v1/ecnet

URL parameters:

Parameter Value

access_token acquired by API Key and Secret Key access_token, reference "Access Token obtain"

Header follows:

Parameter Value

Content-Type application/json

Body request example:

{

"Text": "Baidu is a manual only company"

}

Request parameter

Parameter Type Description Required

Correction text text string to be input limit required 511 bytes

Returning to the description

Parameter Description Description

log_id uint64 request unique identifier

correct_query string 纠错后的文本

score double 模型置信度打分

item object 分析结果

+vec_fragment list 替换候选片段信息

++ori_frag string 原片段

++correct_frag double 替换片段

++begin_pos int 起始(长度单位)

++end_pos list 结尾(长度单位)

Python3调用代码如下:

#调用百度文本纠错接口

def txt_correction(content):

    print ('原文:',content)

    token=get_token()

    url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/ecnet'

    params = dict()

    params['text'] = content

    params = json.dumps(params).encode('utf-8')

    access_token = token

    url = url + "?access_token=" + access_token

    request = urllib.request.Request(url=url, data=params)

    request.add_header('Content-Type', 'application/json')

    response = urllib.request.urlopen(request)

    content = response.read()

    if content:

        content=content.decode('GB2312')

        data = json.loads(content)

        item=data['item']

        print('纠错后:',item['correct_query'])

        print('Score:',item['score'])

txt_correction('汽车形式在这条道路上')

返回结果:

原文: 汽车形式在这条道路上

纠错后: 汽车行驶在这条道路上

Score: 0.982835

测试:

选用不同的数据对文本纠错的准确性进行测试,具体的案例包括生活用语,文章,古诗等。具体案例如下:

test_list = [

            '我来自夏门',

            '朝辞白底彩云间',

            '时光葱葱而过',

            '形像代言人',

            '此事不会影像大局',

            '化夏子孙团结一心',

            '他有点必理不平衡。',

            '这是我们最新的研发计画。',

            '我早就在这排对了。',

            '你中于来了。']

import datetime

print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

for text in test_list:

    txt_correction(text)

print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

结果:

2019-04-16 11:29:52

原文: 我来自夏门

纠错后: 我来自夏门

Score: 0

原文: 朝辞白底彩云间

纠错后: 朝辞白帝彩云间

Score: 0.961476

原文: 时光葱葱而过

纠错后: 时光匆匆而过

Score: 0.938082

原文: 形像代言人

纠错后: 形象代言人

Score: 0.947005

原文: 此事不会影像大局

纠错后: 此事不会影响大局

Score: 0.969137

原文: 化夏子孙团结一心

纠错后: 华夏子孙团结一心

Score: 0.885656

原文: 他有点必理不平衡。

纠错后: 他有点必理不平衡。

Score: 0

原文: 这是我们最新的研发计画。

纠错后: 这是我们最新的研发计划。

Score: 0.441279

原文: 我早就在这排对了。

纠错后: 我早就在这排队了。

Score: 0.977007

原文: 你中于来了。

纠错后: 你中于来了。

Score: 0

2019-04-16 11:30:19

发现在10个测试案例中,有7个错误被成功纠正。纠正成功率为70%左右,感觉不错,不过还有优化空间。随着百度不断地优化应该会有进一步的提高。

10个调用,用时27秒,每次调用2.7秒左右。

使用场景及建议:

文本纠错有很广阔的应用前景,现在已经有了以下场景:

写作辅助:在内容写作平台上内嵌纠错模块,可在作者写作时自动检查并提示错别字情况。从而降低因疏忽导致的错误表述,有效提升作者的文章写作质量,同时给用户更好的阅读体验

搜索纠错:用户经常在搜索时输入错误,通过分析搜索query的形式和特征,可自动纠正搜索query并提示用户,进而给出更符合用户需求的搜索结果,有效屏蔽错别字对用户真实需求的影响

对话语音识别纠错:将文本纠错嵌入对话系统中,可自动修正语音识别转文本过程中的错别字,向对话理解系统传递纠错后的正确query,明显提高语音识别准确率,使产品整体体验更佳

此外还可以应用在:

NLP数据预处理:如在对客服记录,景区评价的数据进行客户情绪分析前可以使用文本纠错功能对数据进行预处理,提高分析的准确度。

单据扫描信息的后处理:对单据扫描文本识别后的数据进行后处理,提高准确度

在自然语言相关的AI应用越来越多的时候,文本纠错作为NLP应用的关键组件,必将得到广泛的应用。在试验中发现,百度AI作为这方面的先行者,已经具备了很好的易用性和调用速度,希望能进一步提高识别的准确率,百尺竿头更进一步。


Guess you like

Origin blog.51cto.com/14664861/2466904