Sklearn tf-idf TfidfVectorizer 无法捕获一个字母的单词

Sklearn tf-idf TfidfVectorizer failed to capture one letter words

一个特殊的例子是"Queens Stop 'N' Swap"。改造后只得到三个特征['Queens', 'Stop', 'SWap']'N' 已被忽略。如何捕获 'N'?。所有参数都是我代码中的默认设置。

### Create the vectorizer method
tfidf_vec = TfidfVectorizer()

### Transform the text into tf-iwine vectors
text_tfidf = tfidf_vec.fit_transform(title_text)

您没有将 'n' 作为标记,因为默认标记生成器不将其视为标记:

from sklearn.feature_extraction.text import TfidfVectorizer

texts = ["Queens Stop 'N' Swap",]
tfidf = TfidfVectorizer(token_pattern='(?u)\b\w\w+\b',)
tfidf.fit(texts)
tfidf.vocabulary_
{'queens': 0, 'stop': 1, 'swap': 2}

要捕获 1 个字母标记,保留大写,将其更改为:

tfidf = TfidfVectorizer(token_pattern='(?u)\b\w+\b',lowercase=False)
tfidf.fit(texts)
tfidf.vocabulary_
{'Queens': 1, 'stop': 2, 'N': 0, 'swap': 3}