日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

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

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

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 = ().strip()

return content

#統計詞頻

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("成功創建文件夾!")

#寫入文件

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"分詞結果"

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計算結果"

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//%"%(new_folder,outfile_name)

out_file(out_path,tf_idf)

i=i+1

if __name__ == '__main__':

main()

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。