Python 如何从gensim中的文档中删除停止字?
我正在使用Python中的Doc2Vec技术,使用其Python 如何从gensim中的文档中删除停止字?,python,nlp,gensim,word2vec,doc2vec,Python,Nlp,Gensim,Word2vec,Doc2vec,我正在使用Python中的Doc2Vec技术,使用其gensim包构建一个NLP聊天应用程序。我已经完成了标记化和词干化。我想从训练集中以及用户抛出的问题中删除停止词(以测试它是否工作得更好) 这是我的密码 import gensim import nltk from gensim import models from gensim import utils from gensim import corpora from nltk.stem import PorterStemmer ps = P
gensim
包构建一个NLP聊天应用程序。我已经完成了标记化和词干化。我想从训练集中以及用户抛出的问题中删除停止词(以测试它是否工作得更好)
这是我的密码
import gensim
import nltk
from gensim import models
from gensim import utils
from gensim import corpora
from nltk.stem import PorterStemmer
ps = PorterStemmer()
sentence0 = models.doc2vec.LabeledSentence(words=[u'sampl',u'what',u'is'],tags=["SENT_0"])
sentence1 = models.doc2vec.LabeledSentence(words=[u'sampl',u'tell',u'me',u'about'],tags=["SENT_1"])
sentence2 = models.doc2vec.LabeledSentence(words=[u'elig',u'what',u'is',u'my'],tags=["SENT_2"])
sentence3 = models.doc2vec.LabeledSentence(words=[u'limit', u'what',u'is',u'my'],tags=["SENT_3"])
sentence4 = models.doc2vec.LabeledSentence(words=[u'claim',u'how',u'much',u'can',u'I'],tags=["SENT_4"])
sentence5 = models.doc2vec.LabeledSentence(words=[u'retir',u'i',u'am',u'how',u'much',u'can',u'elig',u'claim'],tags=["SENT_5"])
sentence6 = models.doc2vec.LabeledSentence(words=[u'resign',u'i',u'have',u'how',u'much',u'can',u'i',u'claim',u'elig'],tags=["SENT_6"])
sentence7 = models.doc2vec.LabeledSentence(words=[u'promot',u'what',u'is',u'my',u'elig',u'post',u'my'],tags=["SENT_7"])
sentence8 = models.doc2vec.LabeledSentence(words=[u'claim',u'can,',u'i',u'for'],tags=["SENT_8"])
sentence9 = models.doc2vec.LabeledSentence(words=[u'product',u'coverag',u'cover',u'what',u'all',u'are'],tags=["SENT_9"])
sentence10 = models.doc2vec.LabeledSentence(words=[u'hotel',u'coverag',u'cover',u'what',u'all',u'are'],tags=["SENT_10"])
sentence11 = models.doc2vec.LabeledSentence(words=[u'onlin',u'product',u'can',u'i',u'for',u'bought',u'through',u'claim',u'sampl'],tags=["SENT_11"])
sentence12 = models.doc2vec.LabeledSentence(words=[u'reimburs',u'guidelin',u'where',u'do',u'i',u'apply',u'form',u'sampl'],tags=["SENT_12"])
sentence13 = models.doc2vec.LabeledSentence(words=[u'reimburs',u'procedur',u'rule',u'and',u'regul',u'what',u'is',u'the',u'for'],tags=["SENT_13"])
sentence14 = models.doc2vec.LabeledSentence(words=[u'can',u'i',u'submit',u'expenditur',u'on',u'behalf',u'of',u'my',u'friend',u'and',u'famili',u'claim',u'and',u'reimburs'],tags=["SENT_14"])
sentence15 = models.doc2vec.LabeledSentence(words=[u'invoic',u'bills',u'procedur',u'can',u'i',u'submit',u'from',u'shopper stop',u'claim'],tags=["SENT_15"])
sentence16 = models.doc2vec.LabeledSentence(words=[u'invoic',u'bills',u'can',u'i',u'submit',u'from',u'pantaloon',u'claim'],tags=["SENT_16"])
sentence17 = models.doc2vec.LabeledSentence(words=[u'invoic',u'procedur',u'can',u'i',u'submit',u'invoic',u'from',u'spencer',u'claim'],tags=["SENT_17"])
# User asks a question.
document = input("Ask a question:")
tokenized_document = list(gensim.utils.tokenize(document, lowercase = True, deacc = True))
#print(type(tokenized_document))
stemmed_document = []
for w in tokenized_document:
stemmed_document.append(ps.stem(w))
sentence19 = models.doc2vec.LabeledSentence(words= stemmed_document, tags=["SENT_19"])
# Building vocab.
sentences = [sentence0,sentence1,sentence2,sentence3, sentence4, sentence5,sentence6, sentence7, sentence8, sentence9, sentence10, sentence11, sentence12, sentence13, sentence14, sentence15, sentence16, sentence17, sentence19]
#I tried to remove the stop words but it didn't work out as LabeledSentence object has no attribute lower.
stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in sentences]
..
有没有一种方法可以直接从
句子中删除停止词
并获得一组没有停止词的新词汇?您的句子
对象已经是一个标签项
对象列表。你在上面构建这些;它们包括单词
中的字符串列表和标记
中的字符串列表
因此列表中的每个项目(列表理解中的document
)都不能应用.lower()
这样的字符串方法。(它也不需要是.split()
,因为它的单词
已经是独立的标记。)
最干净的方法是在停止词用于构建LabeledSentence
对象之前,将其从单词列表中删除。例如,您可以创建一个函数,而不使用顶部定义的\u stopwords()
。然后,您创建LabeledEntence
对象的行可以如下所示:
sentence0 = LabeledSentence(words=remove_stopwords([u'sampl', u'what', u'is']),
tags=["SENT_0"])
或者,您可以对现有的LabeledSentence
对象进行变异,使其每个单词
属性现在都缺少停止词。这将用以下内容替换您的最后一行:
for doc in sentences:
doc.words = [word for word in doc.words if word not in stoplist]
texts = sentences
另外,你没有问但应该知道的事情:
现在是Doc2Vec文本对象的首选示例类名–但事实上,任何具有两个必需属性TaggedDocument
和words
的对象都可以正常工作tags
- Doc2Vec并没有在微小的玩具大小的数据集上显示许多所需的属性——如果一个建立在几十个句子上的模型没有做任何有用的事情,或者误导了什么样的预处理/元参数选项是最好的,请不要感到惊讶。(数以万计的文本,以及至少几十个单词长的文本,对于有意义的结果来说要好得多。)
- Word2Vec/Doc2Vec的很多工作都不涉及词干或停止单词删除,但有时可能会有所帮助
wmdistance()
的形式提供,但在大型数据集上成对计算的速度非常慢。