如何解决使用 map 预处理删除 doc2vec 的停用词列表而不会丢失词序
我正在用 doc2vec
实现一个简单的 gensim
,不是一个 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'],[' not work correctly'],['dont forget gensim doc2vec takes list_of_list']]
-
我的错误是什么以及如何解决?
-
还有其他有效的方法可以在不丢失 顺序正确吗?
我在提问之前检查过的问题列表:
How to apply a function to each sublist of a list in python?
- 我研究了这个并尝试应用到我的具体案例中
Removing stopwords from list of lists
- 顺序很重要我不能用set
Removing stopwords from a list of text files
- 这可能是一个可能的解决方案,类似于我实施的方案。
- 我不明白这种差异,但我不知道如何处理。 在我的情况下,文档没有被标记(并且不应该被标记,因为是 doc2vec 而不是 word2vec)
How to remove stop words using nltk or 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
提供有趣的结果。 Tha 算法,如 Word2Vec
,只有在大数据集上训练时才开始显示它的价值,其中 (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
几乎总是一个坏主意。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。