您只需定义一个函数,该函数将字符串作为输入并重新调整要预处理的内容。因此,例如,大写字符串的简单函数如下所示:
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 生成步骤。