gpt4 book ai didi

python - 如何从 python 中的字符串数组列表中删除停用词?

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:44 25 4
gpt4 key购买 nike

我想从存储在 data 变量中的名为 arrayList1 的数组列表中删除停用词。我尝试下面的方法,但它不起作用。请帮我检查下面的代码并改进代码。谢谢。

import Retrieve_ED_Notes
from nltk.corpus import stopwords

data = Retrieve_ED_Notes.arrayList1

stop_words = set(stopwords.words('english'))


def remove_stopwords(data):
data = [word for word in data if word not in stop_words]
return data

for i in range(0, len(remove_stopwords(data))):
print(remove_stopwords(data[i]))

arrayList1 的控制台输出:

1|I really love writing journals
2|The mat is very comfortable and I will buy it again likes
3|The mousepad is smooth
1|I really love writing journals
4|This pen is very special to me.
4|This pencil is very special to me.
5|Meaningful novels
4|It brights up my day like a lighter and makes me higher.
6|School foolscap
7|As soft as my heart.lovey

最佳答案

将单词转换为 lower 并检查停用词。

from nltk.corpus import stopwords
stopwords=set(stopwords.words('english'))

data =['I really love writing journals','The mat is very comfortable and I will buy it again likes','The mousepad is smooth']

def remove_stopwords(data):
output_array=[]
for sentence in data:
temp_list=[]
for word in sentence.split():
if word.lower() not in stopwords:
temp_list.append(word)
output_array.append(' '.join(temp_list))
return output_array





output=remove_stopwords(data)

print(output)
['really love writing journals','mat comfortable buy likes', 'mousepad smooth']

关于python - 如何从 python 中的字符串数组列表中删除停用词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52361543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com