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)
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
