Text Preprocessing

打开 Google, 输入搜索关键词,显示上百条搜索结果

打开 Google Translate, 输入待翻译文本,翻译结果框中显示出翻译结果

以上二者的共同点便是文本预处理 Pre-Processing

在 NLP 项目中,文本预处理占据了超过半数的时间,其重要性不言而喻。


当然
也可以利用完备且效率可观的工具可以快速完成项目
For Example: 我一直在使用的由 哈工大社会计算与信息检索研究中心开发的 (LTP,Language Technology Platform )语言技术平台

文本预处理

文本是一类序列数据,一篇文章可以看作是字符或单词的序列,本节将介绍文本数据的常见预处理步骤,预处理通常包括四个步骤:

【较为简单的步骤,如果需要专门做NLP相关的时候,要进行特殊的预处理】

  1. 读入文本
  2. 分词
  3. 建立字典,将每个词映射到一个唯一的索引(index)
  4. 将文本从词的序列转换为索引的序列,方便输入模型

中英文文本预处理的特点

  • 中文文本是没有像英文的单词空格那样隔开的,因此不能直接像英文一样可以直接用最简单的空格和标点符号完成分词。所以一般我们需要用分词算法来完成分词。

  • 英文文本的预处理特殊在拼写问题,很多时候,对英文预处理要包括拼写检查,比如“Helo World”这样的错误,我们不能在分析的时候再去纠错。还有就是词干提取(stemming)和词形还原(lemmatization),主要是因为英文中一个词会存在不同的形式,这个步骤有点像孙悟空的火眼金睛,直接得到单词的原始形态。For Example:" faster “、” fastest " -> " fast ";“ leafs ”、“ leaves ” -> " leaf " 。

本文进行简要的介绍和实现
详细可参考:https://www.analyticsvidhya.com/blog/2017/06/word-embeddings-count-word2veec/

读入文本

引入数据源:http://www.gutenberg.org/ebooks/35【小说 Time Machine】

import collections
import re

def read_time_machine():
    with open('path to timemachine.txt', 'r') as f: #每次处理一行
        lines = [re.sub('[^a-z]+', ' ', line.strip().lower()) for line in f]  #正则表达式
    return lines


lines = read_time_machine()
print('# sentences %d' % len(lines))

分词

对每个句子进行分词,也就是将一个句子划分成若干个词(token),转换为一个词的序列。

def tokenize(sentences, token='word'):
    """Split sentences into word or char tokens"""
    if token == 'word':
        return [sentence.split(' ') for sentence in sentences]
    elif token == 'char':
        return [list(sentence) for sentence in sentences]
    else:
        print('ERROR: unkown token type '+token)

tokens = tokenize(lines)
tokens[0:2]
#分词结果:
[['the', 'time', 'machine', 'by', 'h', 'g', 'wells', ''], ['']]

建立字典

为了方便模型处理,将字符串转换为数字。因此先构建一个字典(vocabulary),将每个词映射到一个唯一的索引编号。

class Vocab(object):
    # 构建Vocab类时注意:句子长度统计与构建字典无关 | 所以不必要进行句子长度统计
    def __init__(self, tokens, min_freq=0, use_special_tokens=False):
        counter = count_corpus(tokens)  #<key,value>:<词,词频> 
        self.token_freqs = list(counter.items()) #去重并统计词频
        self.idx_to_token = []
        if use_special_tokens:
            # padding, begin of sentence, end of sentence, unknown
            self.pad, self.bos, self.eos, self.unk = (0, 1, 2, 3)
            self.idx_to_token += ['<pad>', '<bos>', '<eos>', '<unk>']
        else:
            self.unk = 0
            self.idx_to_token += ['<unk>']
        self.idx_to_token += [token for token, freq in self.token_freqs
                        if freq >= min_freq and token not in self.idx_to_token]
        self.token_to_idx = dict()
        for idx, token in enumerate(self.idx_to_token):
            self.token_to_idx[token] = idx

    def __len__(self): # 返回字典的大小
        return len(self.idx_to_token)

    def __getitem__(self, tokens):  # 词到索引的映射
        if not isinstance(tokens, (list, tuple)):
            return self.token_to_idx.get(tokens, self.unk)
        return [self.__getitem__(token) for token in tokens]

    def to_tokens(self, indices):  #索引到词的映射
        if not isinstance(indices, (list, tuple)):
            return self.idx_to_token[indices]
        return [self.idx_to_token[index] for index in indices]

def count_corpus(sentences):
    tokens = [tk for st in sentences for tk in st]
    return collections.Counter(tokens)  # 返回一个字典,记录每个词的出现次数

< unk > 较为特殊,表示为登录词,无论 use_special_token 参数是否为真,都会用到

将词转为索引

使用字典,我们可以将原文本中的句子从单词序列转换为索引序列

for i in range(8, 10):
    print('words:', tokens[i])
    print('indices:', vocab[tokens[i]])

到此,按照常见步骤已经介绍完了

我们对于分词这一重要关卡需要考虑的更多,上边实现的简要分词许多情形还未完善,只能实现部分特定的分词

  1. 标点符号通常可以提供语义信息,但是我们的方法直接将其丢弃了
  2. 类似“shouldn’t", "doesn’t"这样的词会被错误地处理
  3. 类似"Mr.", "Dr."这样的词会被错误地处理

一者,我们可以通过引入更复杂的规则来解决这些问题,但是事实上,有一些现有的工具可以很好地进行分词,在这里简单介绍其中的两个:spaCyNLTK

For Example :

text = “Mr. Chen doesn’t agree with my suggestion.”

spaCy

import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp(text)
print([token.text for token in doc])
Result:
['Mr.', 'Chen', 'does', "n't", 'agree', 'with', 'my', 'suggestion', '.']

NLTK

from nltk.tokenize import word_tokenize
from nltk import data
data.path.append('/home/kesci/input/nltk_data3784/nltk_data')
print(word_tokenize(text))
Result:
['Mr.', 'Chen', 'does', "n't", 'agree', 'with', 'my', 'suggestion', '.']
让对手感动,让对手恐惧
原文地址:https://www.cnblogs.com/RokoBasilisk/p/12305147.html