NLP:最全去掉文本中的中英文标点符号大法

在处理文本时,中英文标点不同带来很大麻烦,我是先处理中文,在处理英文标点,最后还要去掉前边留下的空格。注意:两个库分别是中英文的标点符号是分开的,要分开处理,不能同时处理。

去掉英文符号

from string import punctuation
def preprocess_English(content):
    train_data = []
    for word in content:
        word = re.sub(r'[{}]+'.format(punctuation),' ',word)
        train_data.append(word)
    return train_data
 

去掉中文符号

import re
from zhon.hanzi import punctuation
def preprocess_Chinese(content):
    train = []
    for line in content:
        line = re.sub(r'[{}]+'.format(punctuation),' ',line)
        train.append(line)
    return train
 

处理前边多出的空格

commodity_1 =[]
for word in result:
word = " ".join(word)
commodity_1.append(word)
result= commodity_1

还有更为简单的办法是使用正则:

re.sub(r'[^\w\s]', '', x)

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值