如何提前判断CountVectorizer是否会抛出ValueError:空词汇?

时间:2019-01-05 17:15:16

标签: python python-3.x scikit-learn nlp

是否可以提前知道CountVectorizer是否会抛出

  

ValueError:空词汇?

基本上,我有大量文档,我想过滤掉不会通过CountVectorizer的文档(我正在使用stop_words='english'

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用build_analyzer()识别那些文档。试试吧!

from sklearn.feature_extraction.text import CountVectorizer
corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',
    'this is to',
    'she has'
]
analyzer = CountVectorizer(stop_words='english').build_analyzer()
filter_condtn = [True if analyzer(doc) else False for doc in corpus ]

#[True, True, False, True, False, False]

P.S。 :太糊涂了,看不到第三个文档中的所有单词都是停用词。

相关问题