PYTHON:如何将带有关键字参数的分词器传递给 scikit 的 CountVectorizer?

标签 python scikit-learn tokenize feature-extraction keyword-argument

我有一个带有一些关键字参数的自定义分词器函数:

def tokenizer(text, stem=True, lemmatize=False, char_lower_limit=2, char_upper_limit=30):
    do things...
    return tokens

现在,我怎样才能将这个分词器及其所有参数传递给 CountVectorizer?我尝试过的都没有用;这也不起作用:

from sklearn.feature_extraction.text import CountVectorizer
args = {"stem": False, "lemmatize": True}
count_vect = CountVectorizer(tokenizer=tokenizer(**args), stop_words='english', strip_accents='ascii', min_df=0, max_df=1., vocabulary=None)

非常感谢任何帮助。提前致谢。

最佳答案

tokenizer 应该是可调用的或 None。

(tokenizer=tokenize(**args) 是错字吗?您上面的函数名称是 tokenizer。)

你可以试试这个:

count_vect = CountVectorizer(tokenizer=lambda text: tokenizer(text, **args), stop_words='english', strip_accents='ascii', min_df=0, max_df=1., vocabulary=None)

关于PYTHON:如何将带有关键字参数的分词器传递给 scikit 的 CountVectorizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31843996/

相关文章:

python - 使用 virtualenv 导入错误

python-3.x - 使用自定义目标/损失函数的随机森林回归器(Python/Sklearn)

c++ - 根据某些特定规则对文本进行标记。 C++中的算法

python - 在线版scikit-learn的TfidfVectorizer

python - 与 TfidfVectorizer.fit_transform 的返回结果混淆

java - 使用 Edge N Gram 分析器和字符过滤器创建分析器,用新行替换空格

elasticsearch - 如何在ElasticSearch中标记罗马数字术语?

python - 在 pandas 中,如何识别具有共同值的记录并替换其中一个的值以匹配另一个?

python - GIL 的存在会给多线程带来什么优势?

python - Pandas 与 pickle 0.14.1 和 0.15.2 的向后兼容性问题