在不丢失单词顺序的情况下,使用map预处理doc2vec的列表删除停止词列表



我正在用gensim实现一个简单的doc2vec而不是一个word2vec

我需要在不丢失列表的正确顺序的情况下删除停止语。

每个列表都是一个文档,正如我对doc2vec的理解,该模型将有一个TaggedDocuments 列表作为输入

model = Doc2Vec(lst_tag_documents, vector_size=5, window=2, min_count=1, workers=4)

dataset = [['We should remove the stopwords from this example'],
['Otherwise the algo'],
["will not work correctly"],
['dont forget Gensim doc2vec takes list_of_list' ]]
STOPWORDS = ['we','i','will','the','this','from']

def word_filter(lst):
lower=[word.lower() for word in lst]
lst_ftred = [word for word in lower if not word in STOPWORDS]
return lst_ftred
lst_lst_filtered= list(map(word_filter,dataset))
print(lst_lst_filtered)

输出:

[['we should remove the stopwords from this example'], ['otherwise the algo'], ['will not work correctly'], ['dont forget gensim doc2vec takes list_of_list']]

预期输出:

[[' should remove the stopwords   example'], ['otherwise the algo'], [' not work correctly'], ['dont forget gensim doc2vec takes list_of_list']]

  • 我的错误是什么以及如何修复

  • 在不丢失秩序井然


我在提问前检查的问题列表:

如何将函数应用于python中列表的每个子列表?

  • 我研究了这个问题,并试图应用于我的具体案例

从列表列表中删除停止语

  • 顺序很重要我不能使用set

从文本文件列表中删除停止字

  • 这可能是一个可能的解决方案,与我所实现的类似
  • 我不希望有这样的区别,但我不知道该怎么处理。在我的情况下,文档没有标记化(也不应该标记化,因为它是doc2vec而不是word2vec(

如何使用nltk或python 删除停止字

  • 在这个问题中,SO处理的是一个列表,而不是列表

lower是一个元素的列表,word not in STOPWORDS将返回False。取带有索引的列表中的第一个项目,并按空格分割

lst_ftred = ' '.join([word for word in lower[0].split() if word not in STOPWORDS])
# output: ['should remove stopwords example', 'otherwise algo', 'not work correctly', 'dont forget gensim doc2vec takes list_of_list']
# 'the' is also in STOPWORDS

首先,请注意,从Doc2Vec训练中删除停止语并不重要。其次,请注意,这种微小的玩具数据集不会从Doc2Vec中提供有趣的结果。与Word2Vec一样,Tha算法只有在大型数据集上训练时才开始显示其价值,这些数据集具有(1(比向量维度的数量多得多的唯一单词;以及(2(每个单词的用法有很多不同的例子——至少有几个,最好是几十个或几百个。

尽管如此,如果你想去掉停止语,那么在标记原始字符串后做这件事是最容易的。(也就是说,将字符串拆分为单词列表。这就是Doc2Vec无论如何都需要的格式。(而且,您不希望dataset是一个每个字符串都有一个列表的列表。相反,您希望它是一个字符串列表(最初(,然后是一个每个都有许多标记的列表列表。

以下内容应该有效:

string_dataset = [
'We should remove the stopwords from this example',
'Otherwise the algo',
"will not work correctly",
'dont forget Gensim doc2vec takes list_of_list',
]
STOPWORDS = ['we','i','will','the','this','from']
# Python list comprehension to break into tokens
tokenized_dataset = [s.split() for s in string_dataset]
def filter_words(tokens):
"""lowercase each token, and keep only if not in STOPWORDS"""
return [token.lower() for token in tokens if token not in STOPWORDS]
filtered_dataset = [filter_words(s) for sent in tokenized_dataset]

最后,因为如上所述,Doc2Vec需要多个单词示例才能正常工作,所以使用min_count=1几乎总是一个坏主意。

最新更新