本文主要是介绍SentencePiece的中文测试实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
许多自然语言处理程序中都用到了谷歌开源的SentencePiece作为词切分的基础工作之一,于是跟踪学习了下。
1、基本介绍
What is SentencePiece?
SentencePiece is a re-implementation of sub-word units, an effective way to alleviate the open vocabulary problems in neural machine translation. SentencePiece supports two segmentation algorithms, byte-pair-encoding (BPE) [Sennrich et al.] and unigram language model [Kudo.]. Here are the high level differences from other implementations.
从上面的介绍可以看出,这是一种词切分技术,可为NMT翻译提供重要支撑。里面提供了两种切词算法,BPE和unigram词模型。
2、安装
安装在官网上提供了两种,一种是python包,另外一种是c++,由于官网只提供英语和日本语的处理,而为了实现中文处理,需要安装C++版本。
安装步骤如下所示,不过由于只需要用到spm_train,因此不需要make install.
% cd /path/to/sentencepiece
% mkdir build
% cd build
% cmake ..
% make -j $(nproc)
% sudo make install
% sudo ldconfig -v
安装效果图如下所示:
3、分词训练
以某部小说的TXT作为导入开始训练,其训练语句如下所示:
ndscai@ndscaigpu01:~/downloads/201907/sentencepiece-master/build/src$ ./spm_train --input=./bingwang.txt --model_prefix=/home/ndscai/downloads/201907/bw
训练完成后,利用python进行调用,其结果呈现如下:
这篇关于SentencePiece的中文测试实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!