最近在做項目的敏感詞庫篩選更新。筆者最終的目標是通過百度API將現有的敏感詞庫篩選更新成。
一、準備工作
讀者需在百度智能雲登陸賬號,然後開通百度內容審覈功能,然後根據API Key和Secret Key可以獲得調用接口所需的access_token。
二、代碼調用API進行篩選
獲得API後就是寫代碼進行篩選了,筆者Python代碼如下:
import requests
import json
#獲取審覈結果
def get_result(data_str):
params = {'text': data_str}
request_url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=【此處寫你自己的token】'
result = requests.post(request_url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=params).text
predict_res = json.loads(result)
print(predict_res)
return predict_res['conclusion']
#讀取待測文本
def get_txt():
with open('./待篩選文本/po/drug.txt', 'r', encoding='utf-8') as f:
with open('./篩選結果/drug_檢測失效.txt', 'a', encoding='utf-8') as f_err:
with open('./篩選結果/po/drug.txt', 'a', encoding='utf-8') as f_result:
for line in f:
# print(line.replace('\n',''))
result = get_result(line.replace('\n',''))
if result == "疑似":
f_result.write(line)
elif result == "不合規":
f_result.write(line)
elif result == "合規":
print(line.replace('\n','')+"檢測結果爲合規")
else:
print(line.replace('\n','')+"檢測失效")
f_err.write(line)
#主函數
if __name__ == '__main__':
get_txt()
print("篩選結束")
三、篩選結果
敏感詞篩選分爲兩個部分:中文敏感詞和藏文敏感詞。中文部分的敏感詞除了分爲六個大類外,還將敏感詞分爲正向敏感詞和負向敏感詞。藏文敏感詞則只是將敏感詞分爲六個大類,未分正負向:
篩選前後的敏感詞庫已經上傳到CSDN,需要的讀者可自行下載:https://download.csdn.net/download/m0_37872090/12274456
該詞庫僅用於技術測試,嚴禁用於違法活動!