我正在对文本数据使用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“这样的单词,并在我的结果中包含下一个功能,因此输出可以包括”帮助“功能,这是下一个相关功能。
0 aiding
1 syria
2 latest
3 exchange
4 helping基本上,我希望删除我的features_subject中少于3个字符的特性。
发布于 2019-05-25 13:44:32
下面的列表理解应该能做到这一点:
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发布于 2019-05-25 07:34:43
尝尝这个
words = ['aiding', 'syria', 'latest', 'sid', 'exchange']
result_words = [x for x in words if len(x) > 3]
# Sample output
['aiding', 'syria', 'latest', 'exchange']https://stackoverflow.com/questions/56301489
复制相似问题