CountVectorizer 分词器

CountVectorizer tokenizer

我有一个带有句子的数据框,我使用预定义的词汇表对它使用了 countvectorizer。 对于某些词汇表单词,即使句子中包含字典中的单词,return 也是 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 是字典

我很确定这与分词器定义有关,但不确定如何将其更改为我需要的 任何帮助,将不胜感激 谢谢!

刚刚在另一个 post 上找到了解决方案。 正如预期的那样,CountVectorizer 中的默认标记化删除了所有特殊字符、标点符号和 单个字符 ,这是我的问题。 我需要做的就是更改令牌模式如下:

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

你可以在这里看到完整的解释: