年报文本分析:jieba词频统计

该博客通过Python操作PDF,分析了2015年至2020年间制造和其它类别年报中关键词的出现频率,揭示了信息技术词汇在不同时间段的重要趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添词、计数

import os
import re
import time
import math
import openpyxl
import fitz
import jieba
import jieba.analyse
from collections import Counter
'''
使用Python操作PDF:常用PDF库总结 - 知乎
https://zhuanlan.zhihu.com/p/352722932
'''
def pdr_reader(file):
    doc = fitz.open(file)
    content=''
    for page in doc:
        text = page.get_text()
        content=content+text
    return content
def keyword_count(content,words):
    raw_list = jieba.lcut(content)
    counts = {}
    for word in raw_list:
        if word in words:
            counts[word] = counts.get(word, 0) + 1
    return counts
def count_sum(counts):
    count1 = 0
    count2 = 0
    for key in counts:
        count1 += 1
        count2 += counts.get(key, 0)
    return [count1,count2]
file=r"E:\Alark\Desktop\数字化关键词.txt"
f=open(file,'r',encoding='utf-8')
requests=re.compile(r'\w*[\u4e00-\u9fa5]*、',re.S)
words=[]
result=re.finditer(requests,f.read())
for p in result:
    tmp=re.sub("、",'',p.group(0))
    words.append(tmp)
f.close()
for word in words:
    jieba.add_word(word)
#########
mywb = openpyxl.load_workbook(r'G:\词频统计.xlsx')
mysheet = mywb.active
row=0
list=[]
for r in mysheet.rows:
    if r[0].value!=None:
        row+=1
        list.append((r[0].value,r[1].value))
        if mysheet['A'+str(row+1)]==None:
            break
input=r'G:\年报'
order=0
for i in range(0,6):
    path=input+'\\'+str(2015+i)+'\\'+"制造"
    os.chdir(path)
    for file in os.listdir():
        if os.path.splitext(file)[1]=='.pdf':
            print(str(order+1)+'、'+file+':processing......')
            t1=time.monotonic()
            code=os.path.splitext(file)[0][:6]
            if (code,2015+i) in list:
                order+=1
                print(str(order) + '、' + file + ': is already there.')
            else:
                year=2015+i
                content = pdr_reader(file)
                counts = keyword_count(content)
                [count1,count2]=count_sum(counts)
                values=[code,year,count1,count2,math.log(count1+1,math.e),math.log(count2+1,math.e),1]
                row += 1
                order+=1
                list.append((code,year))
                for j in range(0,7):
                    mysheet.cell(row=row, column=j+1, value=values[j])
                t2 = time.monotonic()
                print(str(order) + '、' + file + ':done.',str(t2-t1)+'s')
                mywb.save(r'G:\词频统计.xlsx')
    path = input + '\\' + str(2015 + i) + '\\' + "其他"
    os.chdir(path)
    for file in os.listdir():
        if os.path.splitext(file)[1]=='.pdf':
            print(str(row+1)+'、'+file+':processing......')
            t1 = time.monotonic()
            code=os.path.splitext(file)[0][:6]
            if (code,2015+i) in list:
                order+=1
                print(str(order) + '、' + file + ': is already there.')
            else:
                year=2015+i
                content = pdr_reader(file)
                counts = keyword_count(content)
                [count1,count2]=count_sum(counts)
                values=[code,year,count1,count2,math.log(count1+1,math.e),math.log(count2+1,math.e),0]
                order+=1
                row += 1
                list.append((code, year))
                for j in range(0,7):
                    mysheet.cell(row=row, column=j+1, value=values[j])
                t2 = time.monotonic()
                print(str(row) + '、' + file + ':done.',str(t2-t1)+'s')
                mywb.save(r'G:\词频统计.xlsx')
mywb.save(r'G:\词频统计.xlsx')

特定章节选取

确定成功提取内容是关键
思路:返回最大中文片段==标题

在处理上市公司年报时,可以使用Python中的jieba库进行关键词提取和词频统计的程序。以下是一个年报样例的处理步骤: 首先,我们需要将年报文本读入程序中。假设该年报的文本保存在一个名为"annual_report.txt"的文本文件中,我们可以使用Python的文件读取功能来读取该文件,将其内容存储在一个字符串变量中。 接下来,需要使用jieba库来进行中文分词。首先,我们需要使用jieba的初始化函数进行分词器的初始化,并加载自定义的词典(如果有)。然后,使用jieba的分词函数来对年报文本进行分词处理。分词结果可以保存在一个列表变量中。 之后,我们可以利用Python的数据结构来进行数据处理和统计。我们可以使用一个字典变量来存储每个关键词的词频统计结果。遍历分词结果列表,对每个分词进行判断,如果分词已经在字典中,我们就将对应的词频加1;如果分词不在字典中,我们就在字典中新增该关键词,并将其词频设置为1。 最后,我们可以按照词频进行排序,以便找出出现频率最高的关键词。可以使用Python的排序函数对字典进行排序,按照词频降序排列。然后,根据需要,可以选择前几个关键词进行输出显示。 综上所述,使用Python中的jieba库可以实现上市公司年报的关键词提取和词频统计。这个程序可以帮助我们对年报进行数字化处理,并快速地获取关键信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

圣道寺

您的支持将使我的探索更进一步

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值