许多自然语言处理程序中都用到了谷歌开源的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进行调用,其结果呈现如下:

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐