【发布时间】:2020-10-13 18:42:27
【问题描述】:
我目前正在一个项目中测试和训练数据以进行情绪分析。因为,我遇到了一个与 re.sub() 相关的问题,我无法弄清楚如何解决这个问题。我的代码如下:
import re def preprocessor(text):
text = re.sub(r"<[^>]*>", "", text) # removes all the html markup
emoticons = re.findall('(?::|;|= )(?:-)?(?:\)|\(|D|P)', text)
# removed all the non word charecter and convert them into lower case
text = (re.sub(r'[\W]+', '', text.lower()) + ''.join(emoticons).replace('-', ''))
return text
如您所见,该功能运行良好,没有引发异常。但是,由于我想打印文本以查看它是否产生我想要的结果,我得到以下输出:
preprocessor(df.loc[0, 'review'][-50:])`
'isseventitlebrazilnotavailable'
而我想要的输出应该是:
'is seven title brazil not available'
我猜我的 re.sub() 正在删除所有空格,但我不知道如何解决这个问题。
一个答案将是值得赞赏的。
注:
我想按如下方式清除字符串:例如:
从
'七岁。
标题(巴西):不可用'
到
'is seven title brazil not available'
谢谢
【问题讨论】:
标签: python scikit-learn