这个只是在以列表形式的状况下对数据进行存储
可以增添和完善其他功能,有空更新
#这个程序的作用可以把源程序文件夹里面的txt文件出现的词语统计到新文件夹里,可以去掉stopwords里的词,也可以自己添加新词
# coding = utf-8 # author:A_Kang
#这个程序的作用可以把源程序文件夹里面的txt文件出现的词语统计到新文件夹里,可以去掉stopwords里的词,也可以自己添加新词 import os
import math import jieba from jieba import analyse def list_dir(filepath):#
获得当前文件夹下所有文件名 dir_names=os.listdir(filepath) #function:返回指定路径下的文件和文件夹列表。
dir_names=os.walk(filepath) #输出在文件夹中的文件名通过在树中游走,向上或者向下。 return dir_names
#返回遍历的文件 def Num_in_set(tf_dic): global wordNum_in_set # 准备将此篇文献中的词,导入到词出现总数文档中
for term in tf_dic: if term in wordNum_in_set: wordNum_in_set[term]+=1 else:
wordNum_in_set[term]=1 def dic_fw(a_dic,file,dicname): fw_path=dicname+
'-result\\'+file.split('\\')[-3]+'\\'+file.split('\\')[-2]+'\\' if os.path.
exists(fw_path)==False: os.makedirs(fw_path) if dicname=='tj': fw_path=fw_path+
file.split('\\')[-1].replace('.txt','-'+dicname+'.txt') # 写入文档位置 fw=open(fw_path
,'w+',encoding='utf-8') for term in a_dic: fw.write(term+'\n') print(term+'\n')
#计算出tj值 if dicname=='tj_idf': fw_path=fw_path+file.split('\\')[-1].replace(
'-tj.txt','-'+dicname+'.txt') # 写入文档位置 fw=open(fw_path,'w+',encoding='utf-8')
for term in a_dic: fw.write(term[0]+'\t'+str(term[1])+'\n') print(term[0]+'\t'+
str(term[1]/max_num)) #计算出tf值 fw.close() def stopwordslist(filepath): stopwords
= [line.strip() for line in open(filepath,'r',encoding='utf-8').readlines()]
return stopwords def idf_write(): global wordNum_in_set wordNum_in_set=sorted(
wordNum_in_set.items(),key=lambda item:item[1]) with open('idf.txt','w+',
encoding='utf-8') as idf: for term in wordNum_in_set: idf.write(term[0]+'\t'+str
(term[1])+'\n') print('词出现次数写入完成---idf.txt') def get_Doc_path_lists(filepath):
# 获取文献地址,返回列表 filelists=[] dir_names=list_dir(filepath) for root,dirs,files in
dir_names: for file in files: if '.txt' in file: print(root+'\\'+file) filelists
.append(root+'\\'+file) # 加地址 return filelists def main():
#jieba.analyse.set_stop_words('s.txt') jieba.load_userdict('w.txt') files=
get_Doc_path_lists(filepath) Doc_Num=len(files) #文献总数 tj_dic={} print(files)
stop=stopwordslist(stop_wordspath) for file in files: with open(file,"r",
encoding='gbk') as f: wenben=f.read() wenben_list=jieba.lcut(wenben) for word in
wenben_list: if word in stop: wenben_list.remove(word) print(wenben_list) #for
i in wenben_list: # tj_dic[i[0]]+=1 #print(tj_dic) #dic_fw(tj_dic,file,'tj')
dic_fw(wenben_list,file,'tj') print(file+':写入完成!') global filepath # 根地址,可改
filepath=r"E:\train\jieba1\jieba1\sourse" global stopword_list # 停用词表
stopword_list=[] global wordNum_in_set # 记录出现过某词的文档数 wordNum_in_set={} global
stop_wordspath stop_wordspath=r"E:\train\jieba1\jieba1\s.txt" if __name__==
'__main__': main()

技术
今日推荐
PPT
阅读数 131
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信