# sentencepiece
**Repository Path**: src-openeuler/sentencepiece
## Basic Information
- **Project Name**: sentencepiece
- **Description**: An unsupervised text tokenizer and detokenizer.
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 6
- **Created**: 2021-09-02
- **Last Updated**: 2025-08-06
## Categories & Tags
**Categories**: Uncategorized
**Tags**: AI
## README
# sentencepiece
#### 介绍
An unsupervised text tokenizer and detokenizer.
#### 软件架构
软件架构说明
#### 安装教程
1. Python模块
SentencePiece 提供了支持 SentencePiece 训练和分割的 Python 包装器。你可以安装 SentencePiece 的 Python 二进制包。
% pip install sentencepiece
2. 从 C++ 源代码构建和安装 SentencePiece 命令行工具
构建 SentencePiece 需要以下工具和库:
* make
* C++11编译器
* gperftools库(可选,可以获得 10-40% 的性能提升。)
在 Ubuntu 上,可以使用 apt-get 安装构建工具:
% sudo apt-get install cmake build-essential pkg-config libgoogle-perftools-dev
然后,您可以按如下方式构建和安装命令行工具。
% git clone https://github.com/google/sentencepiece.git
% cd sentencepiece
% mkdir build
% cd build
% cmake ..
% make -j $(nproc)
% sudo make install
% sudo ldconfig -v
在 OSX/macOS 上,将最后一个命令替换为 sudo update_dyld_shared_cache
3. 用 vcpkg 构建和安装
您可以使用vcpkg依赖项管理器下载并安装句子:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install sentencepiece
vcpkg 中的sentencepiece端口由 Microsoft 团队成员和社区贡献者保持最新。
#### 使用说明
1. 训练句子模型
% spm_train --input= --model_prefix= --vocab_size=8000 --character_coverage=1.0 --model_type=
* --input:每行一个句子的原始语料库文件。无需运行分词器、规范器或预处理器。默认情况下,SentencePiece 使用 Unicode NFKC 规范化输入。您可以传递逗号分隔的文件列表。
* --model_prefix: 输出模型名称前缀。.model并.vocab生成。
* --vocab_size: 词汇量,例如 8000、16000 或 32000
* --character_coverage: 模型覆盖的字符数,好的默认值是:0.9995对于具有丰富字符集的语言,如日语或中文,1.0以及其他具有小字符集的语言。
* --model_type: 型号。从unigram(默认)bpe、char、 或 中选择word。使用wordtype时,输入的句子必须预先标记。
2. 将原始文本编码为句子片段/id
% spm_encode --model= --output_format=piece < input > output
% spm_encode --model= --output_format=id < input > output
使用--extra_optionsflag 插入 BOS/EOS 标记或反转输入顺序。
% spm_encode --extra_options=eos (add only)
% spm_encode --extra_options=bos:eos (add and )
% spm_encode --extra_options=reverse:bos:eos (reverse input and add and )
SentencePiece 支持 nbest 分割和带--output_format=(nbest|sample)_(piece|id)标志的分割采样。
% spm_encode --model= --output_format=sample_piece --nbest_size=-1 --alpha=0.5 < input > output
% spm_encode --model= --output_format=nbest_id --nbest_size=10 < input > output
3. 将句子片段/id 解码为原始文本
% spm_decode --model= --input_format=piece < input > output
% spm_decode --model= --input_format=id < input > output
使用--extra_options标志以相反的顺序解码文本。
% spm_decode --extra_options=reverse < input > output
4. 端到端示例
% spm_train --input=data/botchan.txt --model_prefix=m --vocab_size=1000
unigram_model_trainer.cc(494) LOG(INFO) Starts training with :
input: "../data/botchan.txt"
...
unigram_model_trainer.cc(529) LOG(INFO) EM sub_iter=1 size=1100 obj=10.4973 num_tokens=37630 num_tokens/piece=34.2091
trainer_interface.cc(272) LOG(INFO) Saving model: m.model
trainer_interface.cc(281) LOG(INFO) Saving vocabs: m.vocab
% echo "I saw a girl with a telescope." | spm_encode --model=m.model
▁I ▁saw ▁a ▁girl ▁with ▁a ▁ te le s c o pe .
% echo "I saw a girl with a telescope." | spm_encode --model=m.model --output_format=id
9 459 11 939 44 11 4 142 82 8 28 21 132 6
% echo "9 459 11 939 44 11 4 142 82 8 28 21 132 6" | spm_decode --model=m.model --input_format=id
I saw a girl with a telescope.
可以发现原来的输入句是从词表id序列中还原出来的。
5. 导出词汇表
% spm_export_vocab --model= --output=