基本文本处理技能

基本文本处理技能

文本处理基本流程

中英文文本都存在一致的基本处理流程, 主要包括: 分词(Segmentation), 清洗(Cleaning), 标准化(Normalization), 特征提取(Feature Extraction)和建模(Modeling).

中英文文本预处理特点

中英文文本虽然总体预处理流程一致, 但是存在一些本质的区别. 首先, 中文不像英文天然使用空格和符号完成了分词, 因此需要使用分词算法将一段文本进行切分. 另外英文也存在自身的一些特殊问题: 如拼写错误, 词形还原等. 词形还原是由于英文单词会随着不同的上下文出现各种不同的形式, 这些形式都是表示同一个词, 但是由于拼写改变被当做了不同的词.

文本预处理

完整代码github

读取文本

以上一篇博客使用的THUCNews的子集数据为例

ch_data_file = '../task1/cnews/cnews.train.txt'
with open(ch_data_file, 'r', encoding='utf-8') as f:
    ch_samples = [x.strip().split('\t') for x in f.readlines()]
    
print(len(ch_samples))
print(ch_samples[0])

去除数据中非文本部分

通过正则表达式的方式去除文本中非文本部分

import re

def filter_nontext(samples):
    # 过滤不了\\ \ 中文()还有
    r1 = u'[a-zA-Z0-9’!"#$%&\'()*+,-./:;<=>?@,。?★、…【】《》?“”‘’![\\]^_`{|}~]+'
    #用户也可以在此进行自定义过滤字符 # 者中规则也过滤不完全
    r2 = "[\s+\.\!\/_,$%^*(+\"\']+|[+——!,。?、~@#¥%……&*()]+"
    # \\\可以过滤掉反向单杠和双杠,/可以过滤掉正向单杠和双杠,第一个中括号里放的是英文符号,第二个中括号里放的是中文符号,第二个中括号前不能少|,否则过滤不完全
    r3 =  "[.!//_,$&%^*()<>+\"'?@#-|:~{}]+|[——!\\\\,。=?、:“”‘’《》【】¥……()]+" 
    # 去掉括号和括号内的所有内容
    r4 =  "\\【.*?】+|\\《.*?》+|\\#.*?#+|[.!/_,$&%^*()<>+""'?@|:~{}#]+|[——!\\\,。=?、:“”‘’¥……()《》【】]"

    clear_samples = []
    for sample in samples:
        sentence = sample[1]
        cleanr = re.compile('<.*?>')
        sentence = re.sub(cleanr, ' ', sentence) #去除html标签
        sentence = re.sub(r4,'',sentence)
        clear_samples.append([sample[0], sentence])
    return clear_samples

clear_samples = filter_nontext(ch_samples)
print(len(clear_samples))
print(clear_samples[0])
print(clear_samples[1])

分词

  • 英文分词一般可以直接使用split()操作
  • 中文分词需要使用专门的分词算法, 如jieba分词
import jieba 

def cut_samples(samples):
    new_samples = []
    for sample in samples:
        sentence = sample[1]
        sentence_seg = jieba.cut(sentence)
        new_samples.append([sample[1], list(sentence_seg)])
    return new_samples

seg_samples = cut_samples(clear_samples)
print(len(seg_samples))
print(seg_samples[0])
print(seg_samples[1])

去除停用词

中文停用词表可以参考中文停用词, 将对应停用词表下载并读取.

from nltk.corpus import stopwords 
#stop = set(stopwords.words('english')) 
with open('ch_stopwords.txt', 'r', encoding='utf-8') as f:
    stop = [x.strip() for x in f.readlines()]
    stop = set(stop)
print(stop)

def filter_stopwords(samples):
    new_samples = []
    for sample in samples:
        sentence = sample[1]
        filter_sentence= [w for w in sentence if w not in stop]
        new_samples.append((sample[1], filter_sentence))
    return new_samples

nostop_samples = filter_stopwords(seg_samples)
print(len(nostop_samples))
print(nostop_samples[0])
print(nostop_samples[1])

词频统计

from collections import Counter

def count(samples):
    cnt = Counter()
    for sample in samples:
        cnt += Counter(sample[1])
    return cnt

cnt = count(nostop_samples)
print(cnt.most_common(100))

语言模型

简单的说,语言模型(Language Model)是用来计算一个句子出现概率的模型, n-gram语言模型指由n个连续词组成的词组集合, n=1称为uni-gram, n=2称为bi-gram, n=3称为tri-gram. 以文本I love deep learning为例:

Uni-gram: {I}, {love}, {deep}, {learning}
Bi-gram : {I, love}, {love, deep}, {love, deep}, {deep, learning}
Tri-gram : {I, love, deep}, {love, deep, learning}

给定n-gram表示的文本序列 W = ( w 1 , w 2 , … , w n ) W=(w_1, w_2, \dots, w_n) W=(w1,w2,,wn), 其中 w i w_i wi表示n-gram表示的文本序列中地 i i i个词组, 语句 W W W出现的概率可以表示为:

p ( W ) = p ( w 1 , w 2 , … , w n ) = p ( w 1 ) ⋅ p ( w 2 ∣ w 1 ) ⋅ p ( w 3 ∣ w 1 , w 2 ) … p ( w n ∣ w 1 , w 2 , … , w n − 1 ) \begin{aligned} p(W)&amp;=p(w_1,w_2,\dots,wn) \\ &amp;=p(w_1) \cdot p(w_2|w_1) \cdot p(w_3|w_1,w_2) \dots p(w_n|w_1,w_2,\dots,w_{n-1}) \end{aligned} p(W)=p(w1,w2,,wn)=p(w1)p(w2w1)p(w3w1,w2)p(wnw1,w2,,wn1)

参考资料

文本预处理技术详解
语言模型 Language Madel 与 word2vec
结巴分词

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值