大一期末作业

期末

词性标注

1
2
3
4
5
import jieba.posseg as pseg
text = '我杀掉了你的全家'
words = pseg.cut(text)
words = list(words)
print(words)

增加自定义词典

1
2
3
4
5
6
7
8
9
10
import jieba
text = '我是茸华似雪'
words = jieba.lcut(text,cut_all=True)
print('分词前:',words)
##  ===================================
import jieba
jieba.add_word('茸华')
text = '我是茸华似雪'
words = jieba.lcut(text,cut_all=True)
print('分词后:',words)

词频前10排序

1
2
3
4
5
6
7
8
9
10
import jieba
with open("C:/Users/69099/Desktop/11.txt","r",encoding="utf-8") as file:		# 打开文件
    txt = file.read()									                        # 读取文件
text = jieba.lcut(txt)                                                          # 分词
counts = {}                                                                     # 统计词频
for i in text:
    if len(i) > 1:                                                              # 统计长度为1的词
        counts[i] = counts.get(i,0) + 1
Text = sorted(counts.items(),key = lambda x:x[1],reverse = True)                # 词频排序
print(Text[:10])

柱状图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import matplotlib.pyplot as plt                                                 # 导入matplotlib库
import numpy as np                                                              # 导入numpy库
plt.figure(figsize=(8, 6), dpi=100)                                             # 创建一个点数为 8 x 6 的窗口, 并设置分辨率为 80像素/每英寸
plt.subplot(1, 1, 1)                                                            # 再创建一个规格为 1 x 1 的子图
N = 6                                                                           # 柱子总数
values = (250, 320, 340, 200, 410, 500)                                         # 包含每个柱子对应值的序列
index = np.arange(N)                                                            # 包含每个柱子下标的序列
width = 0.65                                                                    # 柱子的宽度
p2 = plt.bar(index, values, width, label="rainfall", color="#87CEFA")           # 绘制柱状图, 每根柱子的颜色为紫罗兰色
plt.xlabel('Months')                                                            # 设置横轴标签
plt.ylabel('rainfall (mm)')                                                     # 设置纵轴标签
plt.title('Monthly average rainfall')                                           # 添加标题
plt.xticks(index, ('Jan', 'Fub', 'Mar', 'Apr', 'May', 'Jun'))                   # 添加纵横轴的刻度
plt.yticks(np.arange(0, 810, 100))
plt.legend(loc="upper right")                                                   # 添加图例
plt.show()

词云图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from wordcloud import WordCloud                                                 # 导入词云图库
import jieba                                                                    # 导入jieba分词库
import jieba.analyse                                                            # 导入jieba的核心词提取analyse模块
import numpy as np                                                              # 导入NumPy函数库
from PIL import Image                                                           # 导入PIL库中Image
import matplotlib.pyplot as plt                                                 # 导入matplotlib.pyplot库
with open("C:/Users/69099/Desktop/11.txt","r",encoding="utf-8") as file:		# 打开文件
    txt = file.read()									                        # 读取文件
    seg_list = jieba.analyse.extract_tags(txt)                                  # 关键词的提取
    wc_mask = np.array(Image.open("C:/Users/69099/Desktop/QQ图片20231221152454.png"))
    wc = WordCloud(font_path="C:\Windows\Fonts\simkai.TTF",	                    # 设置属性
                          collocations=False,
                          background_color="white",
                          width=1000,
                          height=800,
                          max_font_size=100,
                          contour_color = 'Blue',
                          mask = wc_mask,                                       # 设置背景图
                          contour_width = 2,                                    # 词云边框大小
                          max_words=100).generate(txt)
plt.figure(figsize = [10,10])
plt.imshow(wc)
plt.axis("off")
plt.show()

大一期末作业
http://ronghuasixue.com/2023/10/19/大一期末作业/
作者
茸华似雪
发布于
2023年10月19日
许可协议