python - 如何提前判断 CountVectorizer 是否会抛出 ValueError : empty vocabulary?

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

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

ValueError: empty vocabulary?

基本上,我有一个文档语料库,我想过滤掉那些无法通过 CountVectorizer 的文档(我正在使用 stop_words='english')

谢谢

最佳答案

您可以使用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]

附注:我太困惑了,看不到第三个文档中的所有单词都是停用词。

关于python - 如何提前判断 CountVectorizer 是否会抛出 ValueError : empty vocabulary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54054299/

相关文章:

python - 用单独的程序替换另一个文件中的文本

python - 如果我已经将模型作为 pickle,为什么还需要在 docker 容器中使用 sklearn?

python - Numpy 点操作未使用所有 cpu 内核

python - django queryset 返回 "classname object"而不是 DB 值

python-3.x - AttributeError: 模块 'sklearn.datasets' 没有属性 'load_titanic'

Python jsonschema,什么都不允许,或者只需要两个字段之一

python - PyDev Unresolved sklearn 导入问题

python - Sklearn 使用自然语言处理数值数据

python - 如何在Python中用不同的字符替换字符串中重复多次的字符?

python - 打印二叉树中所有可能的路径