gpt4 book ai didi

python - 删除python中情感分析中的标点符号

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:37 26 4
gpt4 key购买 nike

我编写了以下代码。它工作得很好,但是当我添加带有逗号、句号等的句子时就会出现问题。我已经研究过并且可以将 strip() 视为修复它的潜在选项?我看不到在哪里添加它并尝试过,但只是一个接一个的错误!

谢谢

sent_analysis = {"beer": 10, "wine":13,"spirit": 11,"cider":16,"shot":16}

def sentiment_analysis(dic, text):
split_text = text.split()
result = 0.00
for i in split_text:
if i in dic:
result+= dic[i]
return result


print sentiment_analysis(sent_analysis,"the beer, wine and cider were great")
print sentiment_analysis(sent_analysis,"the beer and the wine were great")

最佳答案

正则表达式可用于从字符串中删除所有非字母数字字符。在下面的代码中,^\w\s 匹配任何非 a-z、A-Z、0-9 和空格(如 ^ 所示)的内容,并将其删除。 return 语句迭代分割字符串,查找任何匹配项,将其添加到列表中,然后返回这些数字的总和。

Regex \s

Regex \w

import re
sent_analysis = {"beer": 10, "wine":13,"spirit": 11,"cider":16,"shot":16}

def sentiment_analysis(dic, text):
result = 0.00
s = re.sub(r'[^\w\s]','',text)
return sum([dic[x] for x in s.split() if x in dic])

print(sentiment_analysis(sent_analysis,"the beer,% wine &*and cider @were great"))

输出:39

这将说明大多数标点符号,如示例字符串中添加的许多不同标点符号所示。

关于python - 删除python中情感分析中的标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36665080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com