CountVectorizer令牌生成器

时间:2020-02-27 11:43:47

标签: python scikit-learn countvectorizer

我有一个带有句子的数据框,我使用countvectorizer和预定义的词汇表。 对于某些词汇,即使句子中包含词典中的单词,返回也为0。 出于某种原因不起作用的单词列表为:

* 1 time
* 1 report
* 7 increase
* not a good fit
* not a great fit
* c level
* not a need

CountVectorizer的定义如下:

CountVectorizer(vocabulary=cols,ngram_range=(1,5))

cols是字典

我很确定这与令牌生成器定义有关,但不确定如何将其更改为我需要的内容 任何帮助,将不胜感激 谢谢!

1 个答案:

答案 0 :(得分:1)

在另一个帖子上找到了解决方案。 不出所料,CountVectorizer中的默认标记化删除了所有特殊字符,标点符号和单个字符,这是我的问题。 我要做的就是更改令牌模式,如下所示:

vectorizer = CountVectorizer(vocabulary=cols,ngram_range=(1,5),token_pattern = r"(?u)\b\w+\b")

您可以在此处查看完整的说明: full explanation