gpt4 book ai didi

python - 在没有 NLTK 的情况下删除文本文件中的停用词

转载 作者:行者123 更新时间:2023-11-28 19:01:47 25 4
gpt4 key购买 nike

我有 2 个文件:stopwords.txta.txt
我想从文件 a.txt 中的文件 stopwords.txt 中删除停用词,并用空格分隔。

我该怎么做?这是我尝试做的:

def remove_stopwords(review_words):
with open('stopwords.txt') as stopfile:
stopwords = stopfile.read()
list = stopwords.split()
print(list)
with open('a.txt') as workfile:
read_data = workfile.read()
data = read_data.split()
print(data)
for word1 in list:
for word2 in data:
if word1 == word2:
return data.remove(list)
print(remove_Stopwords)

提前致谢

最佳答案

这是一个例子:

k = []
z = []
with open('stopWords.txt', 'r') as f:
for word in f:
word = word.split('\n')
k.append(word[0])

with open('a.txt', 'r') as f_obj:
for u in f_obj:
u = u.split('\n')
z.append(u[0])

p = [t for t in z if t not in k]
print(p)

遍历停用词文件中的每个单词并将其附加到列表中,然后遍历另一个文件中的每个单词。执行列表理解并删除停用词列表中出现的每个词。

关于python - 在没有 NLTK 的情况下删除文本文件中的停用词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51873067/

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