如何使用 nltk 或 python 删除停用词

2025-02-13 08:35:00
admin
原创
46
摘要:问题描述:我有一个数据集,我想从中删除停用词。我使用 NLTK 获取了停用词列表:from nltk.corpus import stopwords stopwords.words('english') 我究竟如何将数据与停用词列表进行比较,从而从数据中删除停用词?解决方案 1:from nltk.corp...

问题描述:

我有一个数据集,我想从中删除停用词。

我使用 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 2Python 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'])
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用