python TF-IDF算法实现文本关键词提取?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
TF-IDF算法步骤:
(1)、计算词频:
词频 = 某个词在文章中出现的次数
考虑到文章有长短之分,考虑到不同文章之间的比较,将词频进行标准化
词频 = 某个词在文章中出现的次数/文章的总词数
词频 = 某个词在文章中出现的次数/该文出现次数最多的词出现的次数
(2)、计算逆文档频率
需要一个语料库(corpus)来模拟语言的使用环境。
逆文档频率 = log(语料库的文档总数/(包含该词的文档数 + 1))
(3)、计算TF-IDF
TF-IDF = 词频(TF)* 逆文档频率(IDF)
详细代码如下:
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 计算文档的TF-IDF ''' import codecs import os import math import shutil #读取文本文件 def readtxt(path): with codecs.open(path,"r",encoding="utf-8") as f: content = f.read().strip() return content #统计词频 def count_word(content): word_dic ={} words_list = content.split("/") del_word = ["\r\n","/s"," ","/n"] for word in words_list: if word not in del_word: if word in word_dic: word_dic[word] = word_dic[word]+1 else: word_dic[word] = 1 return word_dic #遍历文件夹 def funfolder(path): filesArray = [] for root,dirs,files in os.walk(path): for file in files: each_file = str(root+"//"+file) filesArray.append(each_file) return filesArray #计算TF-IDF def count_tfidf(word_dic,words_dic,files_Array): word_idf={} word_tfidf = {} num_files = len(files_Array) for word in word_dic: for words in words_dic: if word in words: if word in word_idf: word_idf[word] = word_idf[word] + 1 else: word_idf[word] = 1 for key,value in word_dic.items(): if key !=" ": word_tfidf[key] = value * math.log(num_files/(word_idf[key]+1)) #降序排序 values_list = sorted(word_tfidf.items(),key = lambda item:item[1],reverse=True) return values_list #新建文件夹 def buildfolder(path): if os.path.exists(path): shutil.rmtree(path) os.makedirs(path) print("成功创建文件夹!") #写入文件 def out_file(path,content_list): with codecs.open(path,"a",encoding="utf-8") as f: for content in content_list: f.write(str(content[0]) + ":" + str(content[1])+"\r\n") print("well done!") def main(): #遍历文件夹 folder_path = r"分词结果" files_array = funfolder(folder_path) #生成语料库 files_dic = [] for file_path in files_array: file = readtxt(file_path) word_dic = count_word(file) files_dic.append(word_dic) #新建文件夹 new_folder = r"tfidf计算结果" buildfolder(new_folder) #计算tf-idf,并将结果存入txt i=0 for file in files_dic: tf_idf = count_tfidf(file,files_dic,files_array) files_path = files_array[i].split("//") #print(files_path) outfile_name = files_path[1] #print(outfile_name) out_path = r"%s//%s_tfidf.txt"%(new_folder,outfile_name) out_file(out_path,tf_idf) i=i+1 if __name__ == '__main__': main()
看完上述内容,你们掌握python TF-IDF算法实现文本关键词提取的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。