NLP-文本预处理(1)

前言

繁简转换、字符串分割、去除连续重复标点符号、查看字符串长度分布

一、繁简转换

from opencc import OpenCC
cc = OpenCC('t2s') # t2s 繁转简  s2t 简转繁
print(cc.convert('中國'))

二、字符串分割

import re
# 只保留中文、大小写字母和阿拉伯数字
reg = "[^0-9A-Za-z\u4e00-\u9fa5]"
text = '!@#我BU知道zhe是什么。。111..?'
print(re.sub(reg, '', '!@#我BU知道zhe是什么。。111..?'))
# output => 我BU知道zhe是什么111

text2 = '今天。。你吃饭!!了吗???吃的...什么;啊'
print(re.split('[!?!?。;…]|(\.{3})',text2))
# output => ['今天', None, '', None, '你吃饭', None, '', None, '了吗', None, '', None, '', None, '吃的', '...', '什么', None, '啊']
l = [i for i in re.split('[!?!?。;…]|(\.{3})',text2) if i != '' and i != None].
print(l)
# output => ['今天', '你吃饭', '了吗', '吃的', '...', '什么', '啊']

三、去除连续重复字符及标点符号

from itertools import groupby
text = '好淡啊啊啊啊aaa,好像兑了水!!!!~~~'
s = ''
for i,j in groupby(text):
    s += i
print(s) # output => 好淡啊a,好像兑了水!~

四、查看字符串长度分布

import pandas as pd
df = pd.read_csv(file_path)
df['text_len'] = df['comment'].map(lambda x:len(str(x)))
print(df['text_len'].describe(percentiles=[0.5,0.8,0.9]))
import matplotlib.pyplot as plt
plt.hist(df['text_len'],bins=30,rwidth=0.9,density=True)
plt.show()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值