移除停用词篇

停用词

  • 把数据转换成计算机能理解的过程就是预处理过程。其中,预处理的主要形式就是过滤掉无用的数据。

  • 在自然语言处理中,无用的数据就是停用词(stop words)

停用词有哪些?

具体来说,在英文中的停用词就如a/an/the/in etc

命令行查看停用词列表

import nltk
from nltk.corpus import stopwords
print(stopwords.words("english")
{‘ourselves’, ‘hers’, ‘between’, ‘yourself’, ‘but’, ‘again’, ‘there’, ‘about’, ‘once’, ‘during’, ‘out’, ‘very’, ‘having’,with, ‘they’, ‘own’, ‘an’, ‘be’, ‘some’,for, ‘do’, ‘its’, ‘yours’, ‘such’, ‘into’, ‘of’, ‘most’, ‘itself’, ‘other’, ‘off’,is, ‘s’, ‘am’,or, ‘who’,as,from, ‘him’, ‘each’, ‘the’, ‘themselves’, ‘until’, ‘below’, ‘are’, ‘we’, ‘these’, ‘your’, ‘his’, ‘through’, ‘don’, ‘nor’, ‘me’, ‘were’, ‘her’, ‘more’, ‘himself’, ‘this’, ‘down’, ‘should’, ‘our’, ‘their’,while, ‘above’, ‘both’, ‘up’, ‘to’, ‘ours’, ‘had’, ‘she’,all, ‘no’, ‘when’, ‘at’,any, ‘before’, ‘them’, ‘same’,and, ‘been’, ‘have’,in, ‘will’, ‘on’, ‘does’, ‘yourselves’, ‘then’, ‘that’, ‘because’, ‘what’, ‘over’, ‘why’, ‘so’, ‘can’, ‘did’,not, ‘now’, ‘under’, ‘he’, ‘you’, ‘herself’, ‘has’, ‘just’, ‘where’, ‘too’, ‘only’, ‘myself’, ‘which’, ‘those’, ‘i’, ‘after’, ‘few’, ‘whom’, ‘t’, ‘being’,if, ‘theirs’, ‘my’, ‘against’, ‘a’, ‘by’, ‘doing’, ‘it’, ‘how’, ‘further’, ‘was’, ‘here’, ‘than’} 

移除停用词

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

example_sent = """ This is a sample sentence,
                  showing off the stop words filtration."""
         
stop_words = set(stopwords.words('english'))
# Tokenizers divide strings into lists of substrings.把字符串划分成子字符串的列表形式
word_tokens = word_tokenize(example_sent ) 
# 1
filtered_sentence = [w for w in word_tokens if not w.lower() in stop_words]
# ['sample', 'sentence', ',', 'showing', 'stop', 'words', 'filtration', '.']

# 2=1 同等写法,1是列表的写法
filtered_sentence = []
for w in word_tokens:
    if w not in stop_words:
        filtered_sentence.append(w)
 
print(word_tokens)
print(filtered_sentence)

Output:

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值