如何使用 nltk 或 python 删除停用词
- 2025-02-13 08:35:00
- admin 原创
- 46
问题描述:
我有一个数据集,我想从中删除停用词。
我使用 NLTK 获取了停用词列表:
from nltk.corpus import stopwords
stopwords.words('english')
我究竟如何将数据与停用词列表进行比较,从而从数据中删除停用词?
解决方案 1:
from nltk.corpus import stopwords
# ...
filtered_words = [word for word in word_list if word not in stopwords.words('english')]
解决方案 2:
您还可以执行集合差异,例如:
list(set(nltk.regexp_tokenize(sentence, pattern, gaps=True)) - set(nltk.corpus.stopwords.words('english')))
解决方案 3:
要排除所有类型的停用词(包括 nltk 停用词),您可以执行以下操作:
from stop_words import get_stop_words
from nltk.corpus import stopwords
stop_words = list(get_stop_words('en')) #About 900 stopwords
nltk_words = list(stopwords.words('english')) #About 150 stopwords
stop_words.extend(nltk_words)
output = [w for w in word_list if not w in stop_words]
解决方案 4:
我假设您有一个单词列表 (word_list),您想从中删除停用词。您可以这样做:
filtered_word_list = word_list[:] #make a copy of the word_list
for word in word_list: # iterate over word_list
if word in stopwords.words('english'):
filtered_word_list.remove(word) # remove word from filtered_word_list if it is a stopword
解决方案 5:
有一个非常简单的轻量级 Python 包stop-words
就是为此而开发的。
首先使用以下命令安装该包:pip install stop-words
然后,您可以使用列表推导式在一行中删除您的单词:
from stop_words import get_stop_words
filtered_words = [word for word in dataset if word not in get_stop_words('english')]
这个包非常轻量,可以下载(与 nltk 不同),适用于和Python 2
,Python 3
并且它包含许多其他语言的停用词,例如:
Arabic
Bulgarian
Catalan
Czech
Danish
Dutch
English
Finnish
French
German
Hungarian
Indonesian
Italian
Norwegian
Polish
Portuguese
Romanian
Russian
Spanish
Swedish
Turkish
Ukrainian
解决方案 6:
这是我对此的看法,以防您想立即将答案转换为字符串(而不是过滤词列表):
STOPWORDS = set(stopwords.words('english'))
text = ' '.join([word for word in text.split() if word not in STOPWORDS]) # delete stopwords from text
解决方案 7:
使用textcleaner库从数据中删除停用词。
请访问以下链接:https://yugantm.github.io/textcleaner/documentation.html#remove_stpwrds
请按照以下步骤使用该库进行操作。
pip install textcleaner
安装后:
import textcleaner as tc
data = tc.document(<file_name>)
#you can also pass list of sentences to the document class constructor.
data.remove_stpwrds() #inplace is set to False by default
使用上述代码删除停用词。
解决方案 8:
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'))
word_tokens = word_tokenize(example_sent)
filtered_sentence = [w for w in word_tokens if not w in stop_words]
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print(word_tokens)
print(filtered_sentence)
解决方案 9:
虽然这个问题有点老了,但这里有一个值得一提的新库,它可以执行额外的任务。
在某些情况下,您不仅想删除停用词。相反,您可能想在文本数据中找到停用词并将其存储在列表中,以便找到数据中的噪音并使其更具交互性。
该库名为'textfeatures'
。您可以按如下方式使用它:
! pip install textfeatures
import textfeatures as tf
import pandas as pd
例如,假设您有以下一组字符串:
texts = [
"blue car and blue window",
"black crow in the window",
"i see my reflection in the window"]
df = pd.DataFrame(texts) # Convert to a dataframe
df.columns = ['text'] # give a name to the column
df
现在,调用stopwords()函数并传递您想要的参数:
tf.stopwords(df,"text","stopwords") # extract stop words
df[["text","stopwords"]].head() # give names to columns
其结果将是:
text stopwords
0 blue car and blue window [and]
1 black crow in the window [in, the]
2 i see my reflection in the window [i, my, in, the]
如您所见,最后一列包含该文档(记录)中包含的停用词。
解决方案 10:
你可以使用这个功能,你应该注意到你需要降低所有的单词
from nltk.corpus import stopwords
def remove_stopwords(word_list):
processed_word_list = []
for word in word_list:
word = word.lower() # in case they arenet all lower cased
if word not in stopwords.words("english"):
processed_word_list.append(word)
return processed_word_list
解决方案 11:
使用过滤器:
from nltk.corpus import stopwords
# ...
filtered_words = list(filter(lambda word: word not in stopwords.words('english'), word_list))
解决方案 12:
我将向您展示一些示例首先,我从数据框()中提取文本数据,twitter_df
然后进行如下进一步处理
from nltk.tokenize import word_tokenize
tweetText = twitter_df['text']
然后为了标记化我使用以下方法
from nltk.tokenize import word_tokenize
tweetText = tweetText.apply(word_tokenize)
然后,为了删除停用词,
from nltk.corpus import stopwords
nltk.download('stopwords')
stop_words = set(stopwords.words('english'))
tweetText = tweetText.apply(lambda x:[word for word in x if word not in stop_words])
tweetText.head()
我认为这会对你有帮助
解决方案 13:
尝试一下:
sentence = " ".join([word for word in sentence.split() if word not in stopwords.words('english')]
解决方案 14:
如果您的数据存储为Pandas DataFrame
,您可以使用默认remove_stopwords
使用 NLTK 停用词列表的 textero 。
import pandas as pd
import texthero as hero
df['text_without_stopwords'] = hero.remove_stopwords(df['text'])