目录
4.1 Word Tokenisation: English
4.2 Word Tokenisation: Chinese
1. 为什么第一步要进行预处理?
语言是有构成的。作为人类,我们可以将这些文件分解成各个组成部分。为了理解语言,计算机也应该这样做。
2. 预处理的步骤
- Remove unwanted formatting (e.g. HTML) 删除不需要的格式(如HTML)
- 句子分割 Sentence segmentation: break documents into sentences 将文件分割成句子
- 词语标记化 Word tokenization: break sentences into words 将句子分成词语
- 词语规范化 Word normalization: transform words into canonical forms 词语化为规范形式
- 删除止损词 Stopword removal: delete unwanted words 删除不需要的词
3. Sentence Segmentation 句子分割
按标点分割:
- 最天真的方法是按照句子的标点符号进行分割
- 但当遇到:“U.S.dollar,...,Yahoo!“这类词的时候如果使用标点符号进行分割就会不恰当。
按照标点后有大写字母的规则进行分割:
- 可以使用正则表达式实现这个想法
- 但当遇到:”Mr.Brown“这类词的时候虽然也满足正则表达式,但是分割的不恰当。
按照词典分割:
- 这似乎是一个更好的办法,但是我们很难罗列出所有的名字和缩写。
总的来说,我们没有合适的规则用于句子分割,应该使用 machine learning。
3.1 Binary Classifier 二分类器
- Looks at every "." and decides whether it is the end of a sentence.
- Decision trees, logistic regression
- Features
- Look at the words before and after ".”
- Word shapes:
- Uppercase, lowercase, ALL_CAPS, number 大写、小写、ALL_CAPS、数字
- Character length 字符长度
- Part-of-speech tags
- Determiners tend to start a sentence 定语从句倾向于开始一个句子
4. Word Tokenisation
4.1 Word Tokenisation: English
- Naive approach: separate out alphabetic strings (\w+)
- Abbreviations (U.S.A.)
- Hyphens (merry-go-round vs. well-respected vs. yes-but)
- Numbers (1,000,00.01)
- Dates (3/1/2016)
- Clitics (n't in can't)
- Internet language (http://www.google.com,#metoo, :-))
- Multiword units (New Zealand)
4.2 Word Tokenisation: Chinese
- 一些亚洲语言的文字之间没有空格
- 在中文中,单词通常对应一个以上的字符
- 标准方法假设有一个现有词汇表
- MaxMatch algorithm
- 贪婪地匹配词汇中最长的单词
- 但我们如何知道词汇是什么?
- 而且并不总是有效
4.3 Subword Tokenisation
Colourless green ideas sleep furiously → [colour] [less] [green] [idea] [s] [sleep] [furious] [ly]
- 一种流行的算法:字节对编码(Byte-Pair coding,BPE)
- 核心思想: 迭代地合并频繁对字符
- Advantage:
- 数据知情的标记化
- 适用于不同语言
- 更好地处理未知单词
4.3.1 Byte-Pair Encoding
语料库:
- [5] l o w _
- [2] l o w e s t _
- [6] n ew er_
- [3] w i d er_
- [2] n ew _
词汇:
- 在实践中,BPE 将运行数千次合并,创建大量词汇
- 最常用的单词将用完整单词表示
- 更罕见的词会被分解成子词
- 在最坏的情况下,测试数据中的未知单词将被分解为单个字母
5. Word Normalisation
- Lower casing (Australia = australia)
- Removing morphology (cooking 一 cook)
- Correcting spelling (definately = definitely)
- Expanding abbreviations (U.S.A = USA)
Goal:
- Reduce vocabulary 减少词汇量
- Maps words into the same type 将单词映射为同一类型
5.1 Inflectional Morphology
- Inflectional Morphology 创造语法变体
- 英语使用名词,动词和形容词
- 名词:名词 (- s) 的数量
- 动词:动作的主语数量 (- S)、动作的体态 (- ing) 和动作的时态 (ed)
- 形容词:比较级 (- er) 和最高级 (- est)
- 许多语言的 Inflectional Morphology 比英语丰富得多
- 例如,法语中的名词与性别有关(un chat,une chat)
5.2 Lemmatisation
- 词素化意味着去掉任何变形,以达到未变形的形式,即引理
- speaking → speak
- 在英语中,有一些不规则之处阻碍了琐碎的解决方案:
- poked → poke (not pok)
- stopping → stop (not stopp)
- watches → watch (not watche)
- was → be (not wa)
- 准确引理化所需的引理词典
5.3 Derivational Morphology
- 派生词法创造独特的词汇
- 英语派生词后缀经常改变词类,例如:
- -ly (personal → personally)
- -ise (final → finalise)
- -er (write → writer)
- 英语派生前缀常常在不改变词类的情况下改变意思
- write → rewrite
- healthy → unhealthy
5.4 Stemming
- 去掉所有后缀,留下一个词干
- E.g. automate, automatic, automation → automat
- Often not an actual lexical item
- 通常不是一个实际的词汇项目
- 词汇稀疏性甚至比词素化还要小
- 在信息检索中很受欢迎
- Stem 并不总是可以解释的
5.5 The Porter Stemmer
- 最受欢迎的英语词干
- 分阶段应用重写规则
- 第一条语气词后缀
- E.g. -ies → -i
- 派生后缀
- E.g -isation → -ise → -i
- 第一条语气词后缀
- c (lowercase) = consonant 辅音; e.g. ‘b’, ‘c’, ‘d’
- v (lowercase) = vowel 元音; e.g. ‘a’, ‘e’, ‘i’, ‘o’, ‘u’
- C = a sequence of consonants 一系列辅音
- s, ss, tr, bl
- V = a sequence of vowels 一系列元音
- o, oo, ee, io
一个词有下面四种形式之一:
- CVCV ... C
- CVCV ... V
- VCVC ... C
- VCVC ... V
它可以表示为:
- [C]VCVC … [V]
- [C] (VC)^m [V]
- m = measure
- m=0: TR, EE, TREE, Y, BY
- m=1: TROUBLE, OATS, TREES, IVY
- m=2: TROUBLES, PRIVATE, OATEN, ORRERY
6. Stopword Removal
6.1 Stop Words
定义:将从文件中删除的词语清单
- 典型的单词包(BW)表示
- 当顺序很重要的时候不合适
如何选择?
- 所有 closed-class 或 function 虚词。例如,a,of,tor,he,...
- 任何高频词
- NLTK,spaCy NLP toolkits
6.2 A Final Word
- 文本分析中不可避免的预处理
- 可能对下游应用程序产生重大影响
- 确切的步骤可能因语料、任务而异
- 简单的基于规则的系统工作得很好,但很少完美
- Language-dependent