溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python3 中怎么提取文章標(biāo)題關(guān)鍵字

發(fā)布時(shí)間:2021-06-17 17:05:02 來(lái)源:億速云 閱讀:331 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

Python3 中怎么提取文章標(biāo)題關(guān)鍵字,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

思路:

1.讀取所有文章標(biāo)題;

2.用“結(jié)巴分詞”的工具包進(jìn)行文章標(biāo)題的詞語(yǔ)分割;

3.用“sklearn”的工具包計(jì)算Tf-idf(詞頻-逆文檔率);

4.得到滿足關(guān)鍵詞權(quán)重閾值的詞

結(jié)巴分詞詳見(jiàn):結(jié)巴分詞Github

sklearn詳見(jiàn):文本特征提取——4.2.3.4 Tf-idf項(xiàng)加權(quán)

import os
import jieba
import sys
from sklearn.feature_extraction.text import TfidfVectorizer
 
 
sys.path.append("../")
jieba.load_userdict('userdictTest.txt')
STOP_WORDS = set((
  "基于", "面向", "研究", "系統(tǒng)", "設(shè)計(jì)", "綜述", "應(yīng)用", "進(jìn)展", "技術(shù)", "框架", "txt"
 ))
 
def getFileList(path):
 filelist = []
 files = os.listdir(path)
 for f in files:
  if f[0] == '.':
   pass
  else:
   filelist.append(f)
 return filelist, path
 
def fenci(filename, path, segPath):
 
 # 保存分詞結(jié)果的文件夾
 if not os.path.exists(segPath):
  os.mkdir(segPath)
 seg_list = jieba.cut(filename)
 result = []
 for seg in seg_list:
  seg = ''.join(seg.split())
  if len(seg.strip()) >= 2 and seg.lower() not in STOP_WORDS:
   result.append(seg)
 
 # 將分詞后的結(jié)果用空格隔開(kāi),保存至本地
 f = open(segPath + "/" + filename + "-seg.txt", "w+")
 f.write(' '.join(result))
 f.close()
 
def Tfidf(filelist, sFilePath, path, tfidfw):
 corpus = []
 for ff in filelist:
  fname = path + ff
  f = open(fname + "-seg.txt", 'r+')
  content = f.read()
  f.close()
  corpus.append(content)
 
 vectorizer = TfidfVectorizer() # 該類(lèi)實(shí)現(xiàn)詞向量化和Tf-idf權(quán)重計(jì)算
 tfidf = vectorizer.fit_transform(corpus)
 word = vectorizer.get_feature_names()
 weight = tfidf.toarray()
 
 if not os.path.exists(sFilePath):
  os.mkdir(sFilePath)
 
 for i in range(len(weight)):
  print('----------writing all the tf-idf in the ', i, 'file into ', sFilePath + '/', i, ".txt----------")
  f = open(sFilePath + "/" + str(i) + ".txt", 'w+')
  result = {}
  for j in range(len(word)):
   if weight[i][j] >= tfidfw:
    result[word[j]] = weight[i][j]
  resultsort = sorted(result.items(), key=lambda item: item[1], reverse=True)
  for z in range(len(resultsort)):
   f.write(resultsort[z][0] + " " + str(resultsort[z][1]) + '\r\n')
   print(resultsort[z][0] + " " + str(resultsort[z][1]))
  f.close()

TfidfVectorizer( ) 類(lèi) 實(shí)現(xiàn)了詞向量化和Tf-idf權(quán)重的計(jì)算

詞向量化:vectorizer.fit_transform是將corpus中保存的切分后的單詞轉(zhuǎn)為詞頻矩陣,其過(guò)程為先將所有標(biāo)題切分的單詞形成feature特征和列索引,并在dictionary中保存了{(lán)‘特征':索引,……},如{‘農(nóng)業(yè)':0,‘大數(shù)據(jù)':1,……},在csc_matric中為每個(gè)標(biāo)題保存了 (標(biāo)題下標(biāo),特征索引) 詞頻tf……,然后對(duì)dictionary中的單詞進(jìn)行排序重新編號(hào),并對(duì)應(yīng)更改csc_matric中的特征索引,以便形成一個(gè)特征向量詞頻矩陣,接著計(jì)算每個(gè)feature的idf權(quán)重,其計(jì)算公式為 Python3 中怎么提取文章標(biāo)題關(guān)鍵字 其中是所有文檔數(shù)量,是包含該單詞的文檔數(shù)。最后計(jì)算tf*idf并進(jìn)行正則化,得到關(guān)鍵詞權(quán)重。

以下面六個(gè)文章標(biāo)題為例進(jìn)行關(guān)鍵詞提取

Python3 中怎么提取文章標(biāo)題關(guān)鍵字

Using jieba on 農(nóng)業(yè)大數(shù)據(jù)研究與應(yīng)用進(jìn)展綜述.txt

Using jieba on 基于Hadoop的分布式并行增量爬蟲(chóng)技術(shù)研究.txt

Using jieba on 基于RPA的財(cái)務(wù)共享服務(wù)中心賬表核對(duì)流程優(yōu)化.txt

Using jieba on 基于大數(shù)據(jù)的特征趨勢(shì)統(tǒng)計(jì)系統(tǒng)設(shè)計(jì).txt

Using jieba on 網(wǎng)絡(luò)大數(shù)據(jù)平臺(tái)異常風(fēng)險(xiǎn)監(jiān)測(cè)系統(tǒng)設(shè)計(jì).txt

Using jieba on 面向數(shù)據(jù)中心的多源異構(gòu)數(shù)據(jù)統(tǒng)一訪問(wèn)框架.txt

----------writing all the tf-idf in the 0 file into ./keywords/ 0 .txt----------

農(nóng)業(yè) 0.773262366783

大數(shù)據(jù) 0.634086202434

----------writing all the tf-idf in the 1 file into ./keywords/ 1 .txt----------

hadoop 0.5

分布式 0.5

并行增量 0.5

爬蟲(chóng) 0.5

----------writing all the tf-idf in the 2 file into ./keywords/ 2 .txt----------

rpa 0.408248290464

優(yōu)化 0.408248290464

服務(wù)中心 0.408248290464

流程 0.408248290464

財(cái)務(wù)共享 0.408248290464

賬表核對(duì) 0.408248290464

----------writing all the tf-idf in the 3 file into ./keywords/ 3 .txt----------

特征 0.521823488025

統(tǒng)計(jì) 0.521823488025

趨勢(shì) 0.521823488025

大數(shù)據(jù) 0.427902724969

----------writing all the tf-idf in the 4 file into ./keywords/ 4 .txt----------

大數(shù)據(jù)平臺(tái) 0.4472135955

異常 0.4472135955

監(jiān)測(cè) 0.4472135955

網(wǎng)絡(luò) 0.4472135955

風(fēng)險(xiǎn) 0.4472135955

----------writing all the tf-idf in the 5 file into ./keywords/ 5 .txt----------

多源異構(gòu)數(shù)據(jù) 0.57735026919

數(shù)據(jù)中心 0.57735026919

統(tǒng)一訪問(wèn) 0.57735026919

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI