- WordPiece是一种用于分词的子词划分算法,广泛应用于自然语言处理(NLP)任务中,代码中实现如下,使用贪心的方式进行编码,每次选择最长匹配的子串(i 到 j 之间的子串),并将对应的标记存储在 tokens 数组中
// apply wordpiece
for (const auto &word : words) {
if (word.size() == 0)
continue;
std::string prefix = "";
int i = 0;
int n = word.size();
loop:
while (i < n) {
if (t >= n_max_tokens - 1)
break;
int j = n;
while (j > i) {
auto it = token_map->find(prefix + word.substr(i, j - i));
if (it != token_map->end()) {
tokens[t++] = it->second;
i = j;
prefix = "##";
goto loop;
}
--j;
}
if (j == i) {
fprintf(stderr, "%s: unknown token '%s'\n", __func__, word.substr(i, 1).data());
prefix = "##";
++i;
}
}
}
转成可独立运行的代码如下:
#include <iostream>
#include <unordered_map>
#include <vector>
int main() {
std::vector<std::string> words = {"用法解析"};
const int n_max_tokens = 10; // 最大标记数目,可以根据需求调整
std::unordered_map<std::string, int> tokenmap = {
{"用法",1},
{"##解析",2},
{"##析",3} };; // 假设存在一个标记映射表
std::unordered_map<std::string, int> *token_map = &tokenmap;
std::vector<int> tokens(n_max_tokens, 0); // 存储标记的数组
int t = 0; // 当前标记的索引
// 应用 WordPiece 分词算法
for (const auto &word : words) {
if (word.size() == 0)
continue;
std::string prefix = "";
int i = 0;
int n = word.size();
loop:// loop是一个标签(label),在这段 C++ 代码中,它被用作跳转语句 goto 的目标
while (i < n) {
if (t >= n_max_tokens - 1)
break;
int j = n;
while (j > i) {
auto it = token_map->find(prefix + word.substr(i, j - i));
if (it != token_map->end()) {
tokens[t++] = it->second;
i = j;
prefix = "##";
goto loop;
}
--j;
}
// 处理未知的单个字符,可以根据需求进行适当的处理
if (j == i) {
fprintf(stderr, "%s: unknown token '%s'\n", __func__, word.substr(i, 1).c_str());
prefix = "##";
++i;
}
}
}
// 输出处理后的标记,输出应为:Tokens: 1 2
std::cout << "Tokens: ";
for (int i = 0; i < t; ++i) {
std::cout << tokens[i] << " ";
}
std::cout << std::endl;
return 0;
}
CG
-
WordPiece在 Japanese and Korean Voice Search 中提出
-
git clone https://github.com/a2409895438/wordpiece.git
-
cmake -B ./build
// -B选项用于指定构建目录的路径 -
cmake --build ./build
-
Rust 100.0%
Split tokens into word pieces https://github.com/danieldk/wordpieces -
分词是中文NLP的基础。系是不需要分词的。拉丁语言系不需要分词,因为他们的词语之间有空格分割,可以根据空格就可以把单词分开。