数据清洗实例

在自然语言处理中,往往我们拿到一份数据,不能直接使用,需进行预操作,把数据转化成我们需要的样子。

下面介绍一下基本的数据清洗操作:

代码:

import re
from nltk.corpus import stopwords
s = '     RT @Amila #Test\nTom\'s newly listed Co & Mary\'s unlisted   Group to supply tech for nlTK.\nh $TSLA $AAPL http://t.co/x3wu2u32ush'
cache_english_stopwords = stopwords.words('english')

print("原始数据:",s,'\n')

#去除HTML标签
text_no_special_entities = re.sub(r'&\w*;|#\w*|@\w*','',s)
print("去除特殊标签后:",text_no_special_entities,'\n')

#去除价值符号

text_no_tickers = re.sub(r'\$\w*','',text_no_special_entities)
print("去除价值符号:",text_no_tickers,'\n')

#去除超链接
text_no_hyperlinks = re.sub(r'https?:\/\/.*\/\w*','',text_no_tickers)
print("去除超链接:",text_no_hyperlinks,'\n')

#去掉专门名词缩写
text_no_small_words = re.sub(r'\b\w{1,2}\b','',text_no_hyperlinks)
print("去掉名词缩写:",text_no_small_words,'\n')

#去掉多余的空格
text_no_whitespace = re.sub(r'\s\s+',' ',text_no_small_words)
text_no_whitespace = text_no_whitespace.strip(' ')
print("去掉多余空格:",text_no_whitespace,'\n')

tokens = word_tokenize(text_no_whitespace)
print("分词结果:",tokens,'\n')

#去停用词
list_no_stopwords = [i for i in tokens if i not in cache_english_stopwords]
print("去停用词:",list_no_stopwords,'\n')

text_filtered = ' '.join(list_no_stopwords)
print('过滤后:',text_filtered)

运行结果:

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不写代码的程序员~zs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值