中文分词并字典化

一、数据集准备

在这里插入图片描述
在这里插入图片描述

二、代码实现

import csv
import fnmatch
import os
import re
from collections import OrderedDict
import jieba

text_list = []


def word_frequency_analysis(path):
    # print("111")
    files = os.listdir(path)  # files为列表,存储的是path里面的所有文件名
    for filename in files:  # 遍历文件夹中的所有文件
        if not fnmatch.fnmatch(filename, '*.TXT'):  # 找到txt文件
            continue
        txt_path = os.path.join(path, filename)
        txt_content = open(txt_path, 'rb').read()  # 读取txt文件中的中文

        text_cutted = jiebaCutText(txt_content)  # 用结巴分词
        text_list.append(text_cutted)  # 列表中添加分词后的结果


def jiebaCutText(text):
    seg_list = jieba.cut(text, cut_all=True)
    liststr = '/'.join(seg_list)  # 分词用/分割
    return liststr  # 返回的结果中会带标点符号


def jiebaCutText(text):
    seg_list = jieba.cut(text, cut_all=True)
    liststr = '/'.join(seg_list)
    return liststr  # 返回的结果中会带标点符号


def clearText(text):
    mywordlist = []
    for myword in text.split('/'):
        if len(myword.strip()) > 1 and contain_zh(myword.strip()):
            mywordlist.append(myword.strip())
    return '/'.join(mywordlist)


def contain_zh(word):
    zh = re.compile(u'[\u4200-\u9fa5]+')
    match = zh.search(word)
    return match


def countwords(text, counter_file):
    count_dict = dict()
    for item in text.split('/'):
        if item in count_dict:
            count_dict[item] += 1
        else:
            count_dict[item] = 1

    d_sorted_by_value = OrderedDict(sorted(count_dict.items(), key=lambda x: x[1]))
    with open(counter_file, 'w', encoding='utf-8-sig') as f:
        # f.write(codecs.BOM_UTF8)
        w = csv.writer(f)
        w.writerows(d_sorted_by_value.items())


if __name__ == '__main__':
    rootdir = r"C:\Users\fyyuj\Desktop\实验室练习\大作业\dataset"

    list = os.listdir(rootdir)  # 列出文件夹下所有的目录与文件

    for i in range(0, len(list)):
        path = os.path.join(rootdir, list[i])
        word_frequency_analysis(path)
    newfile = open(r'test1.txt', 'w')
    for i in range(0, len(text_list)):
        text_clear = clearText(text_list[i])
        newfile.write(text_clear)
    newfile.close()

    with open('test1.txt', 'r') as f:
        contents = f.read()
        contents = contents.split('/')

    with open('output.txt', 'w') as f:
        # f.write(str(contents))
        dic = dict(enumerate(contents))
        f.write(str(dic))

三、结果片段

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值