如何删除少于 3 个字符的单词?

How can I remove words with less than 3 characters?

我在文本数据上使用 tf-idf,但无法删除少于 3 个字符的单词。我正在使用stop-words来忽略一些单词,但是如何指定长度来限制单词少于3个字符?

stopwords = ENGLISH_STOP_WORDS.union(['docx','45','ect', 'hou', 'com', 'recipient' , '030311' , '2011' , 'hrc' , 'qaddafi'])

vectsubject = TfidfVectorizer(analyzer='word', stop_words=stopwords, max_df=0.50, min_df=2)
X_SUBJECT = vectsubject.fit_transform(datasetemail.MetadataSubject)
features_subject = vectsubject.get_feature_names()

# Let's print the top 5 terms in body
dfbodyfeatures = gettop5(features_subject)
print(dfbodyfeatures)

我的结果是特征少于 3 个字符。

0      aiding
1       syria
2      latest
3         sid
4    exchange

我想删除像 "sid" 这样的词并在我的结果中包含下一个特征,因此输出可能包含 "helping" 特征,这是下一个相关特征

0      aiding
1       syria
2      latest
3      exchange
4      helping

基本上,我想删除 features_subject.

中少于 3 个字符的特征

试试这个

words = ['aiding', 'syria', 'latest', 'sid', 'exchange']
result_words = [x for x in words if len(x) > 3]

# Sample output
['aiding', 'syria', 'latest', 'exchange']

下面的列表理解应该可以解决问题:

features_subject = [f for f in vectsubject.get_feature_names() if len(f) > 3]

现在输出应该排除任何长度小于 3 的单词:

dfbodyfeatures = gettop5(features_subject)
print(dfbodyfeatures)

0      aiding
1       syria
2      latest
3      exchange
4      helping