基于jieba包的自动提取
关键方法:jieba.analyse.extract_tags(content,topK=n)
具体思路:通过jieba包自带的extract_tags方法,在遍历读取文件内容时,获得每篇文档前n个关键字
使用的包:
import os
import codecs
import pandas as pd
import jieba
import jieba.analyse
过程:
'''定义变量
文件路径/文件内容/关键字(5个)'''
filepaths = []
contents =[]
tag1 = []
tag2 = []
tag3 = []
tag4 = []
tag5 = []
#遍历文件,同时得到关键字
for root, dirs, files in os.walk(
r'path'):
for name in files:
filepath = root + '\\' +name #根目录加文件名构成文件路径
f = codecs.open(filepath,'r','utf-8') #根据文件路径以只读的形式打开文件
content = f.read().strip() #将文件内容传入content变量
f.close() #关闭文件
tags = jieba.analyse.extract_tags(content,topK=5) #根据文件内容获取前5个关键字(出现次数最多)
filepaths.append(filepath) #得到文件路径的集合
contents.append(content) #得到文件内容的集合
tag1.append(tags[0])
tag2.append(tags[1])
tag3.append(tags[2])
tag4.append(tags[3])
tag5.append(tags[4])
tagDF = pd.DataFrame({
'文件路径':filepaths,
'文件内容':contents,
'关键词1':tag1,
'关键词2':tag2,
'关键词3':tag3,
'关键词4':tag4,
'关键词5':tag5})
最终得到包含文件路径,文件内容,和每篇5个关键字的数据框
基于TF-IDF算法的手动提取
关键:基于TF-IDF原理,引入分词权重的概念
词频(TF)
逆文档频率(IDF):词的权重,即词的重要程度
TF-IDF:权衡某个分词是否关键词的指标,值越大,是关键字的可能性就越大
TF-IDF的计算公式:
TF=该词在文档中出现的次数
IDF=log[文档总数/(包含该词的文档数+1)]
TF-IDF = TF*IDF
Tips:只提取中文关键字,用正则表达式判断分词是否为中文
具体实现:
#创建语料库,导入停用词
#获得分词结果
import re
zh = re.compile(u'[\u4e00-\u9fa5]+')
import jieba
segments = []
filepath = []
#导入停用词
stopwords = pd.read_csv(r'path',encoding='utf-8',index_col=False)
for index, row in corpos.iterrows():
filePath = row['filePath']
fileContent = row['fileContent']
segs = jieba.cut(fileContent)
for seg in segs:
if zh.search(seg): #只匹配中文分词
if (seg not in stopwords.stopword.values) and (len(seg.strip())>1): #取非停用词和长度>1的词
segments.append(seg)
filepath.append(filePath)
segmeng_DF = pd.DataFrame({
'segment': segments,
'filePath': filepath})
#词频统计
import numpy as np
segcount = segmeng_DF.groupby(by=['filePath','segment'
])['segment'].agg({'词频':np.size}
).reset_index().sort_values(by=['词频'],ascending=False)
segcount = segcount[segcount.词频 > 1] #只取词频大于1的分词
#词频向量化运算
TF =segcount.pivot_table(index='filePath',
columns='segment',
values='词频',
fill_value=0)
TF.columns #列名是各篇文章的分词集合
#根据公式分别得到IDF和TF-IDF的值
def hanlder(x):
return (np.log2(len(corpos) / (np.sum(x>0)+1)))
IDF = TF.apply(hanlder) #结果是各分词的权重
TF_IDF = pd.DataFrame(TF*IDF)
TF_IDF.columns #列名是各篇文章的分词集合
TF_IDF.index #索引是文件路径
#获取关键字
tag1 = []
tag2 = []
tag3 = []
tag4 = []
tag5 = []
for filePath in TF_IDF.index:
tagis = TF_IDF.loc[filePath].sort_values(ascending=False)[:5].index
tag1.append(tagis[0])
tag2.append(tagis[1])
tag3.append(tagis[2])
tag4.append(tagis[3])
tag5.append(tagis[4])
#最后得到包含文件路径,文件内容,和每篇5个关键字数据框