栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python文本文档去重、去停用词

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python文本文档去重、去停用词

问题描述

test.txt

你好吗
我很好
今天怎么样
今天怎么样
今天怎么样
今天怎么样
今天怎么样
今天怎么样
今天怎么样
今天怎么样
首先
高兴
是不是
说说

stopword.txt

首先
高兴
是不是
说说

对test.txt去重并去除stopword.txt定义的停用词




解决方案
  1. 使用生成器对文档进行读取,防止一次性读取超大文档内存不足




代码
def file_unique(filename, savefile, stopword='', encoding='utf-8'):
    '''文本文档去重去停用词

    :param filename: 需要处理的文本文档
    :param savefile: 保存路径
    :param stopword: 停用词文本文档
    :param encoding: 编码
    :return: 处理后的行数
    '''

    def read(filename, encoding='utf-8'):
        '''读取文本文档生成器'''
        with open(filename, encoding=encoding) as f:
            for line in f:
                yield line.strip()  # 去除空格换行

    file = set(list(read(filename, encoding)))
    if stopword:
        stopword = set(list(read(stopword, encoding)))
    newfile = []
    for i in file:
        if i not in stopword:
            newfile.append(i)
    with open(savefile, mode='w', encoding=encoding) as f:
        for i in newfile:
            f.write(i + 'n')
    return len(newfile)


if __name__ == '__main__':
    print(file_unique(filename='test.txt', savefile='out1.txt', encoding='utf-8'))
    print(file_unique(filename='test.txt', savefile='out2.txt', stopword='stopword.txt', encoding='utf-8'))
结果

out1.txt

你好吗
我很好
今天怎么样
首先
高兴
是不是
说说

out2.txt

你好吗
我很好
今天怎么样
根据拼音排序
from itertools import chain
from pypinyin import pinyin, Style


def to_pinyin(s):
    '''转拼音

    :param s: 字符串或列表
    :type s: str or list
    :return: 拼音字符串
    >>> to_pinyin('你好吗')
    'ni3hao3ma'
    >>> to_pinyin(['你好', '吗'])
    'ni3hao3ma'
    '''
    return ''.join(chain.from_iterable(pinyin(s, style=Style.TONE3)))

newfile = sorted(newfile, key=to_pinyin)  # 根据拼音排序




改进思路
  1. 不用newfile改用del file某个元素




参考文献
  1. io — 处理流的核心工具
  2. Python列表去重
  3. Python根据拼音对中文排序
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/833826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号