日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python如何打开txt文件、并算词频_python TF-IDF词频算法实现文本关键词提取代码...

發(fā)布時間:2025/3/20 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何打开txt文件、并算词频_python TF-IDF词频算法实现文本关键词提取代码... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

TF(Term Frequency)詞頻,在文章中出現(xiàn)次數(shù)最多的詞,然而文章中出現(xiàn)次數(shù)較多的詞并不一定就是關(guān)鍵詞,比如常見的對文章本身并沒有多大意義的停用詞。所以我們需要一個重要性調(diào)整系數(shù)來衡量一個詞是不是常見詞。該權(quán)重為IDF(Inverse Document Frequency)逆文檔頻率,它的大小與一個詞的常見程度成反比。在我們得到詞頻(TF)和逆文檔頻率(IDF)以后,將兩個值相乘,即可得到一個詞的TF-IDF值,某個詞對文章的重要性越高,其TF-IDF值就越大,所以排在最前面的幾個詞就是文章的關(guān)鍵詞。

TF-IDF算法的優(yōu)點是簡單快速,結(jié)果比較符合實際情況,但是單純以“詞頻”衡量一個詞的重要性,不夠全面,有時候重要的詞可能出現(xiàn)的次數(shù)并不多,而且這種算法無法體現(xiàn)詞的位置信息,出現(xiàn)位置靠前的詞和出現(xiàn)位置靠后的詞,都被視為同樣重要,是不合理的。

TF-IDF算法步驟:

(1)、計算詞頻:

詞頻 = 某個詞在文章中出現(xiàn)的次數(shù)

考慮到文章有長短之分,考慮到不同文章之間的比較,將詞頻進行標準化

詞頻 = 某個詞在文章中出現(xiàn)的次數(shù)/文章的總詞數(shù)

詞頻 = 某個詞在文章中出現(xiàn)的次數(shù)/該文出現(xiàn)次數(shù)最多的詞出現(xiàn)的次數(shù)

(2)、計算逆文檔頻率

需要一個語料庫(corpus)來模擬語言的使用環(huán)境。

逆文檔頻率 = log(語料庫的文檔總數(shù)/(包含該詞的文檔數(shù) + 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 = ().strip()

return content

#統(tǒng)計詞頻

def count_word(content):

word_dic ={}

words_list = ("/")

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)

(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 * (num_files/(word_idf[key]+1))

#降序排序

values_list = sorted((),key = lambda item:item[1],reverse=True)

return values_list

#新建文件夾

def buildfolder(path):

if os.path.exists(path):

(path)

(path)

print("成功創(chuàng)建文件夾!")

#寫入文件

def out_file(path,content_list):

with codecs.open(path,"a",encoding="utf-8") as f:

for content in content_list:

(str(content[0]) + ":" + str(content[1])+"\r\n")

print("well done!")

def main():

#遍歷文件夾

folder_path = r"分詞結(jié)果"

files_array = funfolder(folder_path)

#生成語料庫

files_dic = []

for file_path in files_array:

file = readtxt(file_path)

word_dic = count_word(file)

(word_dic)

#新建文件夾

new_folder = r"tfidf計算結(jié)果"

buildfolder(new_folder)

#計算tf-idf,并將結(jié)果存入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//%"%(new_folder,outfile_name)

out_file(out_path,tf_idf)

i=i+1

if __name__ == '__main__':

main()

總結(jié)

以上是生活随笔為你收集整理的python如何打开txt文件、并算词频_python TF-IDF词频算法实现文本关键词提取代码...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。