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