清洗中文语料过程

语料需要的清洗的问题

  • 1、标点符号,中文标点混合英文标点符号,全半角等
  • 2、有一些特殊的表情符号存在于句子中
  • 3、还有一些标点符号重复使用
  • 4、至于繁体中文转中文,停用词等之类

1、规则匹配方法

### 匹配除了数字、英文标点、中文标点、中文字符、中文字符之外符号;这种符号一般可以去掉中文文本表达中的表情符号,特殊字符等之类的。

improt re
from string import punctuation
from string import digits
rule = re.compile(u'[^a-zA-Z.,;《》?!“”‘’@#¥%…&×()——+【】{};;●,。&~、|\s::'+digits+punctuation+'\u4e00-\u9fa5]+')
s= re.sub(rule, '', sentence)
###处理文本重复符号的表达,如替换多个。!.
s = re.sub('[!]+','!', s)
s = re.sub('[.]+','。', s)
s = re.sub('[。]+','。', s)

 2、处理整段中文语料上述问题,只提取中文部分

def clean_line(s):
    """
    :param s: 清洗爬取的中文语料格式
    :return:
    """
    import re
    from string import digits, punctuation
    rule = re.compile(u'[^a-zA-Z.,;《》?!“”‘’@#¥%…&×()——+【】{};;●,。&~、|\s::' + digits + punctuation + '\u4e00-\u9fa5]+')
    s = re.sub(rule, '', s)
    s = re.sub('[、]+', ',', s)
    s = re.sub('\'', '', s)
    s = re.sub('[#]+', ',', s)
    s = re.sub('[?]+', '?', s)
    s = re.sub('[;]+', ',', s)
    s = re.sub('[,]+', ',', s)
    s = re.sub('[!]+', '!', s)
    s = re.sub('[.]+', '.', s)
    s = re.sub('[,]+', ',', s)
    s = re.sub('[。]+', '。', s)
    s = s.strip().lower()
    return s

3、匹配中文字符

sent = "酒店<e0-2>特别好</e0>,<a0-2>前台</a0>办理入住的<a1-2>工作人员</a1><e1-2-a1>服务热情</e1>," \
       "<exp-fac-a1-e1>主动给升级了房间</exp-fac>,酒店<a2-2>环境</a2><e2-2-a2>超级好</e2>," \
       "有一个比较大的儿童乐园,然后走个2,30米又<exp-fac>有跷跷板啊,滑梯啊这样的小玩具</exp-fac>," \
       "酒店还有一个专门的儿童餐厅,<a3-2>早饭</a3><exp-fac-a3>都是各种卡通图形的面包,华夫饼</exp-fac>," \
       "<exp-sug>如果是亲子旅游的话,我超级推荐海韵酒店</exp-sug>。酒店<exp-fac>过了马路就是海," \
       "踏浪也很方便</exp-fac>。酒店<e3-2>干净</e3><exp-fac>没有蚊虫</exp-fac>。<e4-2>总之满分推荐</e4>。"
pa = re.compile(r'<[a0-9-]*>')
paa = re.compile(r'<[/][a0-9-]*>')
pe = re.compile(r'<e[a0-9-]*>')
pe2 = re.compile(r'</e[a0-9-]*>')
pexp = re.compile(r'<exp[a-z0-9-]*>')
pexp2 = re.compile(r'</exp[a-z0-9-]*>')
print(pa.findall(sent))
print(paa.findall(sent))
print(pe.findall(sent))
print(pe2.findall(sent))
print(pexp.findall(sent))
print(pexp2.findall(sent))

 

 

  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值