Skip to content

Latest commit

 

History

History
71 lines (53 loc) · 1.68 KB

File metadata and controls

71 lines (53 loc) · 1.68 KB
Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0483
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在: 
	- 个人网站: `https://oeasy.org` 
	- 蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
	- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
	- Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

实验标题

实验介绍

下载四大名著

wget https://labfile.shiyanlou.com/courses/3584/novels.zip
unzip novels.zip
  • 下载并解压zip

图片描述

红楼梦

  • 情感分析
    • 分析各个章节的情感倾向
    • 绘制情感曲线
    • 看看故事情节的起伏
from snownlp import SnowNLP
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei']  # 文泉驿正黑
plt.rcParams['axes.unicode_minus'] = False

def analyze_emotion(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        content = f.read()

    # 按章节分割
    chapters = content.split('第')[1:]  # 假设每章都以"第"字开头
    emotions = []

    for chapter in chapters:
        s = SnowNLP(chapter)
        emotion_score = s.sentiments  # 获取情感分数
        emotions.append(emotion_score)

    # 绘制情感曲线
    plt.figure(figsize=(15, 6))
    plt.plot(emotions)
    plt.title('情感变化曲线')
    plt.xlabel('章节')
    plt.ylabel('情感值(0-负面,1-正面)')
    plt.show()

# 使用示例
analyze_emotion('hlm.txt')

西游记


  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。