5聊天机器人_文本分词

文本分词

目标

  1. 完成停用词的准备
  2. 完成分词方法的封装

1. 准备词典和停用词

1.1 准备词典

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-11DpUGBy-1613751653380)(…/images/2.1/词典.png)]

1.2 准备停用词

stopwords = set([i.strip() for i in open(config.stopwords_path).readlines()])

2. 准备按照单个字切分句子的方法

def _cut_by_word(sentence):
    # 对中文按照字进行处理,对英文不分为字母
    sentence = re.sub("\s+"," ",sentence)
    sentence = sentence.strip()
    result = []
    temp = ""
    for word in sentence:
        if word.lower() in letters:
            temp += word.lower()
        else:
            if temp != "": #不是字母
                result.append(temp)
                temp = ""
            if word.strip() in filters: #标点符号
                continue
            else: #是单个字
                result.append(word)
    if temp != "": #最后的temp中包含字母
        result.append(temp)
    return result

3. 完成分词方法的封装

lib 下创建cut_sentence.py文件,完成分词方法的构建

import logging
import jieba
import jieba.posseg as psg
import config
import re
import string

#关闭jieba log输出
jieba.setLogLevel(logging.INFO)
#加载词典
jieba.load_userdict(config.keywords_path)
#单字分割,英文部分
letters = string.ascii_lowercase
#单字分割 去除的标点
filters= [",","-","."," "]
#停用词
stopwords = set([i.strip() for i in open(config.stopwords_path).readlines()])


def cut(sentence,by_word=False,use_stopwords=False,with_sg=False):
    assert by_word!=True or with_sg!=True,"根据word切分时候无法返回词性"
    if by_word:
        return _cut_by_word(sentence)
    else:
        ret = psg.lcut(sentence)
        if use_stopwords:
            ret = [(i.word,i.flag) for i in ret if i.word not in stopwords]
        if not with_sg:
            ret = [i.word for i in ret]
        return ret


def _cut_by_word(sentence):
    # 对中文按照字进行处理,对英文不分为字母
    sentence = re.sub("\s+"," ",sentence)
    sentence = sentence.strip()
    result = []
    temp = ""
    for word in sentence:
        if word.lower() in letters:
            temp += word.lower()
        else:
            if temp != "": #不是字母
                result.append(temp)
                temp = ""
            if word.strip() in filters: #标点符号
                continue
            else: #是单个字
                result.append(word)
    if temp != "": #最后的temp中包含字母
        result.append(temp)
    return result
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值