利用停顿词进行分词

只是为了记录避免忘记

import pandas as pd
import jieba

"""
去停用词stopwords
:param list content_lines: 将被处理文本的data
:param list new_content_lines: 存储处理好的data,默认=[]
:return list new_content_lines:  存储处理好的data
"""
def preprocess_text(content_lines, new_content_lines=[]):
    stopwords = pd.read_csv("data/stopwords.txt", index_col=False, quoting=3, sep='\t', names=['stopword'], encoding='utf-8')
    stopwords = list(stopwords['stopword'].values)
    
    for line in content_lines:
        segs_line = jieba.lcut(line)
        new_line = [word for word in segs_line if word not in stopwords]
        new_content_lines.append(new_line)
    return new_content_lines

# 数据准备
df_sports = pd.read_csv("./data/sports_news.csv", encoding='utf-8')
df_sports = df_sports.dropna()
sports = df_sports.content.values.tolist()[:200] # 取前200条数据。实际中可用20000条数据

# 数据处理
sentences = []
preprocess_text(sports, sentences)

# test
print(sentences)

运行结果

[ ['中新网', '深圳', '日电', ' ', '郑小红', '2019', '深圳'(此处省略)],
  ['王蔷', '岁', '天津', '纯真', '性格', '颇受欢迎',(此处省略)],
  ['寒冷', '冬季', '并未', '挡住', '奔跑', '热情'', (此处省略)],
  ......(此处省略两万字)......

参考连接:参考的博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值