您只需定义一个函数,该函数接受字符串作为输入并返回要预处理的内容。例如,一个处理大写字符串的简单函数如下所示:
def preProcess(s):
return s.upper()
一旦你完成了你的功能,那么你只需将它传递到你的TfidfVectorizer
目的。例如:
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = [
'This is the first document.',
'This is the second second document.',
'And the third one.',
'Is this the first document?'
]
X = TfidfVectorizer(preprocessor=preProcess)
X.fit(corpus)
X.get_feature_names()
结果是:
[u'AND', u'DOCUMENT', u'FIRST', u'IS', u'ONE', u'SECOND', u'THE', u'THIRD', u'THIS']
这间接回答了您的后续问题,因为尽管将小写设置为 true,但大写的预处理函数会覆盖它。文档中也提到了这一点:
预处理器:可调用或无(默认)覆盖预处理
(字符串转换)阶段,同时保留标记化和
n-gram 生成步骤。