下载依赖:
pip install bert-serving-server
pip install bert-serving-client
下载预训练好的中文模型:
中文模型地址
启动bert-serving-start
$ cd /usr/local/bin
# 替换为自己的地址
$ bert-serving-start -model_dir /bertdemo/chinese_L-12_H-768_A-12
编写脚本
# train.py
from bert_serving.client import BertClient
bc = BertClient()
result = []
value = 0.90
# 替换文件地址
f = open('/data/source_word/all.txt','r',encoding='utf-8');
# 替换文件地址
r = open('/project/bertdemo/all.txt','a',encoding='utf-8')
for line in f:
result.append(line.strip('\n'))
#print(result)
input = bc.encode(result)
for line in input:
r.write(str(line))
print(r)
运行脚本
python3 train.py