2 Words and Tokens

2.1 Words

在 NLP 语境下,词的考虑范围需要包括

词的度量

使用词作为 token 单位的缺点

2.2 Morphemes

本书对于 morpheme 的定义:minimal meaning-bearing unit in a language。需要区别于汉语教材中的定义:最小音义复合体。

morphological typology 研究词如何拆分成语素。不同语言的语素分析所聚焦的维度通常都会包括 :

使用语素作为 token 单位的缺点

2.3 Unicode

Unicode 是一个大小为 4 bytes 的 character set,其为每一个 character 都分配了一个 code point。

ASCII 是 Unicode 的一个子集,大小为 1 bytes,其中 0-31 和 127 为控制字符(供 teletype 使用,属于历史遗留产物),32-126 这 95 个字符是可打印字符,包括空格(32)、数字(48-57)、大小写英文字母(65-90、97-122)、标点符号等常见符号。

code point 对应的是语义上的 character,并不对应 glyph,character 的视觉标识由 fonts 决定

encoding 决定了实际内存储存中的字符的二进制表示

使用字符作为 token 单位的优缺

2.4 Subword Tokenization

BPE training:以字符串(字符作为单位)为例子阐述了 BPE 的核心流程:对于 corpus 中的每一个 word(包含前置空格),按照 adjacent token pair 出现频率从高到低不断合并形成新 token
BPE pseudo.png665
BPE encoder:利用学到的词汇条,逐条执行替换规则(这意味着 corpus 中存在的词一定会被作为一整个 token)

BPE in practice:

2.5 Corpora

谈论语料在现实中的多样性:

因此建立 corpus dataset 时需要构建 datasheet/data statement 来指明:

2.6 Regular Expression

character disjunction:[] [^] [1-3]
counter:* + ? *? +?
anchor:^ $ \b \B
disjunction:|
precedence:()
blackslash:special \\ \* \+ etc.,special \r\t\n\f,alias \d \D \w \W \s \S
lookahead assetion:(?=) (?!)
for substitution:capture group () non-capturing group (?:)

precision:reduce false position;recall:reduce false negatives

pretokenization example:r"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[ˆ\s\p{L}\p{N}]+|\s+(?!\S)|\s+"

2.7 Simple Unix Tools for Word Tokenization

没看

2.8 Rule-based Tokenization

BPE 是 data-based tokenization

没看

2.9 Minimal Edit Distance

和 embedding vector 的不同在于从形式层面比较 token 的相似性

the minimum edit distance between two strings 定义:the minimum edit distance minimum number of editing operations (operations like insertion, deletion, substitution) needed to transform one string into another.

an alignment is a correspondence between substrings of the two sequences.

最简单的最小编辑距离 Levenshtein distance,包括三种操作 insert delete substitution 且代价均为 1(同字符替换 0 代价)。变种:只包括 insert delete 且代价均为 1。

DP 可以 Θ(mn) 求解,m=len(source),n=len(target)

D[i,j]=min{D[i1,j]+del-cost(source[i])D[i,j1]+ins-cost(target[j])D[i1,j1]+sub-cost(source[i],target[j])