微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

仅过滤单词而不是标点符号

如何解决仅过滤单词而不是标点符号

我试图从本质上浏览正面 ('pos') 和负面 ('neg') 列表。这里的问题是下面的脚本也包括空格和标点符号,我不想要那样。我只想从这些列表中提取最常见的 30 个单词。知道如何做到这一点吗?

#for counting frequently occurrence of negative and positive words.

from collections import Counter

count1 = Counter(" ".join(data[data['sentiment']=='pos']["text"]).split()).most_common(30)
data1 = pd.DataFrame.from_dict(count1)
data1 = data1.rename(columns={0: "words of positive",1 : "count"})
count2 = Counter(" ".join(data[data['sentiment']=='neg']["text"]).split()).most_common(30)
data2 = pd.DataFrame.from_dict(count2)
data2 = data2.rename(columns={0: "words of negative",1 : "count_"})

编辑

上面的实现有正确的想法,但是我希望提取具有意义的单词而不是逻辑连接词,例如 (the,and a it that) 以及标点符号

解决方法

您可以替换 .split()

data[data['sentiment']=='pos']["text"]).split()

带有 <command> 函数

import re

def return_words(string):
    return re.findall(r'\b\S+\b',string.lower())

return_words(data[data['sentiment']=='pos']["text"]))

所以把它放在你的代码中:

from collections import Counter
import re

def return_words(string):
    return re.findall(r'\b\S+\b',string.lower())

count1 = Counter(" ".join(return_words(data[data['sentiment']=='pos']["text"]))).most_common(30)
data1 = pd.DataFrame.from_dict(count1)
data1 = data1.rename(columns={0: "words of positive",1 : "count"})
count2 = Counter(" ".join(retrn_words(data[data['sentiment']=='neg']["text"]))).most_common(30)
data2 = pd.DataFrame.from_dict(count2)
data2 = data2.rename(columns={0: "words of negative",1 : "count_"})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。