当前位置:   article > 正文

python文本数据分析作业分享案例_python文本分析案例

python文本分析案例

作业需求:

分析两本类型不同的现代小说的词性分布,如武侠或侦探小说等.用一个类读入整本小说。用自然语言处理工具。初始化过程分析内容。分解词语并获取词性(如动词.形容词等).类对象取索引返回词和词性两项主要信息在调用类对象的函数中,实现词性的统计。用饼状图可视化个主要词性频率,对比两本小说的饼状

编辑

全部代码:

  1. import jieba
  2. import jieba.posseg
  3. import matplotlib.pyplot as plt
  4. import matplotlib
  5. from pylab import mpl
  6. mpl.rcParams['font.sans-serif'] = ['SimHei'] # 字体更改
  7. matplotlib.rcParams.update({'font.size': 15}) # 字体大小
  8. fig = plt.figure(figsize=(8, 8), dpi=80)
  9. word_type = ["a", "d", "n", "p", "r", "u", "v", "y"]
  10. word_type_chin = ["形容词", "副词", "名词", "介词", "代词", "助词", "动词", "语气词"]
  11. class Text():
  12. def init(self):
  13. with open("yitian.txt", mode="r", encoding="utf8") as txt1:
  14. a = txt1.read()
  15. with open("baiyexing.txt", mode="r", encoding="gbk") as txt2:
  16. b = txt2.read()
  17. self.txt = [a, b]
  18. self.output = [[], []]
  19. self.flag = [[], []]
  20. self.word = [[], []]
  21. self.identify(self)
  22. return self.output[0], self.output[1]
  23. def identify(self):
  24. for x in range(0, 2):
  25. self.txt[x] = jieba.posseg.cut(self.txt[x])
  26. for text in self.txt[x]:
  27. self.output[x].append([text.word, text.flag])
  28. self.flag[x].append(text.flag)
  29. for t in range(0, 8):
  30. print(f"{word_type_chin[t]}: {self.flag[x].count(word_type[t])}")
  31. self.word[x].append(self.flag[x].count(word_type[t]))
  32. def pie(self):
  33. for x in range(0, 2):
  34. fig.add_subplot(1, 2, x + 1)
  35. plt.pie(self.word[x],
  36. labels=word_type_chin, # 设置饼图标签
  37. # radius=1.2,
  38. autopct="%d%%",
  39. )
  40. if x == 0:
  41. text_type = "武侠"
  42. elif x == 1:
  43. text_type = "侦探"
  44. plt.title(f"{text_type}小说的词性分布")
  45. fig.show()
  46. text_1, text_2 = Text.init(Text)
  47. print(f"武侠小说:\n{text_1}")
  48. print(f"\n侦探小说:\n{text_2}")
  49. Text.pie(Text)
  50. input(" >>> ENTER以继续 <<< ")

编辑

数据+代码:

https://download.csdn.net/download/qq_38735017/87354408

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

闽ICP备14008679号