• 分词工具使用系列——sentencepiece使用


    分词工具使用系列

    第一章 sentencepiece使用
    第二章 jieba工具使用



    前言——细说分词

    分词是干啥的:

    分词的目的就是找到构成句子的基本单位,然后模型学习这些基本单位组合的概率情况,完成语言模型的构建。
    分词的工具主要就是两个任务:

    • 使用分词算法(前向后向匹配,单个词划分,字母划分,语言模型划分)构建分词后的字典
    • 根据字典的分词排序对完整句子做分词,实现句子到分词ID的双向转换

    分词算法有

    • word-based: 使用空格,标点进行分割(英文就是空格,中文就是单个汉字)
    • character-based: 使用abcde这样的字符(大小写字母,标点256个)来分割
    • Subword-based:介于单个词和字母之间,使用算法寻找单元组合出现概率最大的作为一个分词结果(英文的基本单位组合就是字母,中文就是一个汉字)
      • BPE分词
      • BPE分词的改进
        • WordPiece
        • Byte-level BPE
        • SentencePiece
        • Unigram

    BPE分词细说

    BPE分词可以压缩单个词划分的词典大小,也能扩充仅仅由字母表的组成的词典

    在这里插入图片描述

    在BPE分词基础上改进的算法就是改进了寻找字母组合的方式,由原始的数数统计改为由模型学习获得,代表性的就是unigram

    一、sentencepiece是什么?

    sentencepiece地址

    SentencePiece是一个无监督的文本标记器和去标记器,主要用于基于神经网络的文本生成系统,其中词汇量在神经模型训练之前就已经预先确定了。SentencePiece实现了子词单元(例如,字节对编码(BPE)[Sennrich et al.])和unigram语言模型[Kudo.]),具有从原始句子直接训练的扩展性。SentencePiece允许我们做一个纯粹的端到端系统,不依赖于特定语言的前/后处理。

    二、sentencepiece使用步骤

    🥌准备文本

    数据格式的准确是模型能够正确运行的重要前提

    sentencepiece的输入数据格式就是一行文本占一行,保存在txt文件里,每行文本的内容完整最好,长度不限,保证内容语义完整即可。参考内容如下:

    🥌训练模型

    训练模型就一段代码十分简单

    
    import sentencepiece as spm
    
    # train sentencepiece model from `botchan.txt` and makes `m.model` and `m.vocab`
    # `m.vocab` is just a reference. not used in the segmentation.
    spm.SentencePieceTrainer.train('--input=aishell1.txt --model_prefix=m --vocab_size=5000 --model_type=bpe')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    完整教程地址,下载后解压,python文件里说明了所有的用法

    # 可选参数
    --control_symbols=<foo>,<bar> 
    # https://github.com/google/sentencepiece/blob/master/doc/special_symbols.md
    --user_defined_symbols=<user1>,<user2> 
    --input=<input file> 
    --model_prefix=<model file> 
    --vocab_size=8000
    # 分词类型
    --model_type=char/word/bep/unigram
    # 文本归一化
    --normalization_rule_name=nfkc_cf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    🥌使用模型

    # makes segmenter instance and loads the model file (m.model)
    sp = spm.SentencePieceProcessor()
    sp.load('m.model')
    
    # # encode: text => id
    print(sp.encode_as_pieces('你好是一个汉语词语'))
    print(sp.encode_as_ids('你好是一个汉语词语'))
    #
    # # decode: id => text
    print(sp.decode_pieces(['▁你', '好', '是', '一', '个', '汉', '语', '词', '语']))
    print(sp.decode_ids([547, 2280, 2172, 2170, 2200, 2527, 3198, 3643, 3198]))
    
    # 支持统计字词的偏移量
    # One best result
    spt = sentencepiece_pb2.SentencePieceText()
    spt.ParseFromString(sp.encode_as_serialized_proto('hello')) # Full width hello
    
    # begin/end (offsets) are pointing to the original input.
    print(spt)
    
    # Nbest results
    nspt = sentencepiece_pb2.NBestSentencePieceText()
    nspt.ParseFromString(sp.nbest_encode_as_serialized_proto('hello', 5))
    # print(nspt)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    公众号同步更新欢迎关注

  • 相关阅读:
    【Oracle】Oracle系列之九--Oracle常用函数
    瀑布流布局
    尚硅谷设计模式学习(八)桥接模式
    Ext JS 如何定义公用方法(单例类 or 静态方法)
    EdgeX Foundry 边缘物联网中间件平台
    基于 socketio 的 room 的使用
    windows11中安装curl
    教学案例五 循环结构
    深入理解Java ArrayList集合及其源码详解
    openresty+etcd配置实现原理
  • 原文地址:https://blog.csdn.net/qq_37771209/article/details/127664462