繁体   English   中英

如何强制sklearn CountVectorizer不删除特殊字符(即#,@ 、、 $或%)

[英]How to force sklearn CountVectorizer to not remove special characters (i.e. #, @, , $ or %)

这是我的代码:

count = CountVectorizer(lowercase = False)

vocabulary = count.fit_transform([words])
print(count.get_feature_names())

例如,如果:

 words = "Hello @friend, this is a good day. #good."

我希望将其分为以下部分:

['Hello', '@friend', 'this', 'is', 'a', 'good', 'day', '#good']

当前,这是分为以下几类:

['Hello', 'friend', 'this', 'is', 'a', 'good', 'day']

您可以使用token_pattern从这里参数CountVectorizer作为中提到的文档

传递一个正则表达式告诉CountVectorizer应该把什么当作单词。 假设在这种情况下,我们告诉CountVectorizer,即使带有#@的单词也应该是一个单词。 然后做:

count = CountVectorizer(lowercase = False, token_pattern = '[a-zA-Z0-9$&+,:;=?@#|<>.^*()%!-]+')

输出:

['#good', '@friend', 'Hello', 'a', 'day', 'good', 'is', 'this']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM