NLP-中文文本去除标点符号

本文介绍了中文文本预处理中去除标点符号的方法,包括使用Zhon库和自定义符号集两种方式,并对比了它们的效果。
  • 简单记录一下中文文本如何去除标点和特殊符号的问题。。。

目录


一、回顾一下英文如何去除符号等预处理问题

①去除特殊符号
def isSymbol(inputString):
    return bool(re.match(r'[^\w]', inputString))
②去除数字
def hasNumbers(inputString):
    return bool(re.search(r'\d', inputString))
③词形归一
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
④停止词
from nltk.corpus import stopwords
stop = stopwords.words('english')
⑤将上述内容综合
def check(word):
    """
    如果需要这个单词,则True
    如果应该去除,则False
    """
    word= word.lower()
    if word in stop:
        return False
    elif hasNumbers(word) or isSymbol(word):
        return False
    else:
        return True

# 把上面的方法综合起来
def preprocessing(sen):
    res = []
    for word in sen:
        if check(word):#如果word为True的话则进行词形归一
            res.append(wordnet_lemmatizer.lemmatize(word))
    return res

接下来直接调用preprocessing()完成数据的预处理:

X_train = [preprocessing(x) for x in X_train]
X_test = [preprocessing(x) for x in X_test]

二、中文文本去除标点符号

1.背景知识

  • (1)使用 zhon.hanzi.punctuation函数实现。
  • (2)其中zhon是一个python库,它提供了在中文文本处理中常用的几个功能函数,一共包含四个模块: zhon.hanzizhon.pinyinzhon.zhuyinzhon.cedict
  • (3)其中实现去除标点的功能在 zhon.hanzi模块中,我们使用 from zhon.hanzi import punctuation进行调用。
  • (4)其中 zhon.hanzi.punctuation函数是 zhon.hanzi.non_stopszhon.hanzi.stop两个函数的结合。

zhon.hanzi.non_stops
它包含中文标点符号,不包括用作停止的标点符号。"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏

zhon.hanzi.stops
包含起着停止作用的符号: !?。。

了解更多Zhon内容请点击链接:Zhon

2.示例说明1:使用Zhon库中的符号集

①命令行中安装Zhon库

pip install zhon

②代码
import re
from zhon.hanzi import punctuation
from zhon.hanzi import non_stops
from zhon.hanzi import stops
corpus='花!呗/期?免,息.---蚂!蚁/花呗?期免stops息,什么。意思??'
print(corpus)
string1 = re.sub(r"[%s]+" %punctuation, "",corpus)#去除, ! ? 。
string2 = re.sub(r"[%s]+" %non_stops, "",corpus)#去除,
string3 = re.sub(r"[%s]+" %stops, "",corpus)#去除! ? 。
print(string1)
print(string2)
print(string3)
③结果

这里写图片描述

④问题说明

通过实验发现,利用Zhon库中的上述只能去除, 。 !?四种符号,而且这四种符号必须是中文符号,无法去除英文符号,无法达到预期结果。。。

3.示例说明2:自定义特殊符号集进行去除

使用自定义的符号集,用于去除想去除的符号,保留对实验有用的符号。

import re
corpus='花呗***期免息!   蚂蚁花呗。***期免息什么意思?'
print(corpus)
string = re.sub("[\s+\.\!\/_,$%^*(+\"\']+|[+——!,。?、~@#¥%……&*()]", "",corpus)
print(string)

结果:
这里写图片描述

`jieba` 是一个非常流行的用于中文分词的 Python 第三方库。如果你想在使用 `jieba` 进行自然语言处理时去除标点符号,可以按照以下步骤操作: ### 去除标点符号的基本思路 1. **导入必要的模块**:首先需要加载 `jieba` 库以及其他支持工具(如字符串模块)。 2. **自定义过滤函数**:创建一个过滤规则来识别并移除文本中的标点符号。 3. **结合分词功能**:利用 `jieba.lcut()` 或其他分词方法对文本进行切割,并应用过滤条件。 以下是具体的代码示例: ```python import jieba import string # 定义要去掉的标点集合 (包括英文和中文常见标点) punctuations = set(string.punctuation) # 英文标点 chinese_punctuations = "!?。"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆" all_punctuations = punctuations.union(set(chinese_punctuations)) def remove_punctuation(text): words = jieba.lcut(text) # 分词 filtered_words = [word for word in words if word not in all_punctuations] # 移除标点 return "".join(filtered_words) if __name__ == "__main__": text = "你好,世界!Python编程真的很有意思..." result = remove_punctuation(text) print(result) # 输出结果为“你好世界Python编程真的很有意思” ``` 在这个例子中我们通过列表推导式检查每个单词是否属于预设好的所有标点字符集 `all_punctuations`, 若不属于则保留下来最终连接成新的无标点句子。 ### 注意事项 - 确保已经安装了 `jieba` 模块 (`pip install jieba`); - 可能还需要额外考虑一些特殊场景下的标点形式扩充到 `chinese_punctuations` 中。
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值