当前位置:   article > 正文

度量上市公司年报中的管理层语调(Python代码实现)_年报语调 csdn

年报语调 csdn

需求:度量上市公司年报的管理层语调的代码,要求给出积极(pos)语调、消极(neg)语调、积极词汇数量、消极词汇数量、段落总词数、停用词数量、总句数量,最后计算出管理层的乐观主义指标。最终结果要输入到Excel中,按照年份和公司代码排列。代码要求适应环境为python3,可以更改年报的选取时间。

环境:

Python3实现度量上市公司年报管理层语调,并输出到Excel中。

本代码使用了中文停用词库和情感词库来进行文本分析。在运行代码前,请确保已经安装以下Python库

  • jieba
  • pandas

代码实现:

  1. import jieba
  2. import pandas as pd
  3. import numpy as np
  4. import os
  5. import re
  6. # 设置停用词表
  7. stopwords_path = 'stopwords.txt'
  8. stopwords = set([line.strip() for line in open(stopwords_path, 'r', encoding='utf-8').readlines()])
  9. # 设置情感词表
  10. posdict_path = 'posdict.txt'
  11. posdict = set([line.strip() for line in open(posdict_path, 'r', encoding='utf-8').readlines()])
  12. negdict_path = 'negdict.txt'
  13. negdict = set([line.strip() for line in open(negdict_path, 'r', encoding='utf-8').readlines()])
  14. # 对文本进行分词并过滤停用词
  15. def segment(text):
  16. seg_list = jieba.cut(text)
  17. filtered_words = [word for word in seg_list if word not in stopwords]
  18. return filtered_words
  19. # 计算积极语调、消极语调、积极词汇数量、消极词汇数量、段落总词数、停用词数量、总句数量
  20. def analyze_text(text):
  21. pos_count = 0
  22. neg_count = 0
  23. pos_word_count = 0
  24. neg_word_count = 0
  25. word_count = 0
  26. stopword_count = 0
  27. sentence_count = 0
  28. # 将文本按照段落分割
  29. paragraphs = re.split('\n|\r', text)
  30. for para in paragraphs:
  31. if not para.strip():
  32. continue
  33. # 将段落分割成句子
  34. sentences = re.split('[。!?]', para)
  35. for sentence in sentences:
  36. if not sentence.strip():
  37. continue
  38. # 将句子分词并过滤停用词
  39. words = segment(sentence)
  40. word_count += len(words)
  41. stopword_count += sum([1 for word in words if word in stopwords])
  42. pos_word_count += len([word for word in words if word in posdict])
  43. neg_word_count += len([word for word in words if word in negdict])
  44. # 判断句子的情感倾向
  45. if pos_word_count > neg_word_count:
  46. pos_count += 1
  47. elif pos_word_count < neg_word_count:
  48. neg_count += 1
  49. sentence_count += 1
  50. return pos_count, neg_count, pos_word_count, neg_word_count, word_count, stopword_count, sentence_count
  51. # 计算乐观主义指标
  52. def calculate_optimism(pos_count, neg_count, sentence_count):
  53. if pos_count + neg_count == 0:
  54. return 0
  55. optimism = pos_count / (pos_count + neg_count)
  56. return optimism

读取年报,并分析文本,计算上述文本指标,并输出到Excel中:

  1. # 读取年报文件并分析文本
  2. def analyze_report(file_path):
  3. with open(file_path, 'r', encoding='utf-8') as f:
  4. text = f.read()
  5. pos_count, neg_count, pos_word_count, neg_word_count, word_count, stopword_count, sentence_count = analyze_text(text)
  6. optimism = calculate_optimism(pos_count, neg_count, sentence_count)
  7. return {
  8. 'pos_count': pos_count,
  9. 'neg_count': neg_count,
  10. 'pos_word_count': pos_word_count,
  11. 'neg_word_count': neg_word_count,
  12. 'word_count': word_count,
  13. 'stopword_count': stopword_count,
  14. 'sentence_count': sentence_count,
  15. 'optimism': optimism
  16. }
  17. # 读取目录下的所有年报文件并分析
  18. def analyze_reports(dir_path):
  19. company_reports = {}
  20. for root, dirs, files in os.walk(dir_path):
  21. for file in files:
  22. if file.endswith('.txt'):
  23. company_code = file.split('_')[0]
  24. file_path = os.path.join(root, file)
  25. report_data = analyze_report(file_path)
  26. if company_code in company_reports:
  27. company_reports[company_code].append(report_data)
  28. else:
  29. company_reports[company_code] = [report_data]
  30. return company_reports
  31. # 将数据存储在Pandas DataFrame中并输出到Excel文件
  32. def save_to_excel(company_reports, output_path):
  33. rows = []
  34. for company_code, reports in company_reports.items():
  35. for report in reports:
  36. row = {
  37. 'company_code': company_code,
  38. 'pos_count': report['pos_count'],
  39. 'neg_count': report['neg_count'],
  40. 'pos_word_count': report['pos_word_count'],
  41. 'neg_word_count': report['neg_word_count'],
  42. 'word_count': report['word_count'],
  43. 'stopword_count': report['stopword_count'],
  44. 'sentence_count': report['sentence_count'],
  45. 'optimism': report['optimism']
  46. }
  47. rows.append(row)
  48. df = pd.DataFrame(rows)
  49. df.to_excel(output_path, index=False)
  50. # 设置年报文件目录和输出Excel文件路径
  51. dir_path = 'annual_reports'
  52. output_path = 'annual_report_data.xlsx'
  53. # 分析年报并存储数据到Excel文件中
  54. company_reports = analyze_reports(dir_path)
  55. save_to_excel(company_reports, output_path)

在使用此代码之前,需要将停用词表、积极词表和消极词表放置在与代码相同的目录中,并将其命名为stopwords.txtposdict.txtnegdict.txt

此外,还需要将要分析的年报文件放置在名为annual_reports的文件夹中。

在运行完整代码后,将在指定的输出路径中生成一个Excel文件,其中包含乐观主义指标以及其他有关年报的分析数据,按公司代码和年份排序。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/720205
推荐阅读
相关标签
  

闽ICP备14008679号